我可以在不使用静态变量或方法的情况下在两个JFrame之间进行通信吗?

问题描述

基本上,我试图在Frame1中获取JButton来编辑Frame2中的JLabel。我知道如果将Frame2中的JLabel和getLabel()方法设置为静态,并让ActionListener直接引用Frame1,它可以工作,但是我想知道是否有一种方法可以不使用静态变量或方法

这是代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class Test {

   public static void main(String[] args) {
      
      Frame2 f2 = new Frame2();
      f2.pack();
      f2.setLocation(700,400);
      f2.setVisible(true);

      Frame1 f1 = new Frame1(f2);
      f1.pack();
      f1.setLocation(400,400);
      f1.setVisible(true);
   }
}

class Frame1 extends JFrame {

   JButton button;

   public Frame1(JFrame f) {
      
      super("Frame 1");
      setDefaultCloSEOperation(EXIT_ON_CLOSE);

      button = new JButton("Button");

      add(button);
      
      button.addActionListener(new Listener(f.getLabel()));
   }
}

class Frame2 extends JFrame {

   JLabel label;

   public Frame2() {
            
      super("Frame 2");
      setDefaultCloSEOperation(EXIT_ON_CLOSE);
      
      label = new JLabel("hello");

      add(label);
   }
   
   public JLabel getLabel() {
      return label;
   }  
}

class Listener implements ActionListener {
   
   private JLabel lab;

   public Listener(JLabel lab) {
      this.lab = lab;
   }  
   
   public void actionPerformed(ActionEvent e) {
     
      lab.setText("nice");
   }
}

有什么建议吗?谢谢!

编辑:这是代码的可编译版本-label和getLabel()是静态的,并且ActionListener在被调用时直接引用JFrame1。我的目标是没有静态变量或方法(在main之外)。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class Test {

   public static void main(String[] args) {
          
      Frame2 f2 = new Frame2();
      f2.pack();
      f2.setLocation(700,400);
      f2.setVisible(true);
      
      Frame1 f1 = new Frame1(f2);
      f1.pack();
      f1.setLocation(400,400);
      f1.setVisible(true);
   }
}

class Frame1 extends JFrame {

   JButton button;

   public Frame1(JFrame f) {
      
      super("Frame 1");
      setDefaultCloSEOperation(EXIT_ON_CLOSE);

      button = new JButton("Button");

      add(button);
      
      button.addActionListener(new Listener(Frame2.getLabel()));
   }
}

class Frame2 extends JFrame {

   static JLabel label;

   public Frame2() {
            
      super("Frame 2");
      setDefaultCloSEOperation(EXIT_ON_CLOSE);
      
      label = new JLabel("hello");

      add(label);
   }
   
   public static JLabel getLabel() {
      return label;
   }  
}

class Listener implements ActionListener {
   
   private JLabel lab;

   public Listener(JLabel lab) {
      this.lab = lab;
   }  
   
   public void actionPerformed(ActionEvent e) {
     
      lab.setText("nice");
   }
}

解决方法

Oracle的确很漂亮Swing tutorial。我认为您学习本教程将是一个非常好的主意。

我必须对您的代码进行大量更改才能使其执行。

我所做的主要更改是在JLabel类中保留对Frame2的引用。我将Frame2的实例传递给Listener类。就像我昨天对您先前的问题所做的一样。

这是代码。在明天再问另一个问题之前,花点时间研究我的工作。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DoubleJFrameTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame2 f2 = new Frame2();
                f2.pack();
                f2.setLocation(700,400);
                f2.setVisible(true);

                Frame1 f1 = new Frame1(f2);
                f1.pack();
                f1.setLocation(400,400);
                f1.setVisible(true);
            }
        });
    }
}

class Frame1 extends JFrame {
    private static final long serialVersionUID = 1L;

    private JButton button;

    public Frame1(Frame2 f) {
        super("Frame 1");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new BorderLayout());
        button = new JButton("Button");
        button.addActionListener(new Listener(f));

        panel.add(button,BorderLayout.CENTER);
        add(panel);
    }
}

class Frame2 extends JFrame {
    private static final long serialVersionUID = 1L;

    private JLabel label;

    public Frame2() {
        super("Frame 2");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new BorderLayout());
        label = new JLabel("hello");

        panel.add(label,BorderLayout.CENTER);
        add(panel);
    }

    public void setLabelText(String text) {
        label.setText(text);;
    }
}

class Listener implements ActionListener {
    private Frame2 frame;

    public Listener(Frame2 frame) {
        this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        frame.setLabelText("nice");
    }

}