📄 note.java
字号:
package com.zsf.manager;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.sql.SQLException;
import java.util.*;
public class Note extends JFrame{
private static final long serialVersionUID = 1L;
JMenuBar menubar=new JMenuBar();
JMenu menu1=new JMenu("File");
JMenu menu2=new JMenu("Contacts");
JMenuItem menuitem_save=new JMenuItem("Save");
JMenuItem menuitem_exit=new JMenuItem("Exit");
JMenuItem menuitem_add=new JMenuItem("Add");
JMenuItem menuitem_delete=new JMenuItem("Delete");
DefaultTableModel tablemodel=new DefaultTableModel();
JTable table=new JTable(tablemodel);
JScrollPane scroll=new JScrollPane(table);
JComboBox combobox=new JComboBox();
JButton button1=new JButton("Save");
JButton button2=new JButton("Refresh");
JButton button3=new JButton("Exit");
public Note()
{
this.getContentPane().setLayout(null);
menu1.add(menuitem_save);
menu1.add(menuitem_exit);
menu2.add(menuitem_add);
menu2.add(menuitem_delete);
menubar.add(menu1);
menubar.add(menu2);
combobox.addItem("All");
combobox.addItem("Family");
combobox.addItem("Friends");
combobox.addItem("Music");
combobox.addItem("Work");
tablemodel.addColumn("ID");
tablemodel.addColumn("FirstName");
tablemodel.addColumn("LastName");
tablemodel.addColumn("Telephone");
this.getInformation();
combobox.setBounds(490, 20, 80, 20);
scroll.setBounds(80, 50, 420, 300);
button1.setBounds(100, 400, 80, 30);
button2.setBounds(250, 400, 80, 30);
button3.setBounds(400, 400, 80, 30);
this.setJMenuBar(menubar);
this.getContentPane().add(combobox);
this.getContentPane().add(scroll);
this.getContentPane().add(button1);
this.getContentPane().add(button2);
this.getContentPane().add(button3);
this.setSize(600, 500);
this.setVisible(true);
this.eventRun();
}
public void eventRun()
{
new ButtonHandle(button1,this);
new ButtonHandle(button2,this);
new ButtonHandle(button3,this);
new MenuItemHandle(menuitem_save,this);
new MenuItemHandle(menuitem_exit,this);
new MenuItemHandle(menuitem_add,this);
new MenuItemHandle(menuitem_delete,this);
new ComboBoxHandle(combobox,this);
}
public void getInformation()
{
PostDB postdb=new PostDB(new MyDB());
int i=0;
try {
tablemodel.setRowCount(postdb.getCountOfRecords());
Iterator itr=postdb.getAllPosts();
while(itr.hasNext())
{
Post post=(Post)itr.next();
table.setValueAt(post.getid(), i, 0);
table.setValueAt(post.getfirstname(), i, 1);
table.setValueAt(post.getlastname(), i, 2);
table.setValueAt(post.gettelephone(), i, 3);
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
public void selectRecords(String role){
try{
PostDB postdb=new PostDB(new MyDB());
Iterator itr=postdb.selectPost(role);
int counter=0;
while(itr.hasNext()){
counter++;
}
tablemodel.setRowCount(counter);
int i=0;
while(itr.hasNext()){
Post post=(Post)itr.next();
table.setValueAt(post.getid(), i, 0);
table.setValueAt(post.getfirstname(), i, 1);
table.setValueAt(post.getlastname(), i, 2);
table.setValueAt(post.gettelephone(), i, 3);
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
public void deleteRecords()
{
int row=table.getSelectedRow();
int column=0;
if(row==-1){
JOptionPane.showMessageDialog(this, "No record you choose!","List",JOptionPane.ERROR_MESSAGE);
}else{
int temp=Integer.parseInt(table.getValueAt(row, column).toString());
try{
PostDB postdb=new PostDB(new MyDB());
Iterator itr=postdb.getAllPosts();
while(itr.hasNext()){
Post post=(Post)itr.next();
if(temp==Integer.parseInt(post.getid())){
postdb.deletePost(post);
//自动刷新
this.getInformation();
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
public void updateRecords(){
int rows=table.getRowCount();
for(int i=0;i<rows;i++){
int ID=Integer.parseInt(table.getValueAt(i, 0).toString());
String FirstName=table.getValueAt(i, 1).toString();
String LastName=table.getValueAt(i, 2).toString();
String Telephone=table.getValueAt(i, 3).toString();
try{
PostDB postdb=new PostDB(new MyDB());
Iterator itr=postdb.getAllPosts();
while(itr.hasNext()){
Post post=(Post)itr.next();
if(ID==Integer.parseInt(post.getid())){
post.setfirstname(FirstName);
post.setlastname(LastName);
post.settelephone(Telephone);
postdb.updatePost(post);
//自动刷新
this.getInformation();
}
}
}catch(SQLException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
new Note().setTitle("The Note Of Telephone");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -