java—有没有一种方法可以从单独的类文件中的actionlistener引用setvisible()和dispose()?

x3naxklr  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(398)

我正在开发一个非常复杂的多层swing gui,现在遇到的主要问题是让jbutton actionlistener在一个单独的jframe上执行setvisible(),并立即对当前jframe执行dispose()。因为我的代码很长,所以将main、jframes和actionlistener都拆分为单独的类文件是很重要的。我写了一个非常简化的问题,分为4个小类文件。它们在这里:
文件1:

  1. import javax.swing.*;
  2. public class Test {
  3. public static void main(String[] args) {
  4. JFrame g1 = new GUI1();
  5. g1.pack();
  6. g1.setLocation(200,200);
  7. g1.setVisible(true);
  8. JFrame g2 = new GUI2();
  9. g2.pack();
  10. g2.setLocation(400,200);
  11. g2.setVisible(false);
  12. }
  13. }

文件2:

  1. import javax.swing.*;
  2. public class GUI1 extends JFrame {
  3. JPanel panel;
  4. JButton button;
  5. public GUI1() {
  6. super("GUI1");
  7. setDefaultCloseOperation(EXIT_ON_CLOSE);
  8. panel = new JPanel();
  9. button = new JButton("Create GUI2");
  10. button.addActionListener(new Listener());
  11. add(panel);
  12. add(button);
  13. }
  14. }

文件3:

  1. import javax.swing.*;
  2. public class GUI2 extends JFrame {
  3. JPanel panel;
  4. JLabel label;
  5. public GUI2() {
  6. super("GUI2");
  7. setDefaultCloseOperation(EXIT_ON_CLOSE);
  8. panel = new JPanel();
  9. label = new JLabel("I'm alive!");
  10. add(panel);
  11. add(label);
  12. }
  13. }

文件4:

  1. import java.awt.event.*;
  2. public class Listener implements ActionListener {
  3. public void actionPerformed(ActionEvent e) {
  4. GUI2.setVisible(true);
  5. GUI1.dispose();
  6. }
  7. }

如您所见,actionlistener的唯一功能是将gui2设置为visible并释放gui1,但它运行错误“不能从静态上下文引用非静态方法(setvisible(boolean)和dispose())”。我认为这是因为这两个方法都试图引用main中创建的对象,而main是静态的。我的困惑是如何绕过这个问题,而不是把所有的东西都合并到一个类中。
有什么建议吗?谢谢!
编辑:以上代码编译成一个文件。。。尽管它返回完全相同的错误。

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public class Test {
  4. public static void main(String[] args) {
  5. JFrame g1 = new GUI1();
  6. g1.pack();
  7. g1.setLocation(200,200);
  8. g1.setVisible(true);
  9. JFrame g2 = new GUI2();
  10. g2.pack();
  11. g2.setLocation(400,200);
  12. g2.setVisible(false);
  13. }
  14. }
  15. class GUI1 extends JFrame {
  16. JPanel panel;
  17. JButton button;
  18. public GUI1() {
  19. super("GUI1");
  20. setDefaultCloseOperation(EXIT_ON_CLOSE);
  21. panel = new JPanel();
  22. button = new JButton("Create GUI2");
  23. button.addActionListener(new Listener());
  24. add(panel);
  25. add(button);
  26. }
  27. }
  28. class GUI2 extends JFrame {
  29. JPanel panel;
  30. JLabel label;
  31. public GUI2() {
  32. super("GUI2");
  33. setDefaultCloseOperation(EXIT_ON_CLOSE);
  34. panel = new JPanel();
  35. label = new JLabel("I'm alive!");
  36. add(panel);
  37. add(label);
  38. }
  39. }
  40. class Listener implements ActionListener {
  41. public void actionPerformed(ActionEvent e) {
  42. GUI2.setVisible(true);
  43. GUI1.dispose();
  44. }
  45. }
kd3sttzy

kd3sttzy1#

必须将frame1和frame2的示例传递给 ActionListener .

  1. import java.awt.event.*;
  2. public class Listener implements ActionListener {
  3. private JFrame frame1, frame2;
  4. public Listener(JFrame frame1, JFrame frame2) {
  5. this.frame1 = frame1;
  6. this.frame2 = frame2;
  7. }
  8. public void actionPerformed(ActionEvent e) {
  9. frame2.setVisible(true);
  10. frame1.dispose();
  11. }
  12. }

这意味着您必须将frame2的示例传递给 GUI1 班级。

  1. import javax.swing.*;
  2. public class GUI1 extends JFrame {
  3. JPanel panel;
  4. JButton button;
  5. public GUI1(JFrame frame2) {
  6. super("GUI1");
  7. setDefaultCloseOperation(EXIT_ON_CLOSE);
  8. panel = new JPanel();
  9. button = new JButton("Create GUI2");
  10. button.addActionListener(new Listener(this, frame2));
  11. add(panel);
  12. add(button);
  13. }
  14. }

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

  1. import javax.swing.*;
  2. public class Test {
  3. public static void main(String[] args) {
  4. JFrame g2 = new GUI2();
  5. g2.pack();
  6. g2.setLocation(400,200);
  7. g2.setVisible(false);
  8. JFrame g1 = new GUI1(g2);
  9. g1.pack();
  10. g1.setLocation(200,200);
  11. g1.setVisible(true);
  12. }
  13. }
展开查看全部

相关问题