I have written a small code in java for simpleGUI.
package guidemo1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GuiDemo1 implements ActionListener{
JButton button;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GuiDemo1 gui=new GuiDemo1();
gui.go();
}
public void go()
{
JFrame frame=new JFrame();
button=new JButton();
frame.getContentPane().add(button);
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
button.setText("I've been clicked");
}
}
I am newbie to JAVA.I have few questions related to this program.
Can some one explain how actionPerformed method gets executed with out any call?
Here I have defined frame object locally to the go() method and we are using button in actionPerformed which is another method.How is that possible?Isn't the button gets embedded on the frame?
Thanks..
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…