窗口的创建与监听事件的处理不在同一个类,如何实现关闭前一个窗口?
因为我用JFrame创建窗口的类,和JButton监听事件处理的类不在同一个类,无法使用this.dispose()
所以请问一下,跨类如何使用dispose()或者setVisible(false)关闭前一个窗口?
具体代码如下:
public class enterWindow extends JFrame {
enterWindow(String title){
super(title);
//窗口组件代码
.........
JButton enterButton = new JButton( "登陆 ");
buttonPanel.add(enterButton);
enterButton.addActionListener(
new Command(Command.button_enter));
.........
setVisible(true);
}
}
class Command implements ActionListener {
static final int button_enter = 9;
Command(int button){
curButton = button;
}
public void actionPerformed(ActionEvent e){
switch(curButton){
case button_enter:
funtionWindow fWindow = new funtionWindow( ".. ");
break;
}
}
}
过程就是点击“登陆”按钮,创建一个新的窗口!请问如何关闭前面的登陆窗口?(“登陆窗口”和“事件处理”是两个分开了类~)
谢谢!
------解决方案--------------------.........
JButton enterButton = new JButton( "登陆 ");
buttonPanel.add(enterButton);
enterButton.addActionListener(
new Command(Command.button_enter,this));//修改构造函数
.........
class Command implements ActionListener {
static final int button_enter = 9;
JFrame jFrame;
Command(int button,JFrame jFrame){
curButton = button;
this.jFrame = jFrame;
}
public void actionPerformed(ActionEvent e){
switch(curButton){
case button_enter:
jFrame.dispose(); //关闭
funtionWindow fWindow = new funtionWindow( ".. ");
break;
}
}
}