在Java中,有没有一种方法可以使一组大小可变的按钮居中(使用Swing)?

在Java中,有没有一种方法可以使一组大小可变的按钮居中(使用Swing)?,java,swing,user-interface,button,layout,Java,Swing,User Interface,Button,Layout,我一直在尝试制作这样的GUI: 然而,我遇到的困难是,我需要使按钮的文本可变-每个按钮都应该提供不同的选项,这些选项的文本长度可能不同。虽然这本身并不困难,但我尝试了无数不同的方法,但我无法让按钮居中,尽管它们的文本长度。无论我尝试什么,我总是遇到以下问题之一: 按钮不居中(左侧有空间,但超出了右侧的窗口) 出于某种原因,问题和Goodratio会根据按钮大小更改位置 我不知道该怎么做。有没有人对什么是最好的方法有一些有用的直觉?非常感谢 编辑:构造函数的代码(未正确居中),我使用该代码根

我一直在尝试制作这样的GUI:

然而,我遇到的困难是,我需要使按钮的文本可变-每个按钮都应该提供不同的选项,这些选项的文本长度可能不同。虽然这本身并不困难,但我尝试了无数不同的方法,但我无法让按钮居中,尽管它们的文本长度。无论我尝试什么,我总是遇到以下问题之一:

  • 按钮不居中(左侧有空间,但超出了右侧的窗口)
  • 出于某种原因,问题和Goodratio会根据按钮大小更改位置
我不知道该怎么做。有没有人对什么是最好的方法有一些有用的直觉?非常感谢

编辑:构造函数的代码(未正确居中),我使用该代码根据请求设置组件:

    public ButtonPannel(Test test)
{
    super();

    op1Button = new JButton("Option 1");
    op2Button = new JButton("Option 2");
    op3Button = new JButton("Option 3");
    questionText = new JLabel("Question");
    rightAndWrongAmount = new JLabel("rightAndWrongAmount");

    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{30, 331, 0};
    gridBagLayout.rowHeights = new int[]{16, 0, 134, 35, 16, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
    setLayout(gridBagLayout);

    JPanel panel = new JPanel();
    FlowLayout flowLayout = (FlowLayout) panel.getLayout();

    op1Button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });

    GridBagConstraints gbc_lblQuestion = new GridBagConstraints();
    gbc_lblQuestion.insets = new Insets(0, 0, 5, 0);
    gbc_lblQuestion.gridx = 1;
    gbc_lblQuestion.gridy = 1;
    add(questionText, gbc_lblQuestion);
    panel.add(op1Button);

    panel.add(op2Button);

    panel.add(op3Button);
    GridBagConstraints gbc_panel = new GridBagConstraints();
    gbc_panel.insets = new Insets(0, 0, 5, 0);
    gbc_panel.gridx = 1;
    gbc_panel.gridy = 3;
    add(panel, gbc_panel);

    GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
    gbc_lblNewLabel_1.gridx = 1;
    gbc_lblNewLabel_1.gridy = 4;

    add(rightAndWrongAmount, gbc_lblNewLabel_1);

    op1Button.addActionListener(new InputHandler(test));
    op1Button.setActionCommand("1");
    op2Button.addActionListener(new InputHandler(test));
    op2Button.setActionCommand("2");
    op3Button.addActionListener(new InputHandler(test));
    op3Button.setActionCommand("3");

}

代码是使用WindowBuilder生成的。问题不在于代码,而在于使用哪种布局等等。

布局有点像黑魔法和反复试验。很少有一个单一的布局能达到你想要的,所以你经常需要求助于复合布局(使用多个容器和布局)来获得你想要的

因此,基本上,这是由一个面板组成的,另一个面板持有按钮(另一个面板充当“伪”内容以增加空间)

“但我希望按钮大小相同”我听到你问,当然,改变布局


您必须查看布局,考虑每个元素与其他元素之间的关系,以及您希望它们做什么,然后找到布局管理器,这些管理器将帮助您实现最终结果,并将它们组合在一起。

布局有点像黑魔法,需要反复尝试。很少有一个单一的布局能达到你想要的,所以你经常需要求助于复合布局(使用多个容器和布局)来获得你想要的

因此,基本上,这是由一个面板组成的,另一个面板持有按钮(另一个面板充当“伪”内容以增加空间)

