简体   繁体   English

如何检查另一个Jframe中是否单击了按钮

[英]How to check if a button is clicked in another Jframe

So i have two simple jframes , one is the main frame and the other is visible only when a button is pressed. 因此,我有两个简单的jframe ,一个是主框架 ,另一个仅在按下按钮时可见。

在此处输入图片说明

在此处输入图片说明

What I'm trying to do now is to display which button is being pressed in the second jframe , whether its toy or food in the jlabel in the first jframe. 我现在想要做的是在第二个jframe中 显示正在按下哪个按钮 ,无论它的玩具还是食物都在第一个jframe中显示。

The button launch selection in the first jframe will link to the second jframe, then the user clicks one of the two button and the button that was clicked will be displayed in the jlabel such as "Toy button was clicked" 第一个jframe中的按钮启动选择将链接到第二个jframe,然后用户单击两个按钮中的一个,被单击的按钮将显示在j标签中,例如“单击了玩具按钮”。

I implemented how the two jframes linked by: 我实现了两个jframe如何通过以下方式链接:

class SelectionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        Object_Selection object_select = new Object_Selection(); //launch the second jframe
        object_select.setVisible(true);
    }
}

But I'm having issue on displaying which button was pressed in the second jframe in the jlabel of the first jframe. 但是我在显示第一个jframe的jlabel中的第二个jframe中按下哪个按钮时遇到了问题。

Here an one-file mcve (copy paste the entire code into one file OpenDialogWindow.java , and run) demonstrating what you want to achieve: 这里是一个文件的mcve(将整个代码复制粘贴到一个文件OpenDialogWindow.java ,然后运行),演示您要实现的目标:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class OpenDialogWindow {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Main Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(400,250);
        frame.add(new Pane());
        frame.pack();
        frame.setVisible(true);
    }
}

class Pane extends JPanel{

    private static int WIDTH = 300, HEIGHT = 100, GAP  = 5;
    private final JLabel label;

    Pane() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setLayout(new BorderLayout(GAP,GAP));

        label = new JLabel("", JLabel.CENTER);
        add(label, BorderLayout.PAGE_START);

        JButton show = new JButton("Show Dialog");
        show.addActionListener(e-> new Diag(new DiagButtonListener()));
        add(show, BorderLayout.PAGE_END);
    }

    class DiagButtonListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setText("Diag button clicked !");
        }
    }
}

class Diag extends JDialog  {

    public  Diag(ActionListener listener) {

        setTitle("Dialog window");
        setSize(300, 150);
        setLocation(450,400);

        JButton btn = new JButton("Click");
        btn.addActionListener(listener);

        add(btn, BorderLayout.NORTH);

        JLabel help = new JLabel("Click button and see parent frame updted", JLabel.CENTER);
        add(help, BorderLayout.SOUTH);
        setVisible(true);
    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM