有没有办法在单独的类文件中从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(静态)中创建的对象。我的困惑是如何解决这个问题,而又没有将所有内容都组合到一个类中。

有什么建议吗?谢谢!

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

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);
      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的实例传递到private string Parse(string myString,int myNumber = 0,string name = null)

ActionListener

这意味着您必须将frame2的实例传递给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(); } } 类。

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);
   }

}