【问题标题】:Can I communicate between two JFrames WITHOUT using static variables or methods?我可以在不使用静态变量或方法的情况下在两个 JFrame 之间进行通信吗?
【发布时间】:2020-08-24 05:34:52
【问题描述】:

基本上,我试图让 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");
   }
}

【问题讨论】:

  • 通常你会使用观察者模式,在 Swing 中使用 ActionListener 来实现。
  • 可以吗?是的。你能做到吗?这是你应该问自己的问题。有消息传递,使用队列,您可以让他们访问彼此的实例并使用实例方法,您可以使用观察者模式..
  • 我看到整个代码都已经准备好了,OP 只需要将它们按正确的顺序放在一起。这让我相信这是一个考试或家庭作业问题。也许至少 OP 可以发布一些实际编译的代码?

标签: java swing static jframe actionlistener


【解决方案1】:

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

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

我所做的主要更改是在 Frame2 类中保留对 JLabel 的引用。我将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");
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 2017-09-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2016-06-29
    • 2021-12-30
    相关资源
    最近更新 更多