2011-10-02 32 views
0

我試圖使用GridLayout顯示一個框架,但我的一個面板不顯示。Gui - JPanel將JComponent添加到GridLayout單元格

我遇到的(gridPanel)有問題的JPanel應該有一個50乘50的GridLayout,並且該網格中的每個單元都應該添加一個Square對象。然後,該面板應該被添加到框架,但它不顯示。

import java.awt.*; 
import javax.swing.*; 

public class Gui extends JFrame{ 

    JPanel buttonPanel, populationPanel, velocityPanel, gridPanel; 
    JButton setupButton, stepButton, goButton; 
    JLabel populationLabel, velocityLabel; 
    JSlider populationSlider, velocitySlider; 
    Square [] [] square; 

    public Gui() { 

     setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     //Set up JButtons 
     buttonPanel = new JPanel(); 
     setupButton = new JButton("Setup"); 
     stepButton = new JButton("Step"); 
     goButton = new JButton("Go"); 

     buttonPanel.add(setupButton); 
     buttonPanel.add(stepButton); 
     buttonPanel.add(goButton); 

     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 1; 
     c.gridwidth = 2; //2 columns wide 
     add(buttonPanel, c); 


     //Set up populationPanel  
     populationPanel = new JPanel(); 
     populationLabel = new JLabel("Population"); 
     populationSlider = new JSlider(JSlider.HORIZONTAL,0, 200, 1); 

     populationPanel.add(populationLabel); 
     populationPanel.add(populationSlider); 

     c.fill = GridBagConstraints.LAST_LINE_END; 
     c.gridx = 0; 
     c.gridy = 2; 
     c.gridwidth = 2; //2 columns wide 
     add(populationPanel, c);   


     //Set up velocityPanel 
     velocityPanel = new JPanel(); 
     velocityLabel = new JLabel(" Velocity"); 
     velocitySlider = new JSlider(JSlider.HORIZONTAL,0, 200, 1); 

     velocityPanel.add(velocityLabel); 
     velocityPanel.add(velocitySlider); 

     c.fill = GridBagConstraints.LAST_LINE_END; 
     c.gridx = 0; 
     c.gridy = 3; 
     c.gridwidth = 2; //2 columns wide 
     add(velocityPanel, c); 


     //Set up gridPanel 
     JPanel gridPanel = new JPanel(new GridLayout(50, 50)); 
     square = new Square[50][50]; 

     for(int i = 0; i < 50; i++){ 
      for(int j = 0; j < 50; j++){ 
        square[i][j] = new Square(); 
        gridPanel.add(square[i][j]); 
      } 
     } 

     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 0; 
     c.gridwidth = 2; //2 columns wide 
     add(gridPanel, c); 
    } 

    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event-dispatching thread. 
    */ 
    private static void createAndShowGUI() { 
     //Create and set up the window. 
     Gui frame = new Gui(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

Square類

import java.awt.*; 
import javax.swing.*; 

public class Square extends JComponent{ 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.BLACK); 
     g.fillRect(10, 10, 10, 10); 
    } 

} 

回答

3

你沒有看到任何東西的原因有兩方面(雖然不能完全肯定,如果第二個是故意的,猜測的那部分:)

  • JComponent是一個裸露的容器,它沒有用戶代理,也沒有固有的大小 - 這取決於你的代碼返回一些合理的最小/最大/最小值。畫矩形是硬編碼爲「地方」裏,這可能是也可能是實際的組件面積

繼內部展示(邊境僅見邊界)

public static class Square extends JComponent { 

    public Square() { 
     setBorder(BorderFactory.createLineBorder(Color.RED)); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.BLACK); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(10, 10); 
    } 
    @Override 
    public Dimension getMaximumSize() { 
     return getPreferredSize(); 
    } 
    @Override 
    public Dimension getMinimumSize() { 
     return getPreferredSize(); 
    } 

} 
+0

'的getPreferredSize(){ 返回新維(10,10); ..'(有禮貌的咳嗽)歡迎來到強制組件的黑暗面,以達到特定的尺寸。 ;) –