使用GridBagLayout调整Java Swing组件的大小

时间:2015-08-14 01:26:57

标签: java swing layout-manager gridbaglayout

我的Java Swing GUI遇到了问题。首先,我在其上创建了一个带有GridBagLayout的面板,并将所有标签添加到其中。但是,我还在另一个JPanel右侧创建了一个面板,它添加了一个按钮和2个滑块,这些滑块可能与标签匹配。

问题是JLabel小于其他面板右侧的组件,使它看起来像这样.......

离。水选项 - JSLIDER (jslider看起来更大)

我试图通过将ipadx添加到更大的值来使组件更大,并且我还尝试将标签面板的网格宽度设置得更大,但似乎没有任何效果。它只是没有回应。

以下是代码:

package gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

public class Options extends JFrame{
        //water option (button changes text on or off)
        //make a button listener
        JSlider renderDistance;
        JSlider grassDensity;
        JButton waterToggleButton;

        JLabel rdTitle;
        JLabel gdTitle;
        JLabel wtTitle;
        JButton buttonClose;

        static final int RD_MIN_VALUE = 0;
        static final int RD_MAX_VALUE = 1000;
        static final int RD_INIT_VALUE = 500;


        static final int GD_MIN_VALUE = 0;
        static final int GD_MAX_VALUE = 1000;
        static final int GD_INIT_VALUE = 500;

        public Options() {
                this.setTitle("Settings");
                this.setSize(getMaximumSize());
                this.setLocationRelativeTo(null);
                createView();

                this.setVisible(true);

        }

        private void createView() {
                //Making panels and adding them to window*
                JPanel pOptions = new JPanel();
                this.add(pOptions);

                //These are for the labels that I added so people know which option they are using
                JPanel pOptionLabels = new JPanel(new GridBagLayout());
                pOptions.add(pOptionLabels);

                //These are for the middle columns, the objects like button and slider
                JPanel pOptionObjects = new JPanel(new GridBagLayout());
                pOptions.add(pOptionObjects);

                //Making panels and adding them to window*

                //Initializing Objects*

                GridBagConstraints gbcLabels = new GridBagConstraints();
                GridBagConstraints gbcObjects = new GridBagConstraints();

                renderDistance = new JSlider(RD_MIN_VALUE, RD_MAX_VALUE, RD_INIT_VALUE);
                grassDensity = new JSlider(GD_MIN_VALUE, GD_MAX_VALUE, GD_INIT_VALUE);
                waterToggleButton = new JButton("On");

                rdTitle = new JLabel("Render Distance");
                gdTitle = new JLabel("Grass Density");
                wtTitle = new JLabel("Water Terrain Visibility");

                //Initializing Objects*

                //Giving objects some attributes using methods*

                renderDistance.setMinorTickSpacing(100);
                renderDistance.setMajorTickSpacing(500);
                renderDistance.setPaintTicks(true);
                renderDistance.setPaintLabels(true);

                grassDensity.setMinorTickSpacing(100);
                grassDensity.setMajorTickSpacing(500);
                grassDensity.setPaintTicks(true);
                grassDensity.setPaintLabels(true);

                gbcLabels.gridx = 0;
                gbcLabels.gridy = 0;
                gbcLabels.anchor = GridBagConstraints.LINE_START;

                gbcObjects.gridx = 0;
                gbcObjects.gridy = 0;

                waterToggleButton.addActionListener(new ActionListener() {

                           public void actionPerformed(ActionEvent e) {

                                  if(waterToggleButton.getText().equals("Off")) {
                                          waterToggleButton.setText("On");
                                  }else {
                                          waterToggleButton.setText("Off");


                                  }
                                }
                          });



                //Giving objects some attributes using methods*

                //Add things to panel ex. p.add();
                pOptionLabels.add(rdTitle, gbcLabels);
                gbcLabels.gridy++;
                pOptionLabels.add(gdTitle, gbcLabels);
                gbcLabels.gridy++;
                pOptionLabels.add(wtTitle, gbcLabels);

                gbcObjects.gridx++;
                pOptionObjects.add(renderDistance, gbcObjects);
                gbcObjects.gridy++;
                pOptionObjects.add(grassDensity, gbcObjects);
                gbcObjects.gridy++;
                pOptionObjects.add(waterToggleButton, gbcObjects);





        }

        public static void main(String[] args) {
                new Options();

        }




                }

