📄 personmanager.java
字号:
nameField.setEditable(e);
addressField.setEditable(e);
dateField.setEditable(e);
resumeArea.setEditable(e);
maleButton.setEnabled(e);
femaleButton.setEnabled(e);
}
// 利用 initRecord 中的信息设置各控件的初始值,如果 initRecord 是 null,则用空值初始化
private void setDefaultValue(PersonRecord initRecord) {
if (initRecord != null) {
idField.setText(initRecord.id);
nameField.setText(initRecord.name);
addressField.setText(initRecord.address);
dateField.setText(initRecord.birthday.toString());
resumeArea.setText(initRecord.resume);
maleButton.setSelected(false);
femaleButton.setSelected(false);
if (initRecord.sex.equals("男")) maleButton.setSelected(true);
else femaleButton.setSelected(true);
} else {
idField.setText("");
nameField.setText("");
addressField.setText("");
dateField.setText("");
resumeArea.setText("");
maleButton.setSelected(true);
femaleButton.setSelected(false);
}
}
// 创建输入人员信息的各种控件,并放在方法所返回的窗格中
private JPanel createInfoPanel() {
JPanel basicPanel = new JPanel();
// 为了使得界面比较美观,这里使用了网格包布局管理器
GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.ipadx = 10;
c.weighty = 0.3; c.weightx = 0.3;
c.insets = new Insets(5, 0, 5, 0);
basicPanel.setLayout(layout);
basicPanel.setBorder(BorderFactory.createTitledBorder("基本信息"));
// 创建输入人员代码的提示标签及用户输入的文本字段
JLabel promptLabel = new JLabel("代码(I)", JLabel.LEFT);
promptLabel.setDisplayedMnemonic('I');
idField = new JTextField(20);
promptLabel.setLabelFor(idField);
c.gridx = 0; c.gridy = 0;
c.gridwidth = 1; c.gridheight = 1;
c.anchor = GridBagConstraints.EAST;
layout.setConstraints(promptLabel, c);
basicPanel.add(promptLabel);
c.gridx = 1; c.gridy = 0;
c.gridwidth = 2; c.gridheight = 1;
c.anchor = GridBagConstraints.WEST;
layout.setConstraints(idField, c);
basicPanel.add(idField);
// 创建输入人员姓名的提示标签及用户输入的文本字段
promptLabel = new JLabel("姓名(M)", JLabel.LEFT);
promptLabel.setDisplayedMnemonic('M');
nameField = new JTextField(20);
promptLabel.setLabelFor(nameField);
c.gridx = 3; c.gridy = 0;
c.gridwidth = 1; c.gridheight = 1;
c.anchor = GridBagConstraints.EAST;
layout.setConstraints(promptLabel, c);
basicPanel.add(promptLabel);
c.gridx = 4; c.gridy = 0;
c.gridwidth = 2; c.gridheight = 1;
c.anchor = GridBagConstraints.WEST;
layout.setConstraints(nameField, c);
basicPanel.add(nameField);
// 创建输入人员性别的提示标签及选择用的广播按钮
promptLabel = new JLabel("性别(S)", JLabel.LEFT);
promptLabel.setDisplayedMnemonic('S');
ButtonGroup group = new ButtonGroup();
maleButton = new JRadioButton("男");
maleButton.setSelected(true);
group.add(maleButton);
femaleButton = new JRadioButton("女");
group.add(femaleButton);
c.gridx = 0; c.gridy = 1;
c.gridwidth = 1; c.gridheight = 1;
c.anchor = GridBagConstraints.EAST;
layout.setConstraints(promptLabel, c);
basicPanel.add(promptLabel);
c.gridx = 1; c.gridy = 1;
c.gridwidth = 1; c.gridheight = 1;
c.anchor = GridBagConstraints.WEST;
layout.setConstraints(maleButton, c);
basicPanel.add(maleButton);
c.gridx = 2; c.gridy = 1;
c.gridwidth = 1; c.gridheight = 1;
layout.setConstraints(femaleButton, c);
basicPanel.add(femaleButton);
// 创建输入出生日期的提示标签及用户输入的文本字段,如果使用更复杂的界面,则可编写一个
// 日期选择器 DateChooser
promptLabel = new JLabel("出生日期(D)", JLabel.LEFT);
promptLabel.setDisplayedMnemonic('D');
dateField = new JTextField(20);
promptLabel.setLabelFor(dateField);
c.gridx = 3; c.gridy = 1;
c.gridwidth = 1; c.gridheight = 1;
c.anchor = GridBagConstraints.EAST;
layout.setConstraints(promptLabel, c);
basicPanel.add(promptLabel);
c.gridx = 4; c.gridy = 1;
c.gridwidth = 2; c.gridheight = 1;
c.anchor = GridBagConstraints.WEST;
layout.setConstraints(dateField, c);
basicPanel.add(dateField);
// 创建输入地址信息的提示标签及用户输入的文本字段
promptLabel = new JLabel("地址(A)", JLabel.LEFT);
promptLabel.setDisplayedMnemonic('A');
addressField = new JTextField(60);
promptLabel.setLabelFor(addressField);
c.gridx = 0; c.gridy = 3;
c.gridwidth = 1; c.gridheight = 1;
c.anchor = GridBagConstraints.EAST;
layout.setConstraints(promptLabel, c);
basicPanel.add(promptLabel);
c.gridx = 1; c.gridy = 3;
c.gridwidth = 5; c.gridheight = 1;
c.anchor = GridBagConstraints.WEST;
layout.setConstraints(addressField, c);
basicPanel.add(addressField);
// 创建用于输入个人简历的窗格
JPanel resumePanel = new JPanel();
resumePanel.setLayout(new BoxLayout(resumePanel, BoxLayout.Y_AXIS));
// 设置该窗格使用的带标题的边框
resumePanel.setBorder(BorderFactory.createTitledBorder("请输入你的简历"));
// 创建用于输入个人简历的文本字段,缺省为3行40列
resumeArea = new JTextArea(3, 40);
resumeArea.setLineWrap(true); // 设置在输入时自动折行
// 将文本区域放置在一个滚动窗格中,以支持输入多行文本
JScrollPane scroll = new JScrollPane(resumeArea);
resumePanel.add(scroll);
// 将基本信息窗格和输入简历的窗格都加入到待返回的窗格中
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
infoPanel.add(basicPanel, BorderLayout.CENTER);
infoPanel.add(resumePanel, BorderLayout.SOUTH);
return infoPanel;
}
// 创建用于输入人员信息的各种组件。
private void create() {
// 获取当前对话框内容面板,所以的控件只能放在内容面板中
place = getContentPane();
place.setLayout(new BoxLayout(place, BoxLayout.Y_AXIS));
// 加入放置了各种输入控件的窗格
place.add(createInfoPanel());
// 创建一事件监听器,该监听器监听下述两个按钮上发生的事件
SimpleListener listener = new SimpleListener();
// 创建用于确定输入信息的“确定”按钮
JButton okButton = new JButton("确定(Y)");
okButton.setMnemonic('Y'); // 设置快捷键
// 设置动作命令的名称,这可用来区分用户到底按了什么按钮
okButton.setActionCommand("ok");
okButton.addActionListener(listener); // 添加事件监听器
// 创建用于放弃输入信息的“放弃”按钮
JButton cancelButton = new JButton("放弃(N)");
cancelButton.setMnemonic('N'); // 设置快捷键
// 设置动作命令的名称,这可用来区分用户到底按了什么按钮
cancelButton.setActionCommand("cancel");
cancelButton.addActionListener(listener); // 添加事件监听器
// 创建一临时性窗格,将这两个按钮加入到该窗格,该窗格使用流式布局管理
JPanel tempPanel = new JPanel();
tempPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 0));
tempPanel.add(okButton);
tempPanel.add(cancelButton);
// 将该临时性窗格加入到基础容器
place.add(tempPanel);
}
private class SimpleListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("ok")) closeAndSave();
else closeAndCancel();
}
}
// 用户按确认之后推出对话框,这时要保存所输入的信息到 record 中
private void closeAndSave() {
// 按确认之后,输入的信息是有效的,将它们保存到 record 中
isValidate = true;
record.id = idField.getText();
record.name = nameField.getText();
record.address = addressField.getText();
record.resume = resumeArea.getText();
record.sex = maleButton.isSelected() ? "男" : "女";
String dateString = dateField.getText();
try {
record.birthday = Date.valueOf(dateString);
// 使对话框不可见,就关闭了对话框并退出对话框
setVisible(false);
} catch (Exception exc) {
// 如果输入的日期串不能转换为正确的日期对象,则提示出错,并且让用户重新输入
// 注意,没有让对话框不可见,就意味着用户可以重新使用对话框进行输入
JOptionPane.showMessageDialog(this, "输入的出生日期[" + dateString + "]不符合预定格式[yyyy-mm-dd]!");
exc.printStackTrace();
}
}
private void closeAndCancel() {
// 直接关闭对话框,或按放弃退出对话框,这时直接使对话框不可见即可
isValidate = false;
setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -