2014-06-16 75 views
1

我想將test2添加到我的DFrametest類中,以便test2的表格顯示在我的窗口中。我得到一個空的窗口,無法找到錯誤。向JFrame添加JTable

public class test2 extends JPanel { 

    JTable tbl; 
    DefaultTableModel dt; 

    public test2(){ 
     JLabel label = new JLabel("Course Lookup GUI"); 
     this.add(label); 
     tbl = new JTable(); 
     dt = new DefaultTableModel(); 
     dt.addColumn("ID"); 
     dt.addColumn("Name"); 
     tbl.setModel(dt); 

     try{ 
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/lagerverwaltungwin", "root", ""); 
      Statement st = con.createStatement(); 
      ResultSet rs = st.executeQuery("SELECT ArtNr, Beschreibung FROM artikel"); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

     JScrollPane jp = new JScrollPane(); 
     jp.getViewport().add(tbl); 
     add(jp); 
    } 

} 

這是我Frameclass應該具有test2表:

public class DFrametest extends JFrame { 

    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        DFrametest frame = new DFrametest(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public DFrametest() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 
     test2 t = new test2(); 
     this.add(t); 
     this.setVisible(true); 
    } 

} 

回答

-1

嘗試使用的setBounds()方法,因爲你沒有提到任何佈局(setLayout的爲空)。添加的JPanel到JFrame

setBounds(x-axis,y-axis,width,height); 

EX前 添加的setBounds方法: -

setBounds(10,20,200,400); 

注:

+0

-1,表明空佈局。 – Arijit

+0

我還沒有提出建議。這是OP的斬首。然而,只是想知道我們怎樣才能在指定的/所需的位置放置一些組件。在JFrame上,你能回答@Arijit – user3505725

+1

''我們如何將一些組件放在指定的/所需的位置。在JFrame上''Read [Layout經理](http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html) – Arijit

2

切勿使用空佈局。此外DONOT使用setBounds(100, 100, 450, 300); 使用pack();

更改DFrametest類這樣

 contentPane.setLayout(new BorderLayout()); 
     contentPane.add(t, BorderLayout.CENTER); 

DFrametest類的全碼:

public class DFrametest extends JFrame { 

    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        DFrametest frame = new DFrametest(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public DFrametest() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     test2 t = new test2(); 
     contentPane.setLayout(new BorderLayout()); 
     contentPane.add(t, BorderLayout.CENTER); 

     pack(); 
    } 

}