如何在JFrame

时间:2015-09-19 02:49:02

标签: java swing jpanel

我正在建立一个小游戏/运动的界面。在这个界面中,我得到了一个顶部,女巫是一个JPanel,它占据了窗口的宽度和一定的高度。在它下面是另一个部分(JPanel再次),假设在左边对齐并且在第一部分下面是wright。

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

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

The result of my first method

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

The result of my second method

如何将第二部分(红色部分)粘在左边,两部分之间没有空格?这是我班级的代码:

包游戏;

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

的类

1 个答案:

答案 0 :(得分:0)

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

示例:

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