2014-09-06 64 views
1

我正在創建一個國際象棋遊戲,其中主面板使用BorderLayout,面板上按鈕爲NORTH,面板爲CENTER,板子本身(設置爲GridLayout)和側邊欄East如何在JFrame中調整這些面板的大小?

我已經讓JFrame變得難以置信,我想讓棋盤適合面板,以便East面板更寬(可能200像素),而board仍然是方形。我無法弄清楚如何單獨更改這些組件的大小。

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

public class GameWindow extends JFrame { 

    private final JPanel playArea = new JPanel(new BorderLayout(3,3)); 
    private final JButton[][] boardSquares = new JButton[8][8]; 
    private final JPanel board; 
    private final JPanel sidebar = new JPanel(); 
    private final JLabel message = new JLabel("Game by ..."); 

    public GameWindow() { 
     playArea.setBorder(new EmptyBorder(5, 5, 5, 5)); 

     JToolBar tools = new JToolBar(); 


     tools.setFloatable(false); 
     playArea.add(tools, BorderLayout.PAGE_START); 
     tools.add(new JButton("New Game")); 
     tools.add(new JButton("Save")); 
     tools.add(new JButton("Restore")); 
     tools.addSeparator(); 
     tools.add(new JButton("Resign")) 
     tools.addSeparator(); 
     tools.add(message); 

     board = new JPanel(new GridLayout(0, 8)); 
     board.setBorder(new LineBorder(Color.BLACK)); 
     playArea.add(board, BorderLayout.CENTER); 
     playArea.add(sidebar, BorderLayout.EAST); 

     Insets buttonMargin = new Insets(0,0,0,0); 
     for (int i = 0; i < boardSquares.length; i++) { 
      for (int j = 0; j < boardSquares[i].length; j++) { 
       JButton square = new JButton(); 
       square.setMargin(buttonMargin); 
       if ((i+j)%2 == 0) { 
        square.setBackground(Color.WHITE); 
       } 
       else { 
        square.setBackground(Color.BLACK); 
       } 
       board.setSize(600, 600); 
       board.add(boardSquares[j][i] = square); 
      } 
     } 
    } 

    public final JComponent getChessBoard() { 
    return board; 
    } 

    public final JComponent getGui() { 
     return playArea; 
    } 

    public static void main(String[] args) { 

     GameWindow window = new GameWindow(); 

     JFrame frame = new JFrame("Checkers"); 
     frame.setResizable(false); 
     frame.add(window.getGui()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationByPlatform(true); 
     frame.setSize(800, 800); 

     frame.setVisible(true); 
    } 
} 

回答

4

首先,由於JDK 1.4,Java正在鼓勵使用BorderLayout常量BorderLayout.PAGE_STARTBorderLayout.LINE_STARTBorderLayout.CENTERBorderLayout.LINE_ENDBorderLayout.PAGE_END對您在使用後者。

其次,您可以簡單地覆蓋所述JPanelgetPreferredSize(),以便給出一些您認爲適用於您的用例的大小。 setPreferredSize()的使用受到限制,因爲並非所有LayoutManagers都使用它來遵守由其指定的維度。

因此,你可以這樣做:

private final JPanel sidebar = new JPanel() { 
    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(100, 100); 
    } 
}; 

你可以試試這個修改後的代碼:

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

public class GameWindow extends JFrame { 

    private final JPanel playArea = new CustomPanel(710, 710); 
    private final JButton[][] boardSquares = new JButton[8][8]; 
    private final JPanel board; 
    private final JPanel sidebar = new CustomPanel(100, 100); 
    private final JLabel message = new JLabel("Game by ..."); 

    public GameWindow() { 
     playArea.setLayout(new BorderLayout(3,3)); 
     playArea.setBorder(new EmptyBorder(5, 5, 5, 5)); 

     JToolBar tools = new JToolBar(); 


     tools.setFloatable(false); 
     playArea.add(tools, BorderLayout.PAGE_START); 
     tools.add(new JButton("New Game")); 
     tools.add(new JButton("Save")); 
     tools.add(new JButton("Restore")); 
     tools.addSeparator(); 
     tools.add(new JButton("Resign")); 
     tools.addSeparator(); 
     tools.add(message); 

     board = new CustomPanel(600, 600); 
     board.setLayout(new GridLayout(0, 8)); 
     board.setBorder(new LineBorder(Color.BLACK)); 
     playArea.add(board, BorderLayout.CENTER); 
     playArea.add(sidebar, BorderLayout.LINE_START); 

     Insets buttonMargin = new Insets(0,0,0,0); 
     for (int i = 0; i < boardSquares.length; i++) { 
      for (int j = 0; j < boardSquares[i].length; j++) { 
       JButton square = new JButton(); 
       square.setOpaque(true); 
       square.setMargin(buttonMargin); 
       if ((i+j)%2 == 0) { 
        square.setBackground(Color.WHITE); 
       } 
       else { 
        square.setBackground(Color.BLACK); 
       } 
       board.add(boardSquares[j][i] = square); 
      } 
     } 
    } 

    private class CustomPanel extends JPanel { 
     private int width; 
     private int height; 

     public CustomPanel(int width, int height) { 
      this.width = width; 
      this.height = height; 
      setOpaque(true); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(width, height); 
     } 
    } 

    public final JComponent getChessBoard() { 
    return board; 
    } 

    public final JComponent getGui() { 
     return playArea; 
    } 

    public static void main(String[] args) { 

     GameWindow window = new GameWindow(); 

     JFrame frame = new JFrame("Checkers"); 
     frame.setResizable(false); 
     frame.setContentPane(window.getGui()); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.setLocationByPlatform(true); 
     frame.pack(); 

     frame.setVisible(true); 
    } 
} 

此外,設置的JButton背景之前,明智的做法是叫button.setOpaque(true)財產。

+0

爲什麼'frame.setSize(800,800);'而不是'frame.pack();'? – 2014-09-06 10:35:23

+0

@AndrewThompson:我的不好,我想刪除,所有,我也看到了,認爲我也刪除了:-) Thankyou指出:-) – 2014-09-06 11:32:10

1

設置JPanel的尺寸並不難。請致電setPreferredSize()。你的情況來調整你的東JPanel,請致電:

sidebar.setPreferredSize(new Dimension(200, 200)); 

之後,你會LayoutManagerJPanel的大小設置爲200,200。

對你的其他JPanelboard:這是不可能使Component(如JPanel)保持正方形。他們總是適合長方形。您需要製作JComponent的自己的子類,並且只繪製正方形中的所有內容,並使其餘部分保持透明。因此,覆蓋方法JComponent.paintComponent(Graphics)

+0

請參閱[我應該避免在Swing中使用set(Preferred | Maximum | Minimum)Size方法?](http://stackoverflow.com/q/7229226/418556)(是) – 2014-09-06 10:34:31

+0

@AndrewThompson不要以爲它的更好地覆蓋'JPanel'的'getPreferredSize'作爲_nIcE cOw_建議?我認爲它只有更多的代碼具有相同的功能 – msrd0 2014-09-06 10:40:33

+0

功能相同,但設計更好。 – 2014-09-06 10:48:09