【问题标题】:How to correctly align two JPanel inside a JFrame如何在 JFrame 中正确对齐两个 JPanel
【发布时间】:2015-09-19 02:49:02
【问题描述】:

我正在为一个小游戏/练习构建一个界面。在这个界面中,我有一个顶部部分,女巫是一个 JPanel,它采用窗口的宽度和一定的高度。在它的正下方是另一个部分(再次是JPanel),它应该与左侧对齐并在第一部分下方。

我一直在努力使界面看起来像我想要的那样。我尝试了两件事,但都失败了:

第一个是我有扩展 JFrame 的 GameView 类,我为这两个部分创建了一个 JPanel 并将它们直接添加到 JFrame,但看起来它们只是相互重叠。黑色部分是第一个,红色部分是第二个:

我尝试的第二件事是将这两个 JPanel 放入另一个称为容器的 JPanel 中,但仍然没有得到我想要的。第一部分是完美的,但第二部分应该贴在左边,我希望这两个部分之间没有空格:

我怎样才能将第二部分(红色部分)粘贴到左侧并且两个部分之间没有空间?这是我班级的代码:

打包游戏;

import java.awt.Color;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

    public class GameView extends JFrame //implements MouseListener
    {   
        private GameNumView numberPanel;
        private JLabel butLabel;
        private JLabel progresLabel;
        private JButton nextButton;
        private JButton giveUpButton;
        private JButton resetButton;
        private JCheckBox findMeanCheckBox;
        private JCheckBox noiseCheckBox;

        public GameView()
        {
            initUI();
        }

        public void initUI()
        {
            setTitle("Sommurai");

            setSize(800, 350);

            setLocationRelativeTo(null);

            butLabel = new JLabel("97");
            progresLabel = new JLabel("Somme: 90 (2)");

            nextButton = new JButton("NEXT");
            giveUpButton = new JButton("GIVE UP");
            resetButton = new JButton("RESET");

            findMeanCheckBox = new JCheckBox("Find Mean");
            noiseCheckBox = new JCheckBox("Noise");

            createLayout(butLabel, progresLabel, nextButton, giveUpButton, resetButton, findMeanCheckBox, noiseCheckBox);

            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }

        private void createLayout(JComponent... arg)
        {
            JPanel container = new JPanel();

            numberPanel = new GameNumView(800, 120);

            JPanel buttonsPanel = new JPanel();
            GroupLayout gl = new GroupLayout(buttonsPanel);
            buttonsPanel.setLayout(gl);

            gl.setAutoCreateContainerGaps(true);

            GroupLayout.SequentialGroup hGroup = gl.createSequentialGroup();

            GroupLayout.SequentialGroup vGroup = gl.createSequentialGroup();

            hGroup.addGroup(gl.createParallelGroup()
                    .addComponent(arg[0])
                    .addComponent(arg[1])
                    .addComponent(arg[2])
                    .addComponent(arg[3])
                    .addComponent(arg[4])
                    .addComponent(arg[5])
                    .addComponent(arg[6]));

            gl.setHorizontalGroup(hGroup);

            vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE)
                     .addComponent(arg[0]));
            vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE)
                     .addComponent(arg[1]));
            vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE)
                     .addComponent(arg[2]));
            vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE)
                     .addComponent(arg[3]));
            vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE)
                     .addComponent(arg[4]));
            vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE)
                     .addComponent(arg[5]));
            vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE)
                     .addComponent(arg[6]));

            gl.setVerticalGroup(vGroup);

    buttonsPanel.setBackground(Color.RED);//

            container.add(numberPanel);
            container.add(buttonsPanel);
            add(container);
        }
    }

GameNumView 是另一个 JPanel 类

【问题讨论】:

标签: java swing jpanel


【解决方案1】:

使用 BorderLayout() 代替组布局。制作红色面板 BorderLayout.LEFT 和另一个 BorderLayout.CENTER。

例子:

JPanel container = new JPanel();
JFrame frame = new JFrame();
frame.add (container, BorderLayout.LEFT);

【讨论】:

    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    相关资源
    最近更新 更多