Java对话框 - 查看是否单击了确定? [英] Java Dialog - Find out if OK is clicked?
本文介绍了Java对话框 - 查看是否单击了确定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个客户端GUI对话框,询问要连接的服务器的IP和端口。我还有其他所有内容,但是如何在用户点击对话框中的确定时进行操作呢?这是我到目前为止所拥有的:
I have a dialog for a client-GUI that asks for the IP and Port of the server one wants to connect to. I have everything else, but how would I make it so that when the user clicks "OK" on my dialog box, that it runs something? Here's what I have so far:
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
我知道代码不正确,但我想要的是当用户在对话框上点击OK时,我可以运行一些代码。谢谢!
I know that the code isn't correct, but what I want is that when the user hits "OK" on the dialog, I can run some code. Thanks!
推荐答案
我建议使用 showConfirmDialog
代替
int result = JOptionPane.showConfirmDialog(myParent, "Narrative",
"Title", JOptionPane.INFORMATION_MESSAGE);
在那里你可以测试来自 JDialog / JOptionPane的各种回报值
and there you can test for various returns value from JDialog/JOptionPane
if (result == JOptionPane.OK_OPTION,
JOptionPane.CANCEL_OPTION,
JOptionPane.CLOSED_OPTION, etc..
这篇关于Java对话框 - 查看是否单击了确定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文