public class SimpleGUI extends JFrame implements ActionListener
{
Container c=getContentPane();
private JPanel inputPanel =new JPanel();
private JLabel prompt=new JLabel("input your name");
private JTextField inField=new JTextField(10);
privateJTextArea display=new JTextArea(10, 30);
private JButton goButton=new JButton("click for greeting");
public SimpleGUI(String title){
buildGUI();
setSize(200, 150);
setLocation(100, 150);
setTitle(title);
pack();
setVisible(true);
}
public void buildGUI(){
//c.setLayout(new BorderLayout());
c.add("Center",display);
inputPanel.add(prompt);
inputPanel.add(inField);
inputPanel.add(goButton);
c.add("South", inputPanel);
goButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==goButton){
String name=inField.getText();
display.append("your name is "+name+"\n");
}
}
public static void main(String[] args) {
new SimpleGUI("MY GUI");
}
}
比如这段程序里,ActionListener接口提供的actionPerformed函数,main函数里没有调用但可以执行。
还有,actionPerformed函数里的this到底指哪个对象啊?