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

前端 未结 4 1459
北海茫月
北海茫月 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:50

    GridBagLayout is what you really want to use. The GridLayout will force the same size for each component in the layout no matter what size constraints you put on them. GridBagLayout is a lot more powerful and a lot more complicated. Study up on the API page for it. Using GridBagLayout, the components won't fill the whole grid space if you don't want them to and can even stay the size that you ask it to be. To keep a component's size from changing, I would set all three available size constraints:

    water.setPreferredSize(new Dimension(20, 20));
    water.setMinimumSize(new Dimension(20, 20));
    water.setMaximumSize(new Dimension(20, 20));
    

    For your buttons, I would definitely use an inner panel as Bryan mentions. You could use either a GridLayout like he suggests or a FlowLayout if you don't want all the buttons to be the same size. Add all your buttons to that inner panel instead of the main one.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-21 07:58

    I have had success solving problems like these using TableLayout which is a third party layout manager. You will need to download it and read the tutorial but the key would be to set the justification to CENTER when adding the JButtons to their positions in the layout.

    0 讨论(0)
  • 2021-01-21 08:01

    If you can setResizeable( false ) on the top level frame you can then set your layout manager to null and hard code each location and size via setBounds. This is how I would do it (contingent on resizing of course).

    0 讨论(0)
提交回复
热议问题