gpt4 book ai didi

java - 从对话框中获取值(value)而不关闭它?

转载 作者:行者123 更新时间:2023-12-01 17:27:15 27 4
gpt4 key购买 nike

我有一个应用程序,它使用 JDialog 获取用户的输入,然后搜索文件,不是浏览对话框,而是使用元数据的更专业的对话框。

这一切都运行良好。唯一的问题是我希望能够让用户输入搜索值,按“确定”,并接收这些值来执行搜索和其他一些操作(来自打开对话框的调用类),而无需关闭对话框?

有必要从调用类中执行这些操作,因为这是编辑器中插件的一部分。

基本上,简而言之,它有点像“查找”对话框在任何编辑器中的工作方式 - 当您从一个找到的项目跳到下一个时,查找对话框保持打开状态...

似乎我错过了一些简单的事情,但我不知道如何做到这一点。

编辑:

我根据 Nick Rippe 建议的教程在一个简单的测试应用程序中尝试了这一点,但我认为我在某种程度上误解了它,因为我无法让它工作。我添加了一个带有 getter 和 setter 的字段,然后尝试获取它:

主类:

public class TestJFrames {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
TestForm frame = new TestForm();
frame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
frame.addPropertyChangeListener("fileSelected", new FileSelectedListener());
frame.setVisible(true);
}
}

class FileSelectedListener implements PropertyChangeListener {

@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("TEST");
}
}

来自表单类:

  private String fileSelected;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.setFileSelected("Test");
}

public String getFileSelected() {
return fileSelected;
}

public void setFileSelected(String fileSelected) {
this.fileSelected = fileSelected;
}

我最终找到了不同的解决方案。如果它可以帮助其他有类似困难的人,请将其发布在这里:

我突然意识到,通过将调用类注册为对话框类的监听器,我可以监听来自调用类的按钮事件。我几乎遵循了这个例子:Create a custom event in Java

最佳答案

Java 教程有 section specifically devoted to this 。我建议检查一下。

将其与 Getting User Input 结合起来部分,您就得到了您想要的东西。

编辑

这是一个稍微修改教程的示例:

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

public class Temp extends Box{
JFrame frame;
JTextArea text;

public Temp(JFrame frame){
super(BoxLayout.Y_AXIS);
this.frame = frame;
text = new JTextArea("Clickity Clack, down't the track.\nspam");
add(text);
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
launchDialog();
}
});
add(button);
}

public void launchDialog(){
//What you want the find button to do
JButton findButton = new JButton("Find");
findButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int start = text.getText().indexOf("spam");
int end = start + "spam".length();
if(start != -1){
text.requestFocus();
text.select(start, end);
}
}
});

//Cancel button hides the dialog
JButton cancelButton = new JButton("Cancel");

// Create the options displayed in the dialog
final JOptionPane optionPane = new JOptionPane(
"Find \"spam\"?\n"
+ "Do you understand?",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION, null, new Object[]{findButton, cancelButton});

// Build the dialog window
final JDialog dialog = new JDialog(frame,
"Click a button",
false);

//Add action to close button
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});

//Finish up and make it visible
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.setLocation(100, 100);
dialog.pack();
dialog.setVisible(true);

}


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

@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Temp(frame));
frame.pack();
frame.setVisible(true);
}});
}

}

关于java - 从对话框中获取值(value)而不关闭它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977740/

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