gpt4 book ai didi

java - 监听另一个组件中的事件并等待回复

转载 作者:行者123 更新时间:2023-11-30 05:40:55 27 4
gpt4 key购买 nike

我想向我的 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);
}
}

我想要添加一些代码的部分已被注释,并且我想要添加的功能已被解释。

最佳答案

这是一个在 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);
}

}

关于java - 监听另一个组件中的事件并等待回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55694584/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com