与JPanel顶部的距离增加 [英] Increasing distance from top of JPanel

查看:223
本文介绍了与JPanel顶部的距离增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to increase the distance of my JButtons from the top of my Panel to make it more visually appealing, i've tried using an invisible button but have had no luck.

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()之前,该组件不知道其实际大小.因此,在此之后,我将执行此计算,以确定要为Border放置多少空白空间,并在放置Border之后再次调用frame.pack()repack().

The key basically lies, in the fact, that the component doesn't knows it's actual size, till frame.pack() won't be called. Hence after this I am performing this calculation, to determine how much empty space to put for Border and again calling frame.pack() to repack() everything after putting the Border.

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

Please do have a look at this example, and see if this suite your needs :

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

以下是输出:

此外,如果您将使用GridLayout而不是GridBagLayout作为MainMenu,那么我想结果会更有希望.请查看此更改的示例:

Moreover, if you will use GridLayout instead of using GridBagLayout for the MainMenu, then I guess the results will be more promising. Please have a look at this example for that change :

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

另一个变种看起来要好得多,但是,这次,基本JPanel使用的是GridLayout,而MenuPanel使用的是GridBagLayout.请看这个例子:

EDIT 2 :

Another variant is looking much better, though, this time, the base JPanel is using GridLayout and the MenuPanel is using GridBagLayout. Please have a look at this example :

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

这篇关于与JPanel顶部的距离增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