import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test extends JFrame{
private JFrame jframe;
private JButton b1;
private JLabel jlabel ;
private int count=0;
public Test(){
jframe=new JFrame();
jframe.setLayout(null);
jframe.setTitle("测试");
jframe.setBounds(300, 200, 300, 200);
jlabel = new JLabel("测试标签");
jlabel.setBounds(120, 50, 80, 30);
b1=new JButton("点击");
b1.setBounds(100, 100, 80, 30);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//这是根据随机生成的数字0、1设置标签jlabel的文字
// int choice=(int)(Math.random()*2);
// if(choice==0)
// jlabel.setText("你好");
// else if(choice==1)
// jlabel.setText("李明");
//点击一次改变一次标签文字
count++;
if(count%2==0)
jlabel.setText("你好");
else if(count%2==1)
jlabel.setText("李明");
}
});
jframe.add(jlabel);
jframe.add(b1);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test();
}
}
追问辛苦你了