Is there a way to reference setVisible() and dispose() from an ActionListener in a separate class file?

huangapple go评论72阅读模式
英文:

Is there a way to reference setVisible() and dispose() from an ActionListener in a separate class file?

问题

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();
   
   }
}
英文:

I'm working on a very complicated, multi-layered Swing GUI, and the main issue I'm running into right now involves having a JButton ActionListener perform setVisible() on a separate JFrame and immediately dispose() of the current JFrame. Because of the length of my code, it's important that main, both JFrames, and the ActionListener are all split into individual class files. I wrote a VERY simplified version of my problem, split into 4 tiny class files. Here they are:

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

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

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

File 4:

import java.awt.event.*;
public class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
GUI2.setVisible(true);
GUI1.dispose();
}
}

As you can see, the only function of the ActionListener is to set GUI2 to visible and dispose of GUI1, but it runs the error "non-static method (setVisible(boolean) and dispose()) cannot be referenced from a static context". I figure this is because both methods are trying to reference objects that were created in main, which is static. My confusion is how to get around this, WITHOUT combining everything into one class.

Any suggestions? Thanks!

EDIT:
Here's the above code compiled into one file... although it returns the exact same error.

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

答案1

得分: 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);  
   }

}
英文:

You have to pass instances of frame1 and frame2 to your 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();
}
}

This means you have to pass an instance of frame2 to your GUI1 class.

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

This means you have to create the frames in the reverse order.

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

huangapple
  • 本文由 发表于 2020年8月23日 11:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63543145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定