Java 增加与JPanel顶部的距离

Java 增加与JPanel顶部的距离,java,swing,jpanel,jbutton,layout-manager,Java,Swing,Jpanel,Jbutton,Layout Manager,我试图增加我的按钮与面板顶部的距离,使其更具视觉吸引力,我尝试使用一个隐形按钮,但没有成功 public class SimpleBorder { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Bo

我试图增加我的按钮与面板顶部的距离,使其更具视觉吸引力,我尝试使用一个隐形按钮,但没有成功

public class SimpleBorder {

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       

    Border etched = (Border) BorderFactory.createEtchedBorder();


    String[] items = {"A", "B", "C", "D"};
    JList list = new JList(items);

    JTextArea text = new JTextArea(10, 40);

    JScrollPane scrol = new JScrollPane(text);
    JScrollPane scrol2 = new JScrollPane(list);

    JPanel panel= new JPanel();
    panel.add(scrol2,BorderLayout.WEST);
    panel.add(scrol, BorderLayout.EAST);    
    panel.setBorder(etched);

    frame.add(panel);
    frame.setVisible(true);
}

}

有什么想法吗

关键在于,组件不知道它的实际大小,直到不会调用
frame.pack()
。因此,在此之后,我将执行此计算,以确定要为
边框
放置多少空白空间,并在放置
边框
后再次调用
frame.pack()
repack()
所有内容

请查看此示例,并查看此套件是否满足您的需求:

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);
    }
}
以下是输出:

编辑1: 此外,如果在
主菜单中使用
GridLayout
而不是
gridbagloayout
,那么我想结果会更有希望。请查看此示例以了解该更改:

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
使用
gridbagloayout
。请看一下这个例子:

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);
    }
}

为什么您不能更改您定义的插图的顶部?请编辑您的问题,以包含一个显示您描述的问题的插图。在中,通过
URL
访问发布的图像,如图所示;如图所示使用合成图像;或者使用
UIManager
图标,如图所示。相反,将
EmptyBorder
添加到您正在使用的容器中(不知道它是
JLabel
还是
JPanel
):-)只需这样做
panel.setboorder(BorderFactory.createEmptyBorder(25,10,5,10))
,我想可以,否则,尝试更改这些值以适合您的口味:-)@Icy100:请给我半小时,我将为您提供一个示例,以供您参考。让我喝完牛奶:-)谢谢你!享受你的牛奶:)