2014-10-11 65 views
0

我無法將表跨越到JFrame邊界。我嘗試使用setMinimumSize,但它沒有奏效。我錯過了什麼?JTable將不會跨越JFrame的邊界

添加:我不想將JScrollPane添加到表中(尚未)。我只是想知道如何讓GridBagLayout將特定組件調整爲JFrame的邊框。

public class Ost extends JFrame{ 
    OstBridge bridge; 
    public Ost() { 
     Container cp=this.getContentPane(); 
     GridBagConstraints c=new GridBagConstraints(); 
     cp.setLayout(new GridBagLayout()); 

     // Add button 
     JButton b1=new JButton("Button"); 
     c.gridx=0; 
     c.gridy=0; 
     c.insets=new Insets(20,0,20,0); 
     cp.add(b1,c); 

     // Table data 
     String[] columns={"Album","Url"}; 
     Object[][] data={ 
       {"test1","tes2"} 
     }; 
     // Add Table 
     JTable table=new JTable(data,columns); 
     c.gridx=0; 
     c.gridy=1; 
     c.weightx=1; 
     c.weighty=1; 
     c.gridwidth=c.REMAINDER; 
     table.setBackground(Color.RED); 
     table.setMinimumSize(new Dimension(400,400)); 
     cp.add(table, c); 

     // Show All 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.setSize(320, 240); 
     this.setTitle("Test"); 
     this.setVisible(true); 


    } 

這就是我得到:

http://fjjf.com.ve/i1.png

而這正是我想獲得:

http://fjjf.com.ve/i2.png

+0

我想你需要使用'fill constraint'。查看[如何使用GridBagLayout]上的Swing教程部分(http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html)以獲取更多信息和示例。 – camickr 2014-10-11 00:44:18

回答

1

在爲表我應該約束」 ve used GridBagConstraints.fill

// Add Table 
    JTable table=new JTable(data,columns); 
    c.gridx=0; 
    c.gridy=1; 
    c.weightx=1; 
    c.weighty=1; 
    c.gridwidth=c.REMAINDER; 

    c.fill=GridBagConstraints.HORIZONTAL; // this line solves the problem 

    table.setBackground(Color.RED); 
    table.setMinimumSize(new Dimension(400,400)); 
    cp.add(table, c); 

希望這個答案可以幫助別人試圖給我java第一步驟! :)