请各位看看我的程序:
import java.awt.*;
import java.awt.event.*;
class Ex922 extends Frame implements ActionListener
{
private Button button1;
private Button button2;
public Ex922(){
super("Button事件演示");
this.setLayout(new FlowLayout());
button1=new Button();
button2=new Button("button2 Label");
button2.addActionListener(this);
button1.addActionListener(this);
this.add(button1);
this.add(button2);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
this.pack();
this.show();
}
public static void main(String args[])
{
Ex922 Ex922Demo=new Ex922();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button2)
{
if(button1.getLabel()=="")
{
button1.setLabel("button1 Label has changed");
this.pack();
}
if(button1.getLabel()=="button1 Label has changed")
{
button1.setLabel("button1 Label") ;
this.pack();
}
}
}
}
这个程序的运行的结果是这样的:单击"button2 Label"按钮后第一个按钮的就会有"button1 Label"的标签。也就是说它只是执行了这段程序:
if(button1.getLabel()=="button1 Label has changed")
{
button1.setLabel("button1 Label") ;
this.pack();
}
如果将上面的程序注释掉,运行是就会执行下面的程序:
if(button1.getLabel()=="")
{
button1.setLabel("button1 Label has changed");
this.pack();
}请问谁能给一个合理的解释?谢谢各位了。
import java.awt.*;
import java.awt.event.*;
class Ex922 extends Frame implements ActionListener
{
private Button button1;
private Button button2;
public Ex922(){
super("Button事件演示");
this.setLayout(new FlowLayout());
button1=new Button();
button2=new Button("button2 Label");
button2.addActionListener(this);
button1.addActionListener(this);
this.add(button1);
this.add(button2);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
this.pack();
this.show();
}
public static void main(String args[])
{
Ex922 Ex922Demo=new Ex922();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button2)
{
if(button1.getLabel()=="")
{
button1.setLabel("button1 Label has changed");
this.pack();
}
if(button1.getLabel()=="button1 Label has changed")
{
button1.setLabel("button1 Label") ;
this.pack();
}
}
}
}
这个程序的运行的结果是这样的:单击"button2 Label"按钮后第一个按钮的就会有"button1 Label"的标签。也就是说它只是执行了这段程序:
if(button1.getLabel()=="button1 Label has changed")
{
button1.setLabel("button1 Label") ;
this.pack();
}
如果将上面的程序注释掉,运行是就会执行下面的程序:
if(button1.getLabel()=="")
{
button1.setLabel("button1 Label has changed");
this.pack();
}请问谁能给一个合理的解释?谢谢各位了。
怎么样给你分?