📄 mainwindow.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Vector;
import javax.swing.*;
public class MainWindow extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
BusinessObject bo;
JTextField txtNum; //编号
JTextField txtName;//姓名
JComboBox comboCategory;//类别
Object categ[] ={"请选择类别","朋友","同事","家人"};
JRadioButton rBtnMale;//男
JRadioButton rBtnFemale;//女
JTextField txtTel;//联系方式
JTextField txtPhotoAddr;//照片路径
JLabel lblPhotoShow;//照片
JButton btnFirst;//第一条
JButton btnPrevious;//上一条
JButton btnNext;//下一条
JButton btnLast;//最后一条
JButton btnAdd ;//增加
JButton btnSave;//保存
JButton btnExit ;//退出
JButton btnDelete ;//删除
JButton btnQuery;//查询
JLabel lblSaved;//提示已保存的标签
JButton btnPhoto;//浏览图片的按钮
CardLayout card;
JPanel main;
JPasswordField passwordField;
String password = "1234";
public MainWindow(){
this.setTitle("个人通讯录");
this.setSize(349, 380);
this.setLocationRelativeTo(null);//窗口居中
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bo = new BusinessObject();
//内容面板
JPanel p = new JPanel();
main = new JPanel();
card = new CardLayout();
main.setLayout(card);
p.setLayout(null);
JLabel lblNum = new JLabel("用户编号:");
lblNum.setBounds(20, 20, 60, 25);
p.add(lblNum);
txtNum = new JTextField();
txtNum.setBounds(90, 20, 100, 25);
txtNum.setEditable(false);
p.add(txtNum);
JLabel lblName = new JLabel("姓 名:");
lblName.setBounds(20, 55, 60, 25);
p.add(lblName);
txtName = new JTextField();
txtName.setBounds(90, 55, 100, 25);
p.add(txtName);
JLabel lblCategory = new JLabel("类 别:");
lblCategory.setBounds(20, 90, 60, 25);
p.add(lblCategory);
comboCategory = new JComboBox(categ);
comboCategory.setBounds(90, 90, 100, 25);
p.add(comboCategory);
JLabel lblSex = new JLabel("性 别:");
lblSex.setBounds(20, 125, 60, 25);
p.add(lblSex);
rBtnMale = new JRadioButton("男");
rBtnMale.setBounds(90, 125, 50, 25);
rBtnMale.setSelected(true);
p.add(rBtnMale);
rBtnFemale = new JRadioButton("女");
rBtnFemale.setBounds(150, 125, 50, 25);
p.add(rBtnFemale);
ButtonGroup btnGroup = new ButtonGroup();
btnGroup.add(rBtnMale);
btnGroup.add(rBtnFemale);
JLabel lblTel = new JLabel("联系方式:");
lblTel.setBounds(20, 160, 60, 25);
p.add(lblTel);
txtTel = new JTextField();
txtTel.setBounds(90, 160, 150, 25);
p.add(txtTel);
JLabel lblPhoto = new JLabel("照片路径:");
lblPhoto.setBounds(20, 195, 60, 25);
p.add(lblPhoto);
txtPhotoAddr = new JTextField();
txtPhotoAddr.setBounds(90, 195, 150, 25);
p.add(txtPhotoAddr);
lblPhotoShow = new JLabel(" 照片");
lblPhotoShow.setBounds(233, 20, 100, 120);
lblPhotoShow.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p.add(lblPhotoShow);
//浏览按钮面板
JPanel panelScan = new JPanel();
panelScan.setBounds(5, 225, 330, 60);
panelScan.setBorder(BorderFactory.createTitledBorder(" 浏览"));
p.add(panelScan);
//操作按钮面板
JPanel panelOperation = new JPanel();
panelOperation.setBounds(5, 285, 330, 60);
panelOperation.setBorder(BorderFactory.createTitledBorder(" 操作"));
p.add(panelOperation);
btnFirst = new JButton();
btnFirst.setIcon(new ImageIcon("config/first.png"));
btnFirst.setToolTipText("第一条");
btnPrevious = new JButton();
btnPrevious.setIcon(new ImageIcon("config/up.png"));
btnPrevious.setToolTipText("上一条");
btnNext = new JButton();
btnNext.setIcon(new ImageIcon("config/down.png"));
btnNext.setToolTipText("下一条");
btnLast = new JButton();
btnLast.setIcon(new ImageIcon("config/last.png"));
btnLast.setToolTipText("最后一条");
panelScan.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
panelScan.add(btnFirst);
panelScan.add(btnPrevious);
panelScan.add(btnNext);
panelScan.add(btnLast);
btnAdd = new JButton();
btnAdd.setMnemonic('N'); //快捷键
btnAdd.setToolTipText("增加记录(N)");//鼠标移动显示
btnAdd.setIcon(new ImageIcon("config/new.png"));
btnSave = new JButton();
btnSave.setMnemonic('S');
btnSave.setToolTipText("保存记录(S)");
btnSave.setIcon(new ImageIcon("config/save.gif"));
btnQuery = new JButton();
btnQuery.setMnemonic('Q');
btnQuery.setToolTipText("查询记录(Q)");
btnQuery.setIcon(new ImageIcon("config/query.png"));
btnExit = new JButton();
btnExit.setMnemonic('E');
btnExit.setToolTipText("退出(E)");
btnExit.setIcon(new ImageIcon("config/exit.png"));
btnExit.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent e) {
System.exit(0);
}
});
btnDelete = new JButton();
btnDelete.setMnemonic('D');
btnDelete.setToolTipText("删除记录(D)");
btnDelete.setIcon(new ImageIcon("config/delete.png"));
panelOperation.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
panelOperation.add(btnAdd);
panelOperation.add(btnSave);
panelOperation.add(btnDelete);
panelOperation.add(btnQuery);
panelOperation.add(btnExit);
/*
* 事件监听
*/
btnFirst.addActionListener(this);
btnPrevious.addActionListener(this);
btnNext.addActionListener(this);
btnLast.addActionListener(this);
btnQuery.addActionListener(this);
btnAdd.addActionListener(this);
btnSave.addActionListener(this);
btnDelete.addActionListener(this);
/**
* 启动时显示第一条
*/
displayUserInfo(bo.first());
//保存成功标签
lblSaved = new JLabel();
lblSaved.setBounds(249, 163, 66, 18);
p.add(lblSaved);
btnPhoto = new JButton();
btnPhoto.setToolTipText("浏览");
btnPhoto.setIcon(new ImageIcon("config/browse.png"));
btnPhoto.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser("./config");
if(fileChooser.showOpenDialog(null) == 0){
File selectedFile = fileChooser.getSelectedFile();
lblPhotoShow.setIcon(new ImageIcon(selectedFile.getAbsolutePath()));
txtPhotoAddr.setText(selectedFile.getAbsolutePath());
}
}
});
btnPhoto.setBounds(246, 193, 74, 28);
p.add(btnPhoto);
main.add("welcome",welcomePane());
main.add("main",p);
this.getContentPane().add(main);
this.setVisible(true);
}
class innerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String pw = new String(passwordField.getPassword());
if(pw.equals(password))
MainWindow.this.card.show(MainWindow.this.main, "main");
else
JOptionPane.showMessageDialog(MainWindow.this, " 密码错误!");
}
}
private JPanel welcomePane()
{
JPanel pane = new JPanel();
JButton btnLogin = new JButton("进入");
pane.setLayout(null);
JLabel label2 = new JLabel("个人通讯录");
label2.setFont(new Font("华文行楷", Font.BOLD, 48));
label2.setBounds(45, 10, 280, 50);
pane.add(label2);
JLabel label = new JLabel(new ImageIcon("config/card_bg_13.jpg"));
label.setBounds(0, 55, 349, 230);
pane.add(label);
JLabel lbpswd = new JLabel("密码");
lbpswd.setBounds(80, 300, 30, 25);
pane.add(lbpswd);
passwordField = new JPasswordField(8);
passwordField.setBounds(115, 300, 70, 25);
pane.add(passwordField);
passwordField.setEchoChar('*');
innerListener l = new innerListener();
btnLogin.addActionListener(l);
btnLogin.setBounds(190, 300, 70, 25);
pane.add(btnLogin);
return pane;
}
public static void main(String[] args){
new MainWindow();
}
/**
* 显示用户信息
*/
public int displayUserInfo(User user){
txtNum.setText(""+user.getNo());
txtName.setText(user.getName());
comboCategory.setSelectedItem(user.getCategory());
if(user.getSex().equals("M")){
rBtnMale.setSelected(true);
}else if(user.getSex().equals("F")){
rBtnFemale.setSelected(true);
}
txtTel.setText(user.getPhone());
txtPhotoAddr.setText(user.getImagePath());
lblPhotoShow.setIcon(new ImageIcon(user.getImagePath()));
return bo.proutil.getUsers().indexOf(user);
}
/**
* 事件实现
*/
public void actionPerformed(ActionEvent e) {
lblSaved.setText("");
if(e.getSource() == btnFirst){
displayUserInfo(bo.first());
}else if(e.getSource() == btnPrevious){
displayUserInfo(bo.previous());
}else if(e.getSource() == btnNext){
displayUserInfo(bo.next());
}else if(e.getSource() == btnLast){
displayUserInfo(bo.last());
}else if(e.getSource() == btnAdd){
txtNum.setText(""+(bo.proutil.getUsers().lastElement().getNo()+1));
txtName.setText("");
comboCategory.setSelectedItem(categ[0]);
rBtnMale.setSelected(true);
txtTel.setText("");
txtPhotoAddr.setText("");
lblPhotoShow.setIcon(null);
}else if(e.getSource() == btnSave){
if(txtNum.getText() != null && !txtName.getText().equals("") && !txtTel.getText().equals("")){
User u = new User();
u.setNo(Integer.parseInt(txtNum.getText()));
u.setName(txtName.getText());
u.setCategory(comboCategory.getSelectedItem().toString());
if(rBtnMale.isSelected()){
u.setSex("M");
}else{
u.setSex("F");
}
u.setPhone(txtTel.getText());
u.setImagePath(txtPhotoAddr.getText());
bo.saveUser(u);
lblSaved.setText("保存成功");
}
else
JOptionPane.showMessageDialog(this," 请填入完整信息,不能为空!","错误",
JOptionPane.INFORMATION_MESSAGE);
}
else if(e.getSource() == btnDelete){
if(bo.currentPos == 0 && bo.proutil.getUsers().size() == 1)//只有一个用户时
{
User u = bo.proutil.getUsers().get(0);
u.setNo(1);
u.setName("输入用户名");
u.setCategory("请选择类别");
u.setSex("M");
u.setPhone("请输入电话号码");
u.setImagePath("输入图片路径");
bo.deleteUser(0);
displayUserInfo(u);
bo.saveUser(u);
if(bo.proutil.getUsers().get(0).getNo() == 1)
{
JOptionPane.showMessageDialog(this, " 记录已全部删除!");
}
return;
}
else if(bo.currentPos == 0 && bo.proutil.getUsers().size() > 1)//不止一个用户,删除第一个时
{
bo.deleteUser(0);
displayUserInfo(bo.proutil.getUsers().firstElement());
return;
}
bo.deleteUser(displayUserInfo(bo.previous())+1);
}
else if(e.getSource() == btnQuery)
{
String name = JOptionPane.showInputDialog(this,"请输入姓名");
Vector<User> users = bo.proutil.getUsers();
for(int i = 0; i < users.size(); i++)
{
User u = users.get(i);
if(u.getName().equals(name))
{
displayUserInfo(u);
return;
}
}
JOptionPane.showMessageDialog(this, "你所查询的用户不存在!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -