Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用GridBagLayout调整Java Swing组件大小_Java_Swing_Layout Manager_Gridbaglayout - Fatal编程技术网

使用GridBagLayout调整Java Swing组件大小

使用GridBagLayout调整Java Swing组件大小,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我的JavaSwingGUI一直有问题。首先,我创建了一个带有GridBagLayout的面板,并将所有标签添加到其中。然而,我还在另一个JPanel的右侧创建了一个面板,其中添加了一个按钮和两个滑块,它们应该与标签匹配 问题是JLabel比另一个面板右侧的组件小,这使得它看起来像这样 例如水选项--JSLIDER(JSLIDER看起来要大得多) 我试图通过将ipadx添加到一个更大的值来使组件更大,并且我还尝试将标签面板的网格宽度设置得更大,但似乎没有任何效果。它就是没有反应 代码如下: pa

我的JavaSwingGUI一直有问题。首先,我创建了一个带有GridBagLayout的面板,并将所有标签添加到其中。然而,我还在另一个JPanel的右侧创建了一个面板,其中添加了一个按钮和两个滑块,它们应该与标签匹配

问题是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();

        }




                }

改变你的思维方式。不要试图在单独的容器中布置所有的标签和所有的“其他”组件,而是把它们放在同一个容器中。

这样,当布局它们时,布局计算将与单个上下文中的所有组件相关


改变你的思维方式。不要试图在单独的容器中布置所有的标签和所有的“其他”组件,而是把它们放在同一个容器中。

这样,当布局它们时,布局计算将与单个上下文中的所有组件相关


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

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

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

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

一个小评论:在我看来,GridBagLayout并不是用作“根”布局容器的最佳布局管理器。相反,我建议创建一个带有BorderLayout()的“根”JPanel,并将GridBagLayout JPanel添加到BorderLayout面板add(yourGridPanel,BorderLayout.center)的中心,因为BorderLayout通常免费提供水平/垂直填充的功能,并且可以让您在中心很好地填充窗口(以后可能会在北/南/东/西两侧添加更多内容)


祝您好运!

您所经历的在技术上是正确的,因为您有两个JPanel,其中有两个GridBagLayout manager的独立实例,因此一个布局管理器中的行(和列)没有关于另一个布局管理器中的行的信息

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

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

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

一个小评论:在我看来,GridBagLayout并不是用作“根”布局容器的最佳布局管理器。相反,我建议使用BorderLayout()创建一个“根”JPanel,并将GridBagLayout JPanel添加到BorderLayout面板add(yourGridPanel,BorderLayout.center)的中心因为BorderLayout通常免费提供东西水平/垂直填充的功能,并且可以让您将窗口很好地放在中心位置(稍后可以在北/南/东/西侧添加更多东西)


祝你好运!

跨组件布局管理是…一场噩梦。我听说像
MigLayout
这样的东西会让它更容易,但我从未尝试过。改变你的思维方式,布局每个标签和滑块,使它们相互关联,如果需要,在它们自己的容器中。我应该只有一个gridbaglayout吗?我想知道你的具体情况如果是这样的话,那就是我的建议。每个布局都应该关注单个部分的需求,然后你可以使用多个布局合成多个容器来生成更复杂的布局谢谢你的建议!我正在使用JavierJ的建议,将根面板设为边界布局,所有组件都位于中间,这使得它好极了!我喜欢这包;您使用的方法,我现在也在使用。跨组件布局管理是一场噩梦。我听说像
MigLayout
这样的东西使它更容易,但我从未尝试过。改变您的思维方式,布局每个标签和滑块,使它们相互关联,如果需要,在它们自己的容器中。我应该只有一个网格吗baglayout?依我看,这是我的建议。每个布局都应该关注单个部分的需求,然后你可以使用多个布局合成多个容器以生成更复杂的布局谢谢!我正在使用JavierJ的建议,将根面板设为borderlayout,所有组件到中间去,让它看起来很甜!我喜欢这个包();你使用的方法,我现在也在使用。哇,谢谢你的回答,我喜欢这个例子和代码!我不会只是复制和粘贴这个代码,我会确切地了解你是怎么做到的,谢谢你的帮助!哇,谢谢你的回答,我喜欢这个例子和代码!我不会只是复制和粘贴这个代码,我会准确地学习是你做的吗,谢谢你帮助我!谢谢你的回答!我哈
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();

    }

}