新手问一个JButton的问题!!
jb1=new JButton和JButton jb1=new JButton的区别???
下面代码,在我@的地方,为什么写两种不一样现象??第一种是可以自动加1减1,第二种直接退出程序为什么???
求大神解答!!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Selfmotion1 extends JFrame implements ActionListener{
JTextField jtext;
JButton jb1;
JButton jb2;
public Selfmotion1(){
super("按钮综合应用");
Container w1Container=this.getContentPane();
w1Container.setLayout(null);
jtext=new JTextField("95");
jtext.setBounds(60,20,150,30);
w1Container.add(jtext);
@jb1=new JButton("自动加1");//JButton jb1=new JButton("自动加1");
jb1.addActionListener(this);
jb1.setBounds(20, 150, 100, 30);
w1Container.add(jb1);
@jb2=new JButton("退出");//JButton jb2=new JButton("退出");
jb2.addActionListener(this);
jb2.setBounds(140, 150, 100, 30);
w1Container.add(jb2);
}
public static void main(String[] args) {
Selfmotion1 s1=new Selfmotion1();
s1.setSize(500,350);
s1.setVisible(true);
}
public void actionPerformed(ActionEvent e){
JButton button=(JButton)e.getSource();
if(button==jb1){
int value=Integer.parseInt(jtext.getText().trim());
String text = jb1.getText().trim();
if (text.equals("自动加1")) {
if(value==99)
jb1.setText("自动减1");
value++;
jtext.setText(value + "");
}
else{
if(value==1)
jb1.setText("自动加1");
value--;
jtext.setText(value + "");
}
}
else{
System.exit(0);
}
}
}