【问题标题】:Is there a way to reference setVisible() and dispose() from an ActionListener in a separate class file?有没有办法在单独的类文件中从 ActionListener 引用 setVisible() 和 dispose() ?
【发布时间】:2020-08-23 03:40:24
【问题描述】:

我正在开发一个非常复杂的多层 Swing GUI,我现在遇到的主要问题是让 JButton ActionListener 在单独的 JFrame 上执行 setVisible() 并立即 dispose() 当前框架。由于我的代码很长,重要的是 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);
      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();
   
   }
}

【问题讨论】:

标签: java swing actionlistener


【解决方案1】:

您必须将 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);  
   }

}

【讨论】:

  • 谢谢吉尔伯特!!!这正是我所需要的!!!
猜你喜欢
  • 2018-06-23
  • 2020-12-03
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2017-03-17
  • 1970-01-01
  • 2016-02-14
  • 2019-09-12
相关资源
最近更新 更多