1.主页面
package phoneBookGUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class GUI extends JFrame { //总页面
JButton addButton = new ColorButton("添加",new Color(230,230,255)); //五个功能按钮
JButton deleteButton = new ColorButton("删除",new Color(240,255,255));
JButton setButton = new ColorButton("修改",new Color(250,235,215));
JButton findButton = new ColorButton("查找",new Color(255,250,205));
JButton backButton = new ColorButton("复原",new Color(135,206,250));
private static TableModel model = null; //表格控件
private static GUITable table = null;
public GUI(String title) {
super(title);
String[] titles = {"姓名","电话","性别"};
model = new TableModel(titles);
table = new GUITable(model);
this.setBounds(0, 0, 600, 600);
this.add(new PanelGUI(),BorderLayout.NORTH);
this.add(new PanelTable(table));
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setResizable(false);
addButton.addActionListener((e)->{add();});
deleteButton.addActionListener((e)->{delete();});
findButton.addActionListener((e)->{find();});
setButton.addActionListener((e)->{set();});
backButton.addActionListener((e)->{back();});
}
private class PanelGUI extends JPanel{ //总页面上层面板,放置五个按钮
public PanelGUI() {
this.setBounds(0, 0, 600, 50);
this.add(addButton);
this.add(deleteButton);
this.add(findButton);
this.add(setButton);
this.add(backButton);
}
}
private class PanelTable extends JPanel{ //总页面下层面板,放置表格
public PanelTable(JTable table) {
this.setBounds(0, 0, 800, 400);
this.add(table);
this.add(new JScrollPane(table),BorderLayout.CENTER);
}
}
private void add() { //添加联系人
Add add = new Add(this);
add.setTitle("添加联系人");
add.setModal(true);
add.setVisible(true);
boolean flag = true;
if(add.getAccepted()) {
for(int i = 0; i < table.getRowCount(); i++) {
if(table.getValueAt(i, 1).equals(add.getValue().getPhoneNo())) {
flag = false;
JOptionPane.showMessageDialog(null, "该号码不能重复保存", "错误",JOptionPane.ERROR_MESSAGE);
}
}
if(flag == true)
{
model.addRow(add.getValue());
}
}
}
private void delete() { //删除联系人
int[] rows = table.getSelectedRows();
for(int i = rows.length - 1; i >= 0; i--)
{
int index = rows[i];
model.removeRow(index);
}
}
private void find() {
Find find = new Find(this);
find.setModal(true);
find.set