Can you set a permanent size for a JPanel inside of a JFrame?

前端 未结 4 1465
北海茫月
北海茫月 2021-01-21 07:43

My current problem is that I have a JFrame with a 2x2 GridLayout. And inside one of the squares, I have a JPanel that is to display a grid. I am having a field day with the java

4条回答
  •  走了就别回头了
    2021-01-21 07:56

    If you want the two checkerboards to stay the same size, then you'll need to have them each contained in their own JPanel. Set each of those parent JPanel's to have a layout type of GridBagLayout. Set the preferedSize for each checkerboard component and then add them to their respective containers. GridBagLayout should by default lay each board out in the center of the parent JPanel. So as the window is resized, the JPanel parent area will get larger or smaller, but the checkerboard components inside will remain the same size.

    Alternatively, you could have your blue squares scale to the right size as the window is resized by having each checkboard square be a JPanel with a BorderLayout layout manager and adding the JLabel (with a blue background color) to its BorderLayout.CENTER location.

    As for your buttons, try something like this:

    JPanel theButtonPanel = new JPanel(new BorderLayout());
    JButton button1 = new JButton("Fire");
    JButton button2 = new JButton("Pass");
    JButton button3 = new JButton("Forfiet");
    
    JPanel innerButtonContainer = new JPanel(new Grid(1, 3, 8, 8));
    innerButtonContainer.add(button1);
    innerButtonContainer.add(button2);
    innerButtonContainer.add(button3);
    
    theButtonPanel.add(innterButtonContainer);
    

    Lastly, consider using a design tool for your Swing user interface. Netbeans has an excellent UI designer built into it. Download Netbeans here.

提交回复
热议问题