我无法理解actionListener在以下代码中是如何使用的,以及addWindowListener方法在下面的代码中做了什么:
请帮助我。
public class SwingListenerDemo {
private JFrame mainFrame;
private JLabel statusLabel;
public SwingListenerDemo(){
prepareGUI(); }
public static void main(String[] args){
SwingListenerDemo swingListenerDemo = new SwingListenerDemo();
swingListenerDemo.showActionListenerDemo();}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
mainFrame.setVisible(true);
}
private void showActionListenerDemo(){
JButton okButton = new JButton("OK");
okButton.addActionListener(new CustomActionListener());
mainFrame.add(okButton);
mainFrame.setVisible(true); }
class CustomActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
statusLabel.setText("Ok Button Clicked.");
}
}
}
答案 0 :(得分:1)
actionPerformed
方法将被调用okButton.addActionListener(new CustomActionListener());
windowClosing
方法中所做的事情。