gpt4 book ai didi

java - 一个类中的事件可以触发另一个类中的操作吗?

转载 作者:行者123 更新时间:2023-11-29 03:17:38 25 4
gpt4 key购买 nike

我正在编写一个按顺序完成以下任务的程序:

  1. 从 JPanel 收集用户输入
  2. 使用输入将程序目录中的依赖项复制到新的项目目录中
  3. 使用输入在项目目录中构建交互图

我有一个用于每个任务的单独类和一个按顺序调用每个对象的主类。

我的问题是主类在第 1 步完成之前评估第 2 步。因为在主类调用对象2时用户还没有关闭JPanel,所以在步骤2开始之前没有收集用户输入,程序崩溃。

我需要的是一种向第 2 类发出信号,表明第 1 类中的 JPanel 已关闭。这样,第 2 步在第 1 步收集输入字段后开始。

有没有办法让类 1 中的窗口关闭触发类 2 中的操作?如果没有,解决此问题的最佳方法是什么?

最佳答案

"Is there a way to have the window closing in class 1 trigger an action in class 2? If not, what would be the best way to solve this problem?"

正如 Boris the Spider 所指出的,您应该使用模型对话框。您可能正在使用框架。您应该阅读 Modality 以了解其行为和功能。还要花点时间看看 How to make Dialogs 。简而言之,打开对话框的模式(默认为 JOptionPane 静态 showXxx 方法,可以通过 setModalityType 或通过 constructorJDialog 上设置),流程将“阻塞”直到对话框关闭。

下面是一个例子。对于这样一个简单的任务来说,它可能过于复杂(因为它可以使用 JOptionPane 轻松完成),但它展示了如何使用 JDialog。查看 ShowDialogActionListener 类。对话框设置为可见,并且在对话框关闭之前不会在 actionPerformed 中继续流程,也就是从对话框中获取 Input 时。

import java.awt.GridBagLayout;
import java.awt.GridLayout;
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.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class DialogDemo {
private JFrame frame = new JFrame();

public DialogDemo() {
JButton button = new JButton("Open Dialog");
button.addActionListener(new ShowDialogActionListener());
frame.setLayout(new GridBagLayout());
frame.add(button);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

class ShowDialogActionListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
InputDialog dialog = new InputDialog(frame, true);
System.out.println("Opened dialog.....");

long start = System.currentTimeMillis();
dialog.setVisible(true);
System.out.println("Dialog closed after "
+ (System.currentTimeMillis() - start) + " ms");

Input input = dialog.getInput();
ServiceOne service = new ServiceOne();
service.serviceMethod(input);
}
}

class ServiceOne {
public void serviceMethod(Input input) {
System.out.println(input.getInput());
}
}

class InputDialog extends JDialog {
private Input input;

public InputDialog(JFrame parent, boolean modal) {
super(parent, modal);

JPanel panel = new JPanel(new GridLayout(0, 1));
final JTextField field = new JTextField(20);
JButton okButton = new JButton("OK");
panel.add(field);
panel.add(okButton);

okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = field.getText();
input = new Input();
input.setInput(text);
InputDialog.this.dispose();
}
});

setLayout(new GridBagLayout());
add(panel);
setSize(250, 250);
setLocationRelativeTo(parent);
}

public Input getInput() {
return input;
}
}

class Input {
private String input = "default";

public void setInput(String input) {
this.input = input;
}

public String getInput() {
return input;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DialogDemo();
}
});
}
}

正如我之前所说,用

可以很容易地完成同样的事情
String input = JOptionPane.showInputDialog("Enter a Message");

上述内容也会阻止流程执行。

关于java - 一个类中的事件可以触发另一个类中的操作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584656/

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