有没有办法在一个单独的类文件中引用ActionListener中的setVisible()和dispose()



我正在开发一个非常复杂的多层Swing GUI,我现在遇到的主要问题是让JButton ActionListener在一个单独的JFrame上执行setVisible((,并立即处理当前JFrame的((。由于我的代码很长,所以将main、JFrame和ActionListener都拆分为单独的类文件是很重要的。我写了一个非常简化的问题版本,分成4个小的类文件。它们在这里:

文件1:

import javax.swing.*;
public class Test {
public static void main(String[] args) {

JFrame g1 = new GUI1();
g1.pack();
g1.setLocation(200,200);
g1.setVisible(true);

JFrame g2 = new GUI2();
g2.pack();
g2.setLocation(400,200);
g2.setVisible(false);

}
}

文件2:

import javax.swing.*;
public class GUI1 extends JFrame {
JPanel panel;
JButton button;

public GUI1() {

super("GUI1");
setDefaultCloseOperation(EXIT_ON_CLOSE);

panel = new JPanel();
button = new JButton("Create GUI2");
button.addActionListener(new Listener());

add(panel);
add(button);

}
}

文件3:

import javax.swing.*;
public class GUI2 extends JFrame {
JPanel panel;
JLabel label;

public GUI2() {

super("GUI2");
setDefaultCloseOperation(EXIT_ON_CLOSE);

panel = new JPanel();
label = new JLabel("I'm alive!");

add(panel);
add(label);

}
}

文件4:

import java.awt.event.*;
public class Listener implements ActionListener {

public void actionPerformed(ActionEvent e) {

GUI2.setVisible(true);
GUI1.dispose();

}
}

正如您所看到的,ActionListener的唯一功能是将GUI2设置为可见并处理GUI1,但它运行错误";非静态方法(setVisible(boolean(和dispose(((不能从静态上下文中引用;。我认为这是因为这两个方法都试图引用在main中创建的对象,main是静态的。我的困惑是如何绕过这一点,而不将所有内容合并到一个类中。

有什么建议吗?谢谢

编辑:这是上面编译成一个文件的代码。。。尽管它返回完全相同的错误。

import javax.swing.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {

JFrame g1 = new GUI1();
g1.pack();
g1.setLocation(200,200);
g1.setVisible(true);

JFrame g2 = new GUI2();
g2.pack();
g2.setLocation(400,200);
g2.setVisible(false);

}
}
class GUI1 extends JFrame {
JPanel panel;
JButton button;

public GUI1() {

super("GUI1");
setDefaultCloseOperation(EXIT_ON_CLOSE);

panel = new JPanel();
button = new JButton("Create GUI2");
button.addActionListener(new Listener());

add(panel);
add(button);

}
}
class GUI2 extends JFrame {
JPanel panel;
JLabel label;

public GUI2() {

super("GUI2");
setDefaultCloseOperation(EXIT_ON_CLOSE);

panel = new JPanel();
label = new JLabel("I'm alive!");

add(panel);
add(label);

}
}
class Listener implements ActionListener {

public void actionPerformed(ActionEvent e) {

GUI2.setVisible(true);
GUI1.dispose();

}
}

您必须将frame1和frame2的实例传递给ActionListener

import java.awt.event.*;
public class Listener implements ActionListener {
private JFrame frame1, frame2;
public Listener(JFrame frame1, JFrame frame2) {
this.frame1 = frame1;
this.frame2 = frame2;
}

public void actionPerformed(ActionEvent e) {
frame2.setVisible(true);
frame1.dispose();
}
}

这意味着您必须将frame2的一个实例传递给GUI1类。

import javax.swing.*;
public class GUI1 extends JFrame {
JPanel panel;
JButton button;

public GUI1(JFrame frame2) {

super("GUI1");
setDefaultCloseOperation(EXIT_ON_CLOSE);

panel = new JPanel();
button = new JButton("Create GUI2");
button.addActionListener(new Listener(this, frame2));

add(panel);
add(button);
}
}

这意味着您必须以相反的顺序创建框架。

import javax.swing.*;
public class Test {
public static void main(String[] args) {

JFrame g2 = new GUI2();
g2.pack();
g2.setLocation(400,200);
g2.setVisible(false);
JFrame g1 = new GUI1(g2);
g1.pack();
g1.setLocation(200,200);
g1.setVisible(true);  
}
}

最新更新