“但我希望按钮大小相同”我听到你问,当然,改变布局


您必须查看布局,考虑每个元素与其他元素之间的关系,以及您希望它们做什么,然后找到布局管理器,这些布局管理器将帮助您实现最终结果,并将它们组合在一起

Java有大量的布局管理器;当然,有一个支持这种设置。因此,只需学习相应的教程;当你有一些真正的代码“几乎”做到了这一点;请随便问一下。因此,请转到此处或首先确定哪个管理器适合您。@GhostCat根据请求添加了代码。把代码放在首位是因为我对做这件事所需要的工具比对它的实际实现更感兴趣;但是试着把它变成一个-这样其他人就可以轻松地运行了。按钮的大小应该相同吗?我认为如果它们是平衡的(并且在布局和代码上有所不同),它会看起来更“平衡”;当然,有一个支持这种设置。因此,只需学习相应的教程;当你有一些真正的代码“几乎”做到了这一点;请随便问一下。因此,请转到此处或首先确定哪个管理器适合您。@GhostCat根据请求添加了代码。把代码放在首位是因为我对做这件事所需要的工具比对它的实际实现更感兴趣;但是试着把它变成一个-这样其他人就可以轻松地运行了。按钮的大小应该相同吗?我认为如果它们是平衡的(并且在布局和代码上有所不同),它会看起来更“平衡”。我:“按钮应该大小相等吗?”Pfft。。炫耀@AndrewThompson每个人在这个问题上都有不同的感受,我相信我已经和设计师和其他类似事物的开发者打过几场战争;)你试过WindowBuilder吗?我总是喜欢使用它,因为它使一切变得更容易(布局明智)@lilienfa我尝试过许多表单生成器,并倾向于发现手工构建it UI最终更容易,并且总体上产生更好/更干净的结果。我鼓励我的所有初级开发人员学习手工编写他们的UI,因为它可以让您更好地理解布局的工作原理以及如何将需求分解为独立的工作单元,这有助于减少耦合并简化总体解决方案和工作流程。一旦你知道如何手工操作,那么公司的编辑就成了一个工具,可以完成最初的流程quicker@MadProgrammer我喜欢你的方法,这是真的:)只是觉得使用它会更容易(?)不知道它是否更容易。。。但是要快一点:“按钮大小应该相等吗?”Pfft。。炫耀@AndrewThompson每个人在这个问题上都有不同的感受,我相信我已经和设计师和其他类似事物的开发者打过几场战争;)你试过WindowBuilder吗?我总是喜欢使用它,因为它使一切变得更容易(布局明智)@lilienfa我尝试过许多表单生成器,并倾向于发现手工构建it UI最终更容易,并且总体上产生更好/更干净的结果。我鼓励我的所有初级开发人员学习手工编写他们的UI,因为它可以让您更好地理解布局的工作原理以及如何将需求分解为独立的工作单元,这有助于减少耦合并简化总体解决方案和工作流程。一旦你知道如何手工操作,那么公司的编辑就成了一个工具,可以完成最初的流程quicker@MadProgrammer我喜欢你的方法,这是真的:)只是觉得使用它会更容易(?)不知道它是否更容易。。。B
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(new JLabel("Question"), gbc);
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.weighty = 1;
            // This is just to act as some psudo context
            add(new FillerPane(), gbc);
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0;
            gbc.weighty = 0;
            add(makeButtonPane(), gbc);
            add(new JLabel("Goodratio"), gbc);
        }

        public JPanel makeButtonPane() { 
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            panel.add(new JButton("Short"));
            panel.add(new JButton("Long, long, long and lobng"));
            panel.add(new JButton("In the middle"));
            return panel;
        }

    }

    public class FillerPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(new JLabel("Question"), gbc);
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.weighty = 1;
            // This is just to act as some psudo context
            add(new FillerPane(), gbc);
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0;
            gbc.weighty = 0;
            add(makeButtonPane(), gbc);
            add(new JLabel("Goodratio"), gbc);
        }

        public JPanel makeButtonPane() { 
            JPanel panel = new JPanel(new GridLayout(1, 0));
            panel.add(new JButton("Short"));
            panel.add(new JButton("Long, long, long and lobng"));
            panel.add(new JButton("In the middle"));
            return panel;
        }

    }

    public class FillerPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}