gpt4 book ai didi

Java 对话框 - 查看是否单击了“确定”?

转载 作者:搜寻专家 更新时间:2023-10-31 08:08:32 25 4
gpt4 key购买 nike

我有一个客户端 GUI 对话框,要求提供要连接的服务器的 IP 和端口。我拥有其他所有东西,但我该如何做到这一点,以便当用户在我的对话框上单击“确定”时,它会运行一些东西?这是我到目前为止所拥有的:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class ClientDialog {
JTextField ip = new JTextField(20);
JTextField port = new JTextField(20);
GUI gui = new GUI();
Client client = new Client();
JOptionPane optionPane;

public void CreateDialog(){

Object msg[] = {"IP: ", ip, "\nPort: ", port};

optionPane = new JOptionPane();
optionPane.setMessage(msg);
optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = optionPane.createDialog(null, "Connect to a server");
dialog.setVisible(true);

if(dialog == JOptionPane.OK_OPTION){
System.out.println(ip);

String ipMsg = ip.getText();
int portMsg = Integer.parseInt(port.getText());

gui.CreateConsole(client, ipMsg, portMsg);
}

}

} //End class

我知道代码不正确,但我想要的是当用户在对话框中点击“确定”时,我可以运行一些代码。谢谢!

最佳答案

我建议改用showConfirmDialog

int result = JOptionPane.showConfirmDialog(myParent, "Narrative", 
"Title", JOptionPane.INFORMATION_MESSAGE);

在那里你可以测试来自JDialog/JOptionPane的各种返回值

if (result == JOptionPane.OK_OPTION, 
JOptionPane.CANCEL_OPTION,
JOptionPane.CLOSED_OPTION, etc..

关于Java 对话框 - 查看是否单击了“确定”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8582743/

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