上图是首次运行,这个时候没问题,但是当点击了切换表格按钮(自己定义的),再切换回来时,就出现了下图的情况:

解决方案 »

  1.   

    看到了,看二楼。  我的Table是放在Box里的  , 有关系吗 ?
      

  2.   

    上代码吧。PS. 切换的话,我觉得使用CardLayout方便一些。
      

  3.   

    回复 5楼  代码有些长 主要看一下 事件处理方面的 代码  import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*; 
    import javax.swing.border.*;
    public class InsertRecord extends JDialog implements ActionListener{ //负责插入记录的类
      static JPanel p1,p2,p3;
      static Box baseBox,baseBox1,baseBox2;
          JLabel x,y,z;
          Object[] name={"学号","姓名","性别","年龄"};
          Object[] Name={"课程号","课程名","学分"};
          Object[] NAme={"学号","课程名","成绩"};
          Object[][] a=new Object[1][4];
          Object[][] b=new Object[1][3];
          Object[][] c=new Object[1][3];
          JTable table1,table2,table3;
          JButton r,m,t,button1,button2,button3;
          JPanel p;
          Connection con;
          Statement sql; 
          ResultSet rs;
          String num;
          InsertRecord(String s){
             setTitle(s);
             button1=new JButton("学生信息插入");
             button2=new JButton("课程信息插入");
             button3=new JButton("选课信息插入");
             p=new JPanel();p1=new JPanel();p2=new JPanel();p3=new JPanel();
             p.add(button1); p.add(button2); p.add(button3);
             add(p,BorderLayout.NORTH);
     //        add(p,BorderLayout.CENTER);
             
             x=new JLabel("输入新记录1:");
             y=new JLabel("输入新记录2:");
             z=new JLabel("输入新记录3:");
             table1=new JTable(a,name);
             table2=new JTable(b,Name);
             table3=new JTable(c,NAme);
             r=new JButton("插入新记录1");
             m=new JButton("插入新记录2");
             t=new JButton("插入新记录3");
         
          p1.setLayout(null);
             baseBox=Box.createHorizontalBox();
             baseBox.add(x);
             baseBox.add(new JScrollPane(table1));
             baseBox.add(r);
             baseBox.setBounds(35,40,600,38);
             
             p2.setLayout(null);
             baseBox1=Box.createHorizontalBox();
             baseBox1.add(y);
             baseBox1.add(new JScrollPane(table2));
             baseBox1.add(m);
             baseBox1.setBounds(35,40,600,38);
             
             p3.setLayout(null);
             baseBox2=Box.createHorizontalBox();
             baseBox2.add(z);
             baseBox2.add(new JScrollPane(table3));
             baseBox2.add(t);
             baseBox2.setBounds(35,40,600,38);
    //       p.add(baseBox);
             button1.addActionListener(this);
             button2.addActionListener(this);
             button3.addActionListener(this);
             r.addActionListener(this);
             setBounds(120,160,700,200);
            // validate();
         }
         public void actionPerformed(ActionEvent e){
             if(e.getSource()==button1){
              getContentPane().removeAll();
              add(p,BorderLayout.NORTH);
              add(p1,BorderLayout.CENTER);
                 p1.add(baseBox);
                 validate();
              }
             if(e.getSource()==button2){
              getContentPane().removeAll();
              add(p,BorderLayout.NORTH);
              add(p2,BorderLayout.CENTER);
              p2.add(baseBox1);
              validate();
             }
             if(e.getSource()==button3){
              getContentPane().removeAll();
              add(p,BorderLayout.NORTH);
              add(p3,BorderLayout.CENTER);
              p3.add(baseBox2);
              validate();
             }
           
              if(e.getSource()==r){
          try{  con=DriverManager.getConnection("jdbc:odbc:jec",Dljm.a,Dljm.b);
          sql=con.createStatement();
                 table1.getCellEditor(0,3).stopCellEditing();//
                 int k=sql.executeUpdate
                 ("INSERT INTO s VALUES('"+a[0][0]+"','"+a[0][1]+"','"+a[0][2]+"','"+a[0][3]+"')");
                 if(k==1)
                  JOptionPane.showMessageDialog
                     (this,"插入记录成功","成功",JOptionPane.PLAIN_MESSAGE); 
                 table1=new JTable(a,name);
                 con.close();
                 }  
          catch(SQLException ee){ 
          JOptionPane.showMessageDialog
          (this,"插入记录失败"+ee,"失败",JOptionPane.ERROR_MESSAGE);
          } }
          }     
    }
      

  4.   

    很奇怪。我把代码运行了一下。没遇到你说的问题。我的环境是WinXP + JDK6u20
      

  5.   

    不会吧 这也太神奇了 你用的编译器是什么呀  我用的是Eclipse 难道跟 编译器有关 哎 对了 你的主函数怎么添加的 那个主函数给我贴过来好吗
      

  6.   

    我没有数据库,就把事件处理的最后一段注释掉了。主函数 new InsertRecord("Hello").setVisible(true);
      

  7.   

    我提点修改意见放置三个按钮的p 放到NORTH。p1,p2,p3放到一个使用CardLayout的JPanel里,然后放在CENTER。使用CardLayout切换,比删除再添加方便多了。
      

  8.   

    我在Java1.5下运行。出现你贴图的样子。只不过在单元格双击时就正常了。
      

  9.   

    回复14楼 我试过你的方法 但是 有错误 我感觉是 BoxLyout 布局和 CardLyout布局冲突 是吗
      

  10.   


     在p1,p2,p3上使用 CardLyout之后 出现的  错误类型
     java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string
      

  11.   

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;public class InsertRecord extends JDialog implements ActionListener { // 负责插入记录的类
        private JPanel     p1, p2, p3;
        private Box        baseBox, baseBox1, baseBox2;
        private JLabel     x, y, z;
        private Object[]   name   = { "学号", "姓名", "性别", "年龄" };
        private Object[]   Name   = { "课程号", "课程名", "学分" };
        private Object[]   NAme   = { "学号", "课程名", "成绩" };
        private Object[][] a      = new Object[1][4];
        private Object[][] b      = new Object[1][3];
        private Object[][] c      = new Object[1][3];
        private JTable     table1, table2, table3;
        private JButton    r, m, t, button1, button2, button3;
        private JPanel     buttonPanel;
        private JPanel     tablePanel;
        private CardLayout layout = new CardLayout();
        /*
         * Connection con; Statement sql; ResultSet rs;
         */
        String             num;    InsertRecord(String s) {
            setTitle(s);
            button1 = new JButton("学生信息插入");
            button2 = new JButton("课程信息插入");
            button3 = new JButton("选课信息插入");
            buttonPanel = new JPanel();
            p1 = new JPanel();
            p2 = new JPanel();
            p3 = new JPanel();
            buttonPanel.add(button1);
            buttonPanel.add(button2);
            buttonPanel.add(button3);
            add(buttonPanel, BorderLayout.NORTH);
            // add(p,BorderLayout.CENTER);        x = new JLabel("输入新记录1:");
            y = new JLabel("输入新记录2:");
            z = new JLabel("输入新记录3:");
            table1 = new JTable(a, name);
            table2 = new JTable(b, Name);
            table3 = new JTable(c, NAme);
            r = new JButton("插入新记录1");
            m = new JButton("插入新记录2");
            t = new JButton("插入新记录3");        p1.setLayout(null);
            baseBox = Box.createHorizontalBox();
            baseBox.add(x);
            baseBox.add(new JScrollPane(table1));
            baseBox.add(r);
            baseBox.setBounds(35, 40, 600, 38);
            p1.add(baseBox);        p2.setLayout(null);
            baseBox1 = Box.createHorizontalBox();
            baseBox1.add(y);
            baseBox1.add(new JScrollPane(table2));
            baseBox1.add(m);
            baseBox1.setBounds(35, 40, 600, 38);
            p2.add(baseBox1);        p3.setLayout(null);
            baseBox2 = Box.createHorizontalBox();
            baseBox2.add(z);
            baseBox2.add(new JScrollPane(table3));
            baseBox2.add(t);
            baseBox2.setBounds(35, 40, 600, 38);
            p3.add(baseBox2);        // p.add(baseBox);
            button1.addActionListener(this);
            button2.addActionListener(this);
            button3.addActionListener(this);
            r.addActionListener(this);        tablePanel = new JPanel(layout);
            tablePanel.add(p1, "students");
            tablePanel.add(p2, "courses");
            tablePanel.add(p3, "elective");        add(tablePanel, BorderLayout.CENTER);
            setBounds(120, 160, 700, 200);
            // validate();
        }    public void actionPerformed(ActionEvent e) {
            if (e.getSource() == button1) {
                layout.show(tablePanel, "students");
            }
            if (e.getSource() == button2) {
                layout.show(tablePanel, "courses");
            }
            if (e.getSource() == button3) {
                layout.show(tablePanel, "elective");
            }
            /*
             * if (e.getSource() == r) { try { con = DriverManager.getConnection("jdbc:odbc:jec", Dljm.a, Dljm.b); sql =
             * con.createStatement(); table1.getCellEditor(0, 3).stopCellEditing();// int k =
             * sql.executeUpdate("INSERT INTO s VALUES('" + a[0][0] + "','" + a[0][1] + "','" + a[0][2] + "','" + a[0][3] +
             * "')"); if (k == 1) JOptionPane.showMessageDialog(this, "插入记录成功", "成功", JOptionPane.PLAIN_MESSAGE); table1 =
             * new JTable(a, name); con.close(); } catch (SQLException ee) { JOptionPane.showMessageDialog(this, "插入记录失败" +
             * ee, "失败", JOptionPane.ERROR_MESSAGE); } }
             */
        }    public static void main(final String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JDialog dialog = new InsertRecord("Hello");
                    dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                }
            });
        }
    }