2 个答案:

答案 0 :(得分:3)

改变你的想法。不要试图将所有标签和所有“其他”组件布置在单独的容器中,而是考虑将它们全部放在同一个容器中。

这样,在布局时,布局计算与单个上下文中的所有组件相关。

Sliders

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

public class Options extends JFrame {
                //water option (button changes text on or off)
    //make a button listener

    JSlider renderDistance;
    JSlider grassDensity;
    JButton waterToggleButton;

    JLabel rdTitle;
    JLabel gdTitle;
    JLabel wtTitle;
    JButton buttonClose;

    static final int RD_MIN_VALUE = 0;
    static final int RD_MAX_VALUE = 1000;
    static final int RD_INIT_VALUE = 500;

    static final int GD_MIN_VALUE = 0;
    static final int GD_MAX_VALUE = 1000;
    static final int GD_INIT_VALUE = 500;

    public Options() {
        this.setTitle("Settings");

        createView();

        pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }

    private void createView() {
        //Making panels and adding them to window*
        JPanel pOptions = new JPanel();
        this.add(pOptions);

        //These are for the labels that I added so people know which option they are using
        JPanel pOptionLabels = new JPanel(new GridBagLayout());
        pOptions.add(pOptionLabels);

        GridBagConstraints gbc = new GridBagConstraints();

        renderDistance = new JSlider(RD_MIN_VALUE, RD_MAX_VALUE, RD_INIT_VALUE);
        grassDensity = new JSlider(GD_MIN_VALUE, GD_MAX_VALUE, GD_INIT_VALUE);
        waterToggleButton = new JButton("On");

        rdTitle = new JLabel("Render Distance");
        gdTitle = new JLabel("Grass Density");
        wtTitle = new JLabel("Water Terrain Visibility");

                                //Initializing Objects*
                                //Giving objects some attributes using methods*
        renderDistance.setMinorTickSpacing(100);
        renderDistance.setMajorTickSpacing(500);
        renderDistance.setPaintTicks(true);
        renderDistance.setPaintLabels(true);

        grassDensity.setMinorTickSpacing(100);
        grassDensity.setMajorTickSpacing(500);
        grassDensity.setPaintTicks(true);
        grassDensity.setPaintLabels(true);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.insets = new Insets(2, 2, 2, 2);

        waterToggleButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                if (waterToggleButton.getText().equals("Off")) {
                    waterToggleButton.setText("On");
                } else {
                    waterToggleButton.setText("Off");

                }
            }
        });

                                //Giving objects some attributes using methods*
        //Add things to panel ex. p.add();
        pOptionLabels.add(rdTitle, gbc);
        gbc.gridy++;
        pOptionLabels.add(gdTitle, gbc);
        gbc.gridy++;
        pOptionLabels.add(wtTitle, gbc);

        gbc.gridx++;
        gbc.gridy = 0;
        pOptionLabels.add(renderDistance, gbc);
        gbc.gridy++;
        pOptionLabels.add(grassDensity, gbc);
        gbc.gridy++;
        pOptionLabels.add(waterToggleButton, gbc);

    }

    public static void main(String[] args) {
        new Options();

    }

}

答案 1 :(得分:2)

您遇到的问题在技术上是正确的,因为您有两个带有两个独立GridBagLayout管理器实例的JPanel,因此一个布局管理器中的行(和列)没有关于另一个布局管理器中行的信息。

如果您计划在其中使用JComponents并创建一个类似外观的表,那么单个GridBagLayout就是您的选择。您可以稍后添加其他JPanels(使用不同的布局管理器或更多的GridBagLayouts!)但是在您的主GridBagLayout JPanel中。

GridBagLayout是一个非常强大的布局,我建议坚持使用is并掌握它,因为从长远来看它可以帮助你很好地(和快速地)布局。

这是了解有关GridBagLayout的更多信息的绝佳资源:

https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

一个小评论:IMO GridBagLayout不是最好的布局管理器用作" root"布局容器。我建议创建一个" root"带有BorderLayout()的JPanel并将你的GridBagLayout JPanel添加到BorderLayout面板的中心添加(yourGridPanel,BorderLayout.CENTER),因为BorderLayout通常免费提供水平/垂直填充的功能,并且可以让你很好地打包窗口位于中心(可能会在北/南/东/西两侧增加更多东西)。

祝你好运!