📄 userdialog.java
字号:
//UserDialog.java7package com.cy;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class UserDialog extends JDialog { JPanel panel1 = new JPanel(); JButton btNext = new JButton(); JButton btOK = new JButton(); JButton btCancel = new JButton(); JLabel lbName = new JLabel(); JLabel lbIDCard = new JLabel(); JLabel lbSex = new JLabel(); JLabel lbAge = new JLabel(); JLabel lbPhone = new JLabel(); JTextField tfName = new JTextField(); JTextField tfIDCard = new JTextField(); JLabel lbSN = new JLabel(); JTextField tfAge = new JTextField(); JTextField tfPhone = new JTextField(); JComboBox cmbSex = new JComboBox(UserInfo.SEX_INFO); JTextField tfSN = new JTextField(); UserInfo userInfo = null; boolean isNewUser = true; int recordIndex=-1; GridBagLayout gridBagLayout1 = new GridBagLayout(); public UserDialog(Frame frame, String title, boolean modal, UserInfo info,boolean isNewUser) { super(frame, title, modal); userInfo = info; this.isNewUser=isNewUser; try { jbInit(); pack(); } catch(Exception ex) { ex.printStackTrace(); } } public UserDialog(Frame frame, String title, boolean modal, UserInfo info,int recordIndex) { this(frame,title,modal,info,false); this.recordIndex = recordIndex; } void jbInit() throws Exception { panel1.setLayout(gridBagLayout1); btNext.setText("Next"); btNext.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btNext_actionPerformed(e); } }); btOK.setText("OK"); btOK.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btOK_actionPerformed(e); } }); btCancel.setText("Cancel"); btCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btCancel_actionPerformed(e); } }); lbName.setHorizontalAlignment(SwingConstants.RIGHT); lbName.setText("Name:"); lbIDCard.setHorizontalAlignment(SwingConstants.RIGHT); lbIDCard.setText("ID Card:"); lbSex.setHorizontalAlignment(SwingConstants.RIGHT); lbSex.setText("Sex:"); lbAge.setHorizontalAlignment(SwingConstants.RIGHT); lbAge.setText("Age:"); lbPhone.setHorizontalAlignment(SwingConstants.RIGHT); lbPhone.setText("Phone:"); lbSN.setHorizontalAlignment(SwingConstants.RIGHT); lbSN.setText("SN:"); tfSN.setEditable(false); panel1.setMinimumSize(new Dimension(300, 280)); panel1.setPreferredSize(new Dimension(300, 280)); this.setResizable(false); this.getContentPane().add(panel1, BorderLayout.CENTER); panel1.add(lbIDCard, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(20, 40, 0, 11), 3, 0)); panel1.add(tfPhone, new GridBagConstraints(1, 5, 2, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(13, 30, 0, 34), 148, 0)); panel1.add(lbSN, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 40, 0, 11), 28, 0)); panel1.add(tfIDCard, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(17, 30, 0, 34), 148, 0)); panel1.add(lbPhone, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(17, 40, 0, 11), 9, 0)); panel1.add(lbAge, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(17, 40, 0, 11), 24, 0)); panel1.add(lbSex, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(20, 40, 0, 11), 25, 0)); panel1.add(lbName, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(15, 40, 0, 11), 11, 1)); panel1.add(cmbSex, new GridBagConstraints(1, 3, 2, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(17, 30, 0, 34), 82, 0)); panel1.add(tfAge, new GridBagConstraints(1, 4, 2, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(14, 30, 0, 34), 141, 0)); panel1.add(tfName, new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(13, 30, 0, 34), 148, 0)); panel1.add(tfSN, new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 30, 0, 34), 134, 0)); panel1.add(btNext, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(17, 25, 12, 15), 0, 0)); panel1.add(btOK, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(16, 1, 13, 22), 0, 0)); panel1.add(btCancel, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(16, -3, 13, 43), 32, 0)); initContent(); addWindowListener(new WindowAdapter(){ public void windowOpened(WindowEvent e){ tfName.requestFocus(); } }); ActionListener exitListener = new ActionListener() { public void actionPerformed(ActionEvent ae) { dispose(); } }; getRootPane().registerKeyboardAction(exitListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false), JComponent.WHEN_IN_FOCUSED_WINDOW ); } void btNext_actionPerformed(ActionEvent e) { if (isFieldsFilledOK()==false){ return; } saveGUIInfo(); updateParentGUI(); AppState.currentSN++; initContent(); } public void initContent(){ if (isNewUser==true) { userInfo = new UserInfo(AppState.getCurrentSN()); btNext.setEnabled(true); getRootPane().setDefaultButton(btNext); }else{ getRootPane().setDefaultButton(btOK); btNext.setEnabled(false); } updateGUI(); } public void updateGUI(){ tfSN.setText(userInfo.getSn()+1+""); //note it is a fake increasing, when current information is OK, then OK. tfName.setText(userInfo.getName()); tfAge.setText(userInfo.getAge()+""); tfIDCard.setText(userInfo.getIdCard()); cmbSex.setSelectedIndex(userInfo.getSex()); tfPhone.setText(userInfo.getPhone()); } void btOK_actionPerformed(ActionEvent e) { if (isFieldsFilledOK()==false){ return; } saveGUIInfo(); updateParentGUI(); dispose(); } private boolean isFieldsFilledOK(){ if (tfName.getText().equals("")){ JOptionPane.showMessageDialog(this,"User name can not be empty !","Error", JOptionPane.WARNING_MESSAGE); tfName.requestFocus(); return false; } if (tfIDCard.getText().equals("")&&tfAge.getText().equals("0")&&tfPhone.getText().equals("")){ JOptionPane.showMessageDialog(this,"Without ID card number, you need fill all other fields!","Error", JOptionPane.WARNING_MESSAGE); tfIDCard.requestFocus(); return false; } return true; } private void saveGUIInfo(){ userInfo.setName(tfName.getText()); userInfo.setPhone(tfPhone.getText()); userInfo.setAge(Integer.parseInt(tfAge.getText())); userInfo.setSex(cmbSex.getSelectedIndex()); userInfo.setIdCard(tfIDCard.getText()); if (isNewUser==true){ AppState.addUser(userInfo); }else{ AppState.updateUser(userInfo,recordIndex); } } private void updateParentGUI(){ AppState.getMainFrame().userTable.revalidate(); AppState.getMainFrame().userTable.repaint(); } /** * need set the index */ public void setRecordIndex(int index){ recordIndex=index; } void btCancel_actionPerformed(ActionEvent e) { this.dispose(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -