JTable中对person信息的操作

//老师代码
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;


public class MyTable extends JFrame{
	private static String[] titles;
	static {
		titles = new String[]{"姓名","性别","年龄","电话"};
	}
	
	private List<Person> persons;
	private JTable table;
	
	private TextField nameTf;
	private TextField genderTf;
	private TextField ageTf;
	private TextField phoneTf;
	
	private JButton submitBtn;
	private JButton resetBtn;
	private JButton deleteBtn;
	
	private Integer selectedIndex;
	
	public MyTable() {
		persons = new ArrayList<Person>();
		persons.add(new Person("illu","male",34,"232424"));
		persons.add(new Person("satan","female",34,"6767676"));
		this.setLayout(new GridLayout(1,2));
		table = new JTable();
		table.setModel(getModel());
		this.add(new JScrollPane(table));
		JPanel rightPanel = new JPanel();
		rightPanel.add(new JLabel("人员信息"), BorderLayout.NORTH);
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(4,1));
		JPanel rowPanel = new JPanel();
		
		rowPanel.add(new JLabel("姓名"));
		nameTf = new TextField(20);
		rowPanel.add(nameTf);
		centerPanel.add(rowPanel);
		
		rowPanel = new JPanel();
		rowPanel.add(new JLabel("性别"));
		genderTf = new TextField(20);
		rowPanel.add(genderTf);
		centerPanel.add(rowPanel);
		
		rowPanel = new JPanel();
		rowPanel.add(new JLabel("年龄"));
		ageTf = new TextField(20);
		rowPanel.add(ageTf);
		centerPanel.add(rowPanel);
		
		rowPanel = new JPanel();
		rowPanel.add(new JLabel("电话"));
		phoneTf = new TextField(20);
		rowPanel.add(phoneTf);
		centerPanel.add(rowPanel);
		
		rightPanel.add(centerPanel, BorderLayout.CENTER);
		
		JPanel btnPanel = new JPanel();
		submitBtn = new JButton("新增");
		btnPanel.add(submitBtn);
		
		resetBtn = new JButton("重置");
		btnPanel.add(resetBtn);
		
		deleteBtn = new JButton("删除");
		deleteBtn.setVisible(false);
		btnPanel.add(deleteBtn);
		
		rightPanel.add(btnPanel, BorderLayout.SOUTH);
		this.add(rightPanel);
		actionHandler();
		showMe();
		
	}
	
	private void actionHandler() {
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				selectedIndex = table.getSelectedRow();
				Person p = persons.get(selectedIndex);
				nameTf.setText(p.getName());
				genderTf.setText(p.getGender());
				ageTf.setText(p.getAge()+"");
				phoneTf.setText(p.getPhone());
				
				submitBtn.setText("修改");
				deleteBtn.setVisible(true);
			}
		});
		
		submitBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				System.out.println("submitBtn pressed");
				Person s = new Person();
				s.setName(nameTf.getText());
				s.setGender(genderTf.getText());
				s.setAge(Integer.parseInt(ageTf.getText()));
				s.setPhone(phoneTf.getText());
				if ("新增".equals(event.getActionCommand()))
					persons.add(s);
				else if ("修改".equals(event.getActionCommand()))
					persons.set(selectedIndex, s);
				table.setModel(getModel());
				reset();
			}
		});
		
		resetBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				reset();
				
			}
		});
		deleteBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				persons.remove(selectedIndex.intValue());
				table.setModel(getModel());
				reset();
			}
		});
	}
	
	private void showMe() {
		this.setSize(500,300);
		this.setLocation(400,300);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	private TableModel getModel() {
		String[][] data = new String[persons.size()][4];
		for (int i=0; i<persons.size();i++) {
			data[i][0] = persons.get(i).getName();
			data[i][1] = persons.get(i).getGender();
			data[i][2] = persons.get(i).getAge()+"";
			data[i][3] = persons.get(i).getPhone();
		}
		return new DefaultTableModel(data, titles);
	}
	
	private void reset() {
		nameTf.setText("");
		genderTf.setText("");
		ageTf.setText("");
		phoneTf.setText("");
		
		submitBtn.setText("新增");
		deleteBtn.setVisible(false);
		
		table.clearSelection();
		selectedIndex = null;
	}
	
	public static void main(String[] args) {
		new MyTable();
	}
}
///////////////////////

public class Person {
	private String name;
	private String gender;
	private int age;
	private String phone;
	
	public Person() {}

	public Person(String name, String gender, int age, String phone) {
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.phone = phone;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	
}

 

你可能感兴趣的:(java,swing)