【发布时间】:2012-03-31 02:02:27
【问题描述】:
当单击 JOptionPane 中的按钮时,我需要编写一些代码来处理事件。我实际上是在使用 JOptionPane 插入用户名和密码,如果单击正确和确定按钮,则 JOptionPane 消失并且父 JFrame 保持活动状态,如果单击取消按钮,程序退出。我下面的示例代码仅显示了 JOptionpane 并单击任何按钮将其关闭 `JPanel 面板 = 新的 JPanel(); panel.setLayout(new GridLayout(4, 1)); //创建一个带有文本的标签(用户名) JLabel 用户名 = new JLabel("用户名");
//Create a label with text (Password)
JLabel password = new JLabel("Password");
//Create text field that will use to enter username
JTextField textField = new JTextField(12);
//Create password field that will be use to enter password
JPasswordField passwordField = new JPasswordField(12);
//Add label with text (username) into created panel
panel.add(username);
//Add text field into created panel
panel.add(textField);
//Add label with text (password) into created panel
panel.add(password);
//Add password field into created panel
panel.add(passwordField);
//Show JOptionPane that will ask user for username and password
JOptionPane.showConfirmDialog(mainFrame, panel, "Enter username and password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);`
【问题讨论】:
标签: java swing jdialog joptionpane