2

我正在尝试增加 JButton 与面板顶部的距离,以使其更具视觉吸引力,我尝试使用不可见的按钮,但没有运气。

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

}

有任何想法吗 ?

4

1 回答 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);
    }
}

这是输出:

在此处输入图像描述

编辑 1:

此外,如果您将使用GridLayout而不是使用GridBagLayoutfor 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);
    }
}
于 2013-09-25T13:22:27.223 回答