JAVA的addComponent_Java JDialog.addComponentListener方法代码示例

import javax.swing.JDialog; //导入方法依赖的package包/类

/**

* Ask using a GUI for the username and password.

*/

private void readFromGUI() {

// Create fields for user name.

final JTextField usernameField = new JTextField(20);

usernameField.setText(this.username);

final JLabel usernameLabel = new JLabel("Username: ");

usernameLabel.setLabelFor(usernameField);

final JPanel usernamePane = new JPanel(new FlowLayout(FlowLayout.TRAILING));

usernamePane.add(usernameLabel);

usernamePane.add(usernameField);

// Create fields for password.

final JPasswordField passwordField = new JPasswordField(20);

passwordField.setText(this.password);

final JLabel passwordLabel = new JLabel("Password: ");

passwordLabel.setLabelFor(passwordField);

final JPanel passwordPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));

passwordPane.add(passwordLabel);

passwordPane.add(passwordField);

// Create panel

final JPanel main = new JPanel();

main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));

main.add(usernamePane);

main.add(passwordPane);

// Create and handle dialog

final JOptionPane jop = new JOptionPane(main, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);

final JDialog dialog = jop.createDialog("User name and password");

dialog.addComponentListener(new ComponentAdapter() {

public void componentShown(ComponentEvent e) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

if (usernameField.getText().isEmpty())

{

usernameField.requestFocusInWindow();

}

else

{

passwordField.requestFocusInWindow();

}

}

});

}

});

dialog.setVisible(true);

final Integer result = (Integer) jop.getValue();

dialog.dispose();

if (result.intValue() == JOptionPane.OK_OPTION) {

this.username = usernameField.getText();

final char[] pwd = passwordField.getPassword();

this.password = new String(pwd);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
改进以下代码,让错误出现三次弹出冻结信息并关闭窗体。denglu.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int count = 0; String inputzh = zhanghu.getText(); boolean bl = true; try { new Mysql(); bl = Mysql.jujle(inputzh); } catch (ClassNotFoundException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } catch (SQLException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } System.out.println(bl); if (bl) { try { System.out.println(inputzh); new GUI2(inputzh); jf.dispose(); } catch (Exception e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } } else { ImageIcon titleIcon = new ImageIcon("F:\\ATM.png"); JDialog jDialog = new JDialog(jf, "错误", true); jDialog.setSize(400, 200); jDialog.setLocation(575, 400); JTextField jTextField = new JTextField("您的账号和密码输入有误!请重新输入。"); jTextField.setHorizontalAlignment(JTextField.CENTER); jTextField.setEditable(false); jTextField.setSelectedTextColor(Color.black); jDialog.add(jTextField); jDialog.setIconImage(titleIcon.getImage()); jTextField.setVisible(true); jDialog.setVisible(true); count++; if (count == 3) { ImageIcon titleIcon1 = new ImageIcon("F:\\ATM.png"); JDialog jDialog1 = new JDialog(jf, "错误", true); jDialog.setSize(400, 200); jDialog.setLocation(575, 400); JTextField jTextField1 = new JTextField("您的账号已冻结!请联系人工处理。"); jTextField.setHorizontalAlignment(JTextField.CENTER); jTextField.setEditable(false); jTextField.setSelectedTextColor(Color.black); jDialog.add(jTextField1); jDialog.setIconImage(titleIcon1.getImage()); jTextField.setVisible(true); jDialog.setVisible(true); jf.setDefaultCloseOperation(3); System.exit(0); } else { jpl.repaint(); } } } });
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值