java 在java中链接两个框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9916351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:53:40  来源:igfitidea点击:

Linking two frames in java

javaswing

提问by Aang Namo

I am creating a java application which consists of two frames(JFrame1 and JFrame2) JFrame1 has a grid 6x6 button; and JFrame2 has 6 radio buttons representing colours. How can I link the two frames so that when a button in JFrame1 is clicked, JFrame2 pops up and when a colour is chosen from that the JFrame2 closes and the clicked button gets the respective colour?

我正在创建一个由两个框架(JFrame1 和 JFrame2)组成的 Java 应用程序 JFrame1 有一个网格 6x6 按钮;JFrame2 有 6 个单选按钮代表颜色。如何链接两个框架,以便在单击 JFrame1 中的按钮时,JFrame2 会弹出,并且当从中选择一种颜色时,JFrame2 会关闭并且单击的按钮会获得相应的颜色?

回答by Costis Aivalis

It is better to have one JFrame for every application. Use one for the 6x6 JButtons and create a modal JDialog for your color JRadioButtons.

最好为每个应用程序使用一个 JFrame。为 6x6 JButtons 使用一个,并为您的颜色 JRadioButtons 创建一个模态 JDialog。

  1. The color selection JDialog should have a public getSelectedColor() method in order to return the selected color to the caller class.
  2. Instantiate the ColorDialog in main and do not set it visible.
  3. The ActionListener of each JButton should make the modal JDialog visible.
  4. The RadioButton ActionPerformed should set the selected color and make the JDialog invisible.
  5. Call getSelectedColor() and apply the returned color to your JButton.
  1. 颜色选择 JDialog 应该有一个公共 getSelectedColor() 方法,以便将选定的颜色返回给调用者类。
  2. 在 main 中实例化 ColorDialog 并且不要将其设置为可见。
  3. 每个 JButton 的 ActionListener 应该使模态 JDialog 可见。
  4. RadioButton ActionPerformed 应设置所选颜色并使 JDialog 不可见。
  5. 调用 getSelectedColor() 并将返回的颜色应用到您的 JButton。

回答by Rorchackh

In your frame1's button action listner, you can do something like this

在 frame1 的按钮动作侦听器中,您可以执行以下操作

public void actionPerformed(ActionEvent e) {
   Frame2 frame = new Frame2(this);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
}

where "this" refers to the frame1 object. This is you can access its jTEXTFIELD and jBUTTONs from the second frame. So naturally you have store it in Frame1 object declared in your second class.

其中“this”指的是 frame1 对象。这是你可以从第二帧访问它的 jTEXTFIELD 和 jBUTTONs。所以很自然地,您将它存储在第二个类中声明的 Frame1 对象中。

Suppose you have a clickable color field in frame2 object, once you click on it, you should trigger a function that get the input field from frame1 (using your locale object reference) and store it in it. something like this:

假设您在 frame2 对象中有一个可点击的颜色字段,一旦您点击它,您应该触发一个函数,该函数从 frame1 获取输入字段(使用您的语言环境对象引用)并将其存储在其中。像这样:

public void actionPerformed(ActionEvent e) {
    frame1.getMyTextField().setText(WHAT_THE_CLICKED_ON);
    this.close();
}

Sorry if I made any syntax errors, it's been a long time I didnt work with java :)

抱歉,如果我犯了任何语法错误,我已经很长时间没有使用 Java 了 :)

回答by chenyi1976

Just create another class, let us say FrameMananger, then use the singleton pattern to manage them.

只需创建另一个类,让我们说 FrameMananger,然后使用单例模式来管理它们。

Then in any class, you can use FrameManager.getFrame1() to get the frame1, same as the frame2. You can add logic judgement inside, like dynamically dispose some frame or only create them when needed.

那么在任何一个类中,都可以使用FrameManager.getFrame1()来获取frame1,和frame2一样。你可以在里面添加逻辑判断,比如动态配置一些框架或者只在需要的时候创建它们。

This issue is fairly common concept when you create a game and try to navigate between your every view(like the show score panel from everywhere).

当您创建游戏并尝试在您的每个视图之间导航时,这个问题是相当普遍的概念(例如来自任何地方的显示分数面板)。

public class FrameManager
{
    Frame1 frame1;
    Frame1 frame2;

    public static Frame1 getFrame1()
    {
        if(frame1 == null)
            frame1 = new Frame1();
        return frame1;
    }

    public static Frame1 getFrame2()
    {
        if(frame2 == null)
            frame2 = new Frame1();
        return frame2;
    }

    public class Frame1 extends JFrame
    {

    }

    public class Frame2 extends JFrame
    {

    }
}