Moncel 2022-12-10 15:45 采纳率: 86.7%
浏览 50
已结题

JAVASWING button刷新后被覆盖

点击户口信息后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();
        }
    }
}

  • 写回答

2条回答 默认 最新

  • pzzhao 2022-12-10 16:52
    关注

    setContentPane相当于把主面板给覆盖了,这边给你添加了一个单独的table面板,在你代码的基础上改了下,我把数据库去掉,用固定数据测试是ok的,你看下

    
    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;
        JPanel tP;
        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);
            tP = new JPanel();
            tP.setBounds(10, 10, 750, 400);
            tP.setLayout(new BorderLayout());
            add(tP);
        }
        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);
            tP.add(new JScrollPane(table), BorderLayout.NORTH);
        }
        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);
            tP.add(new JScrollPane(table), BorderLayout.NORTH);
        }
        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);
            tP.add(new JScrollPane(table), BorderLayout.NORTH);
        }
        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();
            }
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月10日
  • 已采纳回答 12月10日
  • 创建了问题 12月10日

悬赏问题

  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题