侦听另一个组件中的事件并等待答复

时间:2019-04-15 17:38:20

标签: java swing user-interface

我想在我的GUI中添加一个JPanel,它将具有一些按钮,例如b1,b2,b3。按下按钮(例如b)时,将在应用程序已经运行时动态添加此JPanel。当我显示JPanel时,直到按下按钮b1,b2,b3之一,按钮b才起作用。

我创建了包含按钮b1,b2,b3的JPanel类,并添加了事件处理程序,并在按钮b的鼠标事件处理程序内部,实例化了jPanel的对象。因此,正在显示JPanel,但是按钮b不在等待b1,b2和b3的响应。

按钮b的其余事件代码将取决于b1,b2和b3之间的选择。因此它将暂停该行代码,直到做出选择为止。

在选择其中一个按钮之前,如何停止按钮b?

这是MCVE ...

1)主班...

package com.company;

import javax.swing.*;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new Frame();
        frame.setVisible(true);
    }
}

2)框架类...

import java.awt.event.MouseEvent;

public class Frame extends JFrame {
    public Frame() {
        super.setLayout(new FlowLayout());
        JButton b = new JButton("Click Me!!");
        b.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Panel panel = new Panel();
                Frame.super.add(panel);

                // After this I have more handling code, which depends on which button was chosen.
                // But I cant stop the execution at this point and make it wait for a choice.

                Frame.super.revalidate();
            }
        });
        super.add(b);
        super.validate();
    }
}

3)面板课程...

package com.company;

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

public class Panel extends JPanel {
    public Panel() {
        super.setLayout(new FlowLayout());
        super.setBackground(Color.BLACK);

        MouseAdapter listener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JButton button = (JButton) e.getSource();
                // return button ??
                // return the name of the button to the event handler of the button b in the JFrame for further execution.
            }
        };

        JButton b1 = new JButton("option 1");
        b1.addMouseListener(listener);
        JButton b2 = new JButton("option 2");
        b2.addMouseListener(listener);
        JButton b3 = new JButton("option 3");
        b3.addMouseListener(listener);

        super.add(b1);
        super.add(b2);
        super.add(b3);
    }
}

注释了我要添加一些代码的部分,并说明了我要添加的功能。

2 个答案:

答案 0 :(得分:1)

这是一个工作示例,在JFrame的顶部显示一个对话框,并禁用JFrame直到对话框关闭,然后在对话框关闭时处理对话框的单击。

package javaapplication4;

import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class Frame extends JFrame implements FocusListener {

    Window window;
    JFrame jFrame;
    JDialog jDialog;
    String whichButton;

    public Frame() {
        this.jDialog = initDialog();
        this.jFrame = initFrame();
    }

    private JFrame initFrame() {

        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);

        JButton b = new JButton("Click Me!!");
        b.requestFocusInWindow();
        b.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Button b clicked");
                setWhichButton("");
                getjFrame().setEnabled(false);
                getjDialog().setVisible(true);
                getjDialog().requestFocusInWindow();
                // the FocusListener will handle input from dialog
            }
        });
        add(b);

        try {
            JDialog jd = getjDialog();
            add(jd);
        } catch (Exception ex) {
            // swallow for now
        }

        setWindow(this);

        return this;

    }

    @Override
    public void focusGained(FocusEvent e) {
        String selectedButton = getWhichButton();
        switch (selectedButton) {
            case "b1":
                System.out.println("Button b1 was clicked");
                break;
            case "b2":
                System.out.println("Button b2 was clicked");
                break;
            case "b3":
                System.out.println("Button b3 was clicked");
                break;
            default:
        }
    }

    @Override
    public void focusLost(FocusEvent e) {
    }

// The Panel to add dynamically

    private JDialog initDialog() {
        JDialog jd = new JDialog();
        setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        jd.setLayout(new FlowLayout());
        jd.setFocusable(true);
        jd.setSize(300, 300);
        jd.setVisible(false);
        jd.setLocation(100, 50);
        JButton b1 = new JButton("option 1");
        b1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Button b1 clicked");
                setWhichButton("b1");
                getjDialog().setVisible(false);
                getjFrame().setEnabled(true);
                getjFrame().requestFocusInWindow();
            }
        });

        JButton b2 = new JButton("option 2");
        b2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Button b2 clicked");
                setWhichButton("b2");
                getjDialog().setVisible(false);
                getjFrame().setEnabled(true);
                getjFrame().requestFocusInWindow();
            }
        });

        JButton b3 = new JButton("option 3");
        b3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Button b3 clicked");
                setWhichButton("b3");
                getjDialog().setVisible(false);
                getjFrame().setEnabled(true);
                getjFrame().requestFocusInWindow();
            }
        });

        jd.add(b1);
        jd.add(b2);
        jd.add(b3);

        jd.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                getjFrame().setEnabled(true);
            }
        });
        return jd;
    }

    public Window getWindow() {
        return window;
    }

    private void setWindow(Window window) {
        this.window = window;
    }

    public JDialog getjDialog() {
        return jDialog;
    }

    public void setjDialog(JDialog jDialog) {
        this.jDialog = jDialog;
    }

    public JFrame getjFrame() {
        return jFrame;
    }

    public void setjFrame(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public String getWhichButton() {
        return whichButton;
    }

    public void setWhichButton(String whichButton) {
        this.whichButton = whichButton;
    }

    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
    }

}

答案 1 :(得分:0)

您可以调用成员函数.setDisabled()禁用b,然后使用.setEnabled()重新启用它。您可以轻松地在按下b时将其禁用,并在按下b1,b2或b3时使用动作侦听器将其重新启用。