主题:[讨论]WindowListener接口的问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowDemo extends JFrame implements ActionListener,WindowListener
{ JButton b1,b2,b3;
JPanel p1,p2;
JTextArea jta;
Container cp=null;
public WindowDemo()
{ p1=new JPanel();
jta=new JTextArea(5,5);
p1.setLayout(new BorderLayout());
p1.add(jta,BorderLayout.CENTER);
jta.setLineWrap(true);
p2=new JPanel();
p2.setLayout(new FlowLayout());
p2.add(b1=new JButton("OK"));
p2.add(b2=new JButton("Cancel"));
p2.add(b3=new JButton("Exit"));
cp=getContentPane();
cp.setLayout(new BorderLayout());
cp.add(p1,BorderLayout.CENTER);
cp.add(p2,BorderLayout.SOUTH);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addWindowListener(new CloseWindow()); /这里有误/
pack();
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{ String command=e.getActionCommand();
if(command.equals("OK"))
jta.setText("OK");
else if(command.equals("Cancel"))
jta.setText("Cancel");
}
class CloseWindow extends WindowAdapter
{ public void windowClosing(WindowEvent e)
{ System.exit(0);}
}
public static void main(String args[])
{ new WindowDemo();}
}
上面的程序提示我 找不着变量b3 但我不明白怎么为何 请明白人帮忙看看 谢谢
import java.awt.event.*;
import javax.swing.*;
public class WindowDemo extends JFrame implements ActionListener,WindowListener
{ JButton b1,b2,b3;
JPanel p1,p2;
JTextArea jta;
Container cp=null;
public WindowDemo()
{ p1=new JPanel();
jta=new JTextArea(5,5);
p1.setLayout(new BorderLayout());
p1.add(jta,BorderLayout.CENTER);
jta.setLineWrap(true);
p2=new JPanel();
p2.setLayout(new FlowLayout());
p2.add(b1=new JButton("OK"));
p2.add(b2=new JButton("Cancel"));
p2.add(b3=new JButton("Exit"));
cp=getContentPane();
cp.setLayout(new BorderLayout());
cp.add(p1,BorderLayout.CENTER);
cp.add(p2,BorderLayout.SOUTH);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addWindowListener(new CloseWindow()); /这里有误/
pack();
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{ String command=e.getActionCommand();
if(command.equals("OK"))
jta.setText("OK");
else if(command.equals("Cancel"))
jta.setText("Cancel");
}
class CloseWindow extends WindowAdapter
{ public void windowClosing(WindowEvent e)
{ System.exit(0);}
}
public static void main(String args[])
{ new WindowDemo();}
}
上面的程序提示我 找不着变量b3 但我不明白怎么为何 请明白人帮忙看看 谢谢