2015-11-22 130 views
2

我不明白問題出在哪裏。 JTable嵌入在JPanel中嵌入的JScrollPanel中。該表不顯示。任何幫助讚賞。我可能錯過了添加一些元素。徹底檢查,但找不到任何東西。這僅僅是一個構造函數:JPanel中的JTable不顯示

public TableIssues() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 894, 597); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     JPanel patientsPanel = new JPanel(); 
     patientsPanel.setBounds(6, 152, 882, 417); 
     patientsPanel.setLayout(null); 

     String[] patientsColumns = { 
       "one", 
       "two", 
       "three"}; 
     String[][] tableInput={{"first","second","third"},{"first","second","third"}}; 

     patientsTable = new JTable(tableInput,patientsColumns); 
     JScrollPane scroll = new JScrollPane(); 
     scroll.setBounds(0, 0, 882, 363); 
     scroll.setLayout(null); 
     scroll.setViewportView(patientsTable); 
     patientsPanel.add(scroll); 

     JButton addPatietsButton = new JButton("Add"); 
     addPatietsButton.setFont(new Font("Lucida Grande", Font.PLAIN, 20)); 
     addPatietsButton.setBounds(356, 375, 211, 36); 

     patientsPanel.add(addPatietsButton); 
     contentPane.add(patientsPanel); 

    } 
+0

請參閱編輯與示例代碼來回答。 –

回答

5

NEVER做到這一點:

scroll.setLayout(null); 

你毀了JScrollPane的佈局,因此它完全失去了它的功能,從而搬起石頭砸自己的腳。刪除該行。

雖然空佈局和setBounds()對於Swing新手來說似乎是創建複雜GUI的最簡單和最好的方式,但更多的Swing GUI會創建使用它們時遇到的更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。

例如,該GUI

enter image description here

由該代碼創建:

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import javax.swing.*; 
import javax.swing.table.DefaultTableModel; 

public class TableIssues2 extends JPanel { 
    private static final int GAP = 5; 
    private static final String[] COL_NAMES = {"One", "Two", "Three"}; 
    private DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0); 
    private JTable patientsTable = new JTable(model); 

    public TableIssues2() { 
     JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, GAP)); 
     buttonPanel.add(new JButton("Add")); 
     buttonPanel.add(new JButton("Remove")); 
     buttonPanel.add(new JButton("Exit")); 

     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(buttonPanel); 

     for (int i = 0; i < 5; i++) { 
      model.addRow(new String[]{"First", "Second", "Third"}); 
     } 
     setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     setLayout(new BorderLayout(GAP, GAP)); 
     add(new JScrollPane(patientsTable), BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.PAGE_END); 
    } 

    private static void createAndShowGui() { 
     TableIssues2 mainPanel = new TableIssues2(); 

     JFrame frame = new JFrame("TableIssues2"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
}