2011-06-19 59 views
4

我正在使用Swing和AWT(用於偵聽器)來製作一個小程序。我有一個關於獲取我的JPanel(名爲Chess的類)大小的問題。 我的佈局:Java,BorderLayout.CENTER,獲取JPanel的寬度和高度

public class Main extends JFrame implements MouseListener, ActionListener{ 

    Chess chessPanel = new Chess(); 
    JButton newGameButton = new JButton ("New Game"); 
    JButton loadGameButton = new JButton ("Load Game"); 
    JButton saveGameButton = new JButton ("Save Game"); 
    JButton exitButton = new JButton ("Exit"); 

    public static void main (String [] args) { 
     new Main(); 
    } 

    Main() { 
     super ("Chess"); 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     setSize(dim); 
     setLocation(0,0); 
     setUndecorated(true); 

     chessPanel.addMouseListener(this); 
     add(chessPanel, BorderLayout.CENTER); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new FlowLayout()); 


     newGameButton.addActionListener(this); 
     loadGameButton.addActionListener(this); 
     saveGameButton.addActionListener(this); 
     exitButton.addActionListener(this); 

     buttonPanel.add(newGameButton); 
     buttonPanel.add(loadGameButton); 
     buttonPanel.add(saveGameButton); 
     buttonPanel.add(exitButton); 

     add(buttonPanel, BorderLayout.SOUTH); 

     setVisible(true); 
    } 

    // ... Code ... 
} 

你可以通過代碼中看到的,我有一個的JPanel的中心,這需要幾乎所有的屏幕。在底部我有另一個JPanel(SOUTH),它有一排按鈕。

我需要的是中心中的JPanel所需的大小。當我調用從JPanel繼承的getWidth(),getHeight()或getBounds()方法時,由於BorderLayout,它們都返回0。 任何想法如何獲得真正的價值?

PS:屏幕總是佔用整個屏幕,並且永遠不會調整大小,如果有幫助的話。

回答

7

您可能在JPanel渲染之前調用getWidth,因此它將爲0.解決的辦法是在渲染後獲得大小,例如在調用pack()或setVisible(true)之後在容納此JPanel的根容器上。因爲大多數標準佈局管理器都遵守組件的首選大小而不是大小,並且當您調用pack()告訴佈局管理器執行它們的操作時,我建議不要在任何情況下調用setSize()設置的大小通常被忽略。如果需要達到一定的大小,可以通過覆蓋setPreferredSize方法來讓自己的JPanel設置自己的大小。然後讓JFrame及其保存的容器在打包時根據其佈局管理器設置下注大小。

例如,

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

public class Main extends JFrame { 

    Chess chessPanel = new Chess(); 
    JButton newGameButton = new JButton("New Game"); 
    JButton loadGameButton = new JButton("Load Game"); 
    JButton saveGameButton = new JButton("Save Game"); 
    JButton exitButton = new JButton("Exit"); 

    public static void main(String[] args) { 
     new Main(); 
    } 

    Main() { 
     super("Chess"); 
     add(chessPanel, BorderLayout.CENTER); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new FlowLayout()); 

     buttonPanel.add(newGameButton); 
     buttonPanel.add(loadGameButton); 
     buttonPanel.add(saveGameButton); 
     buttonPanel.add(exitButton); 

     System.out.printf("chessPanel Size before rendering: %s%n", chessPanel.getSize()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     add(buttonPanel, BorderLayout.SOUTH); 
     pack(); 
     System.out.printf("chessPanel Size after rendering: %s%n", chessPanel.getSize()); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    // ... Code ... 
} 

@SuppressWarnings("serial") 
class Chess extends JPanel { 
    private static final int CHESS_WIDTH = 600; 
    private static final int CHESS_HEIGHT = CHESS_WIDTH; 
    private static final int MAX_ROW = 8; 
    private static final int MAX_COL = 8; 
    private static final Color LIGHT_COLOR = new Color(240, 190, 40); 
    private static final Color DARK_COLOR = new Color(180, 50, 0); 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(CHESS_WIDTH, CHESS_HEIGHT); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     int panelWidth = getWidth(); 
     int panelHeight = getHeight(); 
     int sqrWidth = panelWidth/MAX_ROW; 
     int sqrHeight = panelHeight/MAX_COL; 
     for (int row = 0; row < MAX_ROW; row++) { 
     for (int col = 0; col < MAX_COL; col++) { 
      Color c = (row % 2 == col % 2) ? LIGHT_COLOR : DARK_COLOR; 
      g.setColor(c); 
      int x = (row * panelWidth)/MAX_ROW; 
      int y = (col * panelHeight)/MAX_COL; 
      g.fillRect(x, y, sqrWidth, sqrHeight); 
     } 
     } 
    } 
} 
+0

@Hidde:歡迎您! –