与 JPanel 顶部的距离增加

问题描述 投票:0回答:1

................................................ ...................................................... ...................................................... …………

java swing jpanel jbutton layout-manager
1个回答
2
投票

关键在于,事实上,组件不知道它的实际大小,直到

frame.pack()
不会被调用。因此,在此之后,我将执行此计算,以确定要为
Border
放置多少空白空间,并在放置
frame.pack()
后再次调用
repack()
Border
的所有内容。

请看一下这个示例,看看这个套件是否符合您的需要:

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

public class MainMenu {

    private JButton playButton;
    private JButton instructionButton;
    private JButton scoreboardButton;
    private JButton exitButton;

    private JPanel menuPanel;

    private GridBagConstraints gbc;

    public MainMenu() {
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(15, 15, 15, 15);
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Main Menu");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new BorderLayout());

        menuPanel = new JPanel(new GridBagLayout());
        menuPanel.setOpaque(true);
        menuPanel.setBackground(Color.BLACK);       

        playButton = new JButton("Play");
        instructionButton = new JButton("Instructions");
        scoreboardButton = new JButton("Scoreboard");
        exitButton = new JButton("Exit");

        addComp(menuPanel, playButton, 0, 0, 1, 1, 1.0, 0.20,
                                    GridBagConstraints.HORIZONTAL);
        addComp(menuPanel, instructionButton, 0, 1, 1, 1, 1.0, 0.20,
                                    GridBagConstraints.HORIZONTAL);
        addComp(menuPanel, scoreboardButton, 0, 2, 1, 1, 1.0, 0.20,
                                    GridBagConstraints.HORIZONTAL);
        addComp(menuPanel, exitButton, 0, 3, 1, 1, 1.0, 0.20,
                                    GridBagConstraints.HORIZONTAL);

        contentPane.add(menuPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(
                    contentPane.getHeight() - (contentPane.getHeight() / 4),
                    20, 5, 20));
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp,
                                int gridx, int gridy,
                                int gridwidth, int gridheight,
                                double weightx, double weighty,
                                int fill) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.fill = fill;

        panel.add(comp, gbc);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new MainMenu().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

这是输出:

enter image description here

编辑1:

此外,如果您使用

GridLayout
而不是使用
GridBagLayout
作为
MainMenu
,那么我想结果会更有希望。请查看此示例以了解该更改:

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

public class MainMenu {

    private JButton playButton;
    private JButton instructionButton;
    private JButton scoreboardButton;
    private JButton exitButton;

    private JPanel menuPanel;

    private void displayGUI() {
        JFrame frame = new JFrame("Main Menu");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new GridBagLayout());

        menuPanel = new JPanel(new GridLayout(0, 1, 5, 5));
        menuPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        menuPanel.setOpaque(true);
        menuPanel.setBackground(Color.BLACK);

        playButton = new JButton("Play");
        instructionButton = new JButton("Instructions");
        scoreboardButton = new JButton("Scoreboard");
        exitButton = new JButton("Exit");

        menuPanel.add(playButton);
        menuPanel.add(instructionButton);
        menuPanel.add(scoreboardButton);
        menuPanel.add(exitButton);

        contentPane.add(menuPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(
                    contentPane.getHeight() - 
                        (contentPane.getHeight() - 
                            (3 * menuPanel.getHeight())),
                                                20, 0, 20));
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new MainMenu().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

编辑2:

另一个变体看起来要好得多,不过这次,基础

JPanel
使用
GridLayout
,而
MenuPanel
使用
GridBagLayout
。请看一下这个例子:

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

public class MainMenu {

    private JButton playButton;
    private JButton instructionButton;
    private JButton scoreboardButton;
    private JButton exitButton;

    private JPanel menuPanel;

    private GridBagConstraints gbc;

    public MainMenu() {
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(15, 15, 15, 15);
        gbc.anchor = GridBagConstraints.CENTER;
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Main Menu");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new GridLayout(1, 1, 5, 2));

        menuPanel = new JPanel(new GridBagLayout());
        menuPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        menuPanel.setOpaque(true);
        menuPanel.setBackground(Color.BLACK);

        playButton = new JButton("Play");
        instructionButton = new JButton("Instructions");
        scoreboardButton = new JButton("Scoreboard");
        exitButton = new JButton("Exit");

        addComp(menuPanel, playButton, 0, 0, 1, 1, 1.0, 0.10,
                                    GridBagConstraints.CENTER);
        addComp(menuPanel, instructionButton, 0, 1, 1, 1, 1.0, 0.10,
                                    GridBagConstraints.CENTER);
        addComp(menuPanel, scoreboardButton, 0, 2, 1, 1, 1.0, 0.10,
                                    GridBagConstraints.CENTER);
        addComp(menuPanel, exitButton, 0, 3, 1, 1, 1.0, 0.10,
                                    GridBagConstraints.CENTER);

        contentPane.add(menuPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(
                    contentPane.getHeight() - 
                        (contentPane.getHeight() - 
                            (2 * menuPanel.getHeight()) + 100),
                                20, 2, 20));
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp,
                                int gridx, int gridy,
                                int gridwidth, int gridheight,
                                double weightx, double weighty,
                                int fill) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.fill = fill;

        panel.add(comp, gbc);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new MainMenu().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.