点击户口信息后JTable的文字和JButton的按钮会消失,为什么,如何解决。
package FRAME;
import DB.DBOperation;
import DB.LinkDataBase;
import DB.PI;
import DB.RI;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
public class Test extends JFrame implements ActionListener {
int no = 0;
int rno = 0;
JMenuBar menubar;//菜单条
JMenu menu;//菜单,为JMenuItem的子类,便于嵌套子菜单
JMenuItem menuItem;//菜单项
Icon icon=new ImageIcon("a.gif");//菜单上图标
//成员数据
JMenuBar menuBar;
JMenu menuFruit;
JMenuItem pInformation,rInformation;
JMenu appleMenu;
JMenuItem aPInformation,aRInformation;
LinkedList<PI> pList = DBOperation.pQuery();
LinkedList<RI> rList = DBOperation.rQuery();
JPanel oP;
public Test(int pno) {
no = pno;
rno = pList.get(pno-1).getRno();
setTitle("农村人口管理系统--操作");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
setBounds(400, 150, 800, 500);
iniNMenu();
iniNOMenu();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void iniNOMenu() {
JLabel j11 = new JLabel("姓名");
j11.setBounds(100, 80, 50, 30);
j11.setLocation(50,350);
add(j11);
JTextField t11 = new JTextField();
t11.setBounds(150, 80, 100, 30);
t11.setLocation(100,350);
add(t11);
JLabel j12 = new JLabel("身份证");
j12.setBounds(100, 80, 50, 30);
j12.setLocation(250,350);
add(j12);
JTextField t12 = new JTextField();
t12.setBounds(150, 80, 100, 30);
t12.setLocation(300,350);
add(t12);
JButton b1 = new JButton("添加");
b1.setBounds(100, 200, 60, 30);
b1.setLocation(450,350);
add(b1);
b1.addActionListener(this);
JButton b2 = new JButton("修改身份证");
b2.setBounds(100, 200, 120, 30);
b2.setLocation(540,350);
add(b2);
b2.addActionListener(this);
JButton b3 = new JButton("删除");
b3.setBounds(100, 200, 60, 30);
b3.setLocation(690,350);
add(b3);
b3.addActionListener(this);
}
public void iniNMenu(){
menuBar = new JMenuBar();
menuFruit = new JMenu("操作菜单");//创建一个菜单
pInformation = new JMenuItem("个人信息");//创建一个菜单项
rInformation = new JMenuItem("户口信息");//创建一个菜单项
//增加监听器
pInformation.addActionListener(this);
rInformation.addActionListener(this);
//向菜单中添加菜单项
menuFruit.add(pInformation);//向菜单中添加菜单项
menuFruit.add(rInformation);
menuBar.add(menuFruit);//向菜单条中添加菜单
//为实例窗口添加菜单
this.setJMenuBar(menuBar);
}
public void nPTable(){
}
public void nRTable(){
String [] index={"户口","户主","姓名","身份证","贫困等级"};
Object [][] data=new Object[3][index.length];
//3,向data中添加数据
for (int i = 0,j = 0; i < 3; i++) {
data[j][0]=1;
data[j][1]="张三";
data[j][2]="张三";
data[j][3]="张三";
data[j][4]="张三";
}
//4,创建一个默认的表格模型
DefaultTableModel defaultModel = new DefaultTableModel(data, index);
JTable table = new JTable(defaultModel);
this.setContentPane(new JScrollPane(table));
}
public void mPTable(){
String [] index={"编号","户口","姓名","性别","电话","住址","住所面积","工作地点"};
Object [][] data=new Object[pList.size()][index.length];
//3,向data中添加数据
for (int i = 0; i < pList.size(); i++) {
PI pi = pList.get(i);
data[i][0]=pi.getPno();
data[i][1]=pi.getRno();
data[i][2]=pi.getPname();
data[i][3]=pi.getSex();
data[i][4]=pi.getPhonenum();
data[i][5]=pi.getAddress();
data[i][6]=pi.getHousearea();
data[i][7]=pi.getWorkplace();
}
//4,创建一个默认的表格模型
DefaultTableModel defaultModel = new DefaultTableModel(data, index);
JTable table = new JTable(defaultModel);
this.setContentPane(new JScrollPane(table));
}
public void mRTable(){
String [] index={"户口","户主","身份证","贫困等级"};
Object [][] data=new Object[rList.size()][index.length];
//3,向data中添加数据
for (int i = 0; i < rList.size(); i++) {
RI ri = rList.get(i);
data[i][0]=ri.getRno();
data[i][1]=ri.getHeadname();
data[i][2]=ri.getIdnum();
data[i][3]=ri.getPovertylevel();
}
//4,创建一个默认的表格模型
DefaultTableModel defaultModel = new DefaultTableModel(data, index);
JTable table = new JTable(defaultModel);
this.setContentPane(new JScrollPane(table));
}
public static void main(String[] args) {
new Test(1);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == pInformation){
nPTable();
}
if(e.getSource() == rInformation){
nRTable();
this.validate();
iniNOMenu();
}
if(e.getSource() == aPInformation){
mPTable();
}
if(e.getSource() == aRInformation){
mRTable();
}
}
}