Java GUI-如何将单个按钮居中

Java GUI-如何将单个按钮居中,java,Java,我对java非常陌生(我习惯于python) 注意:即使调整gui的大小,我也希望该位置保持在中间。 我想知道我怎么能把一个按钮放在中间?目前,该按钮位于gui的顶部 public class main_gui extends JFrame{ public static void main(String[] args) { // Initial window JFrame start_frame = new JFrame("P.D");

我对java非常陌生(我习惯于python)

注意:即使调整gui的大小,我也希望该位置保持在中间。

我想知道我怎么能把一个按钮放在中间?目前,该按钮位于gui的顶部

public class main_gui extends JFrame{

    public static void main(String[] args) {

        // Initial window
        JFrame start_frame = new JFrame("P.D");
        start_frame.setSize(1200, 800);
        start_frame.setVisible(true);
        start_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Panel to hold our buttons
        JPanel start_panel = new JPanel();
        start_frame.add(start_panel);


        // Button to initialize everything
        JButton start_button = new JButton("Start");
        // Take out the border around the text
        start_button.setFocusable(false);
        start_panel.add(start_button);
    }
}
这是当前的样子,我只想把这个按钮向下一点,放在中间。


如果要将按钮放在中间,使其占据框架中的整个空间,可以使用类似BorderLayout的布局管理器。因此,您的代码将如下所示:

public class main_gui extends JFrame{

    public static void main(String[] args) {

        // Initial window
        JFrame start_frame = new JFrame("P.D");
        start_frame.setSize(1200, 800);
        start_frame.setVisible(true);
        start_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Panel to hold our buttons
        JPanel start_panel = new JPanel();
        start_panel.setLayout(new BorderLayout());
        start_frame.add(start_panel);


        // Button to initialize everything
        JButton start_button = new JButton("Start");
        // Take out the border around the text
        start_button.setFocusable(false);
        start_panel.add(start_button, BorderLayout.CENTER);
    }
}
您可以不使用布局管理器。这是一种不好的做法,但应该行得通。此代码将在框架中心放置一个小按钮:

public class MainGUI {

    public static void main(String[] args) {
        JFrame start_frame = new JFrame("P.D");
        int width = 1200;
        int height = 800;
        start_frame.setSize(width, height);
        start_frame.setVisible(true);
        start_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Panel to hold our buttons
        JPanel start_panel = new JPanel();
        start_panel.setLayout(null);
        start_frame.add(start_panel);


        // Button to initialize everything
        JButton start_button = new JButton("Start");
        buttonWidth = 80;
        buttonHeight = 20;
        start_button.setBounds(new Rectangle((width - buttonWidth)/2, (height - buttonHeight)/2, buttonWidth, buttonHeight));
        start_button.setSize(new Dimension(buttonWidth, buttonHeight));
        start_button.setFocusable(false);
        start_panel.add(start_button);
    }
}

快速解决方案是将FlowLayout组件之间的垂直间隙设置为JFrame大小的一半:

public class MainGUI {

    static java.awt.Dimension bd;

    public static void main(String[] args) {

        // Initial window
        JFrame start_frame = new JFrame("P.D");
        start_frame.setSize(1200, 800);
        start_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Button to initialize everything
        JButton start_button = new JButton("Start");
        bd = start_button.getPreferredSize();
        // Take out the border around the text
        start_button.setFocusable(false);        

        // Panel to hold our buttons
        java.awt.Dimension d = start_frame.getSize();
        JPanel start_panel = new JPanel(new java.awt.FlowLayout(FlowLayout.CENTER, 0, d.height / 2 - bd.height / 2));
        start_panel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent evt) {
                JPanel c = (JPanel) evt.getSource();
                c.setLayout(new java.awt.FlowLayout(FlowLayout.CENTER, 0, c.getSize().height / 2 - bd.height / 2));
            }
        });

        start_panel.add(start_button);
        start_frame.add(start_panel);  
        start_frame.setVisible(true);
    }
}
当JFrame的大小更改时,ComponentAdapter将重新计算新高度并将按钮放置到新的中心

为了将按钮放置在垂直中心,我们计算按钮的高度并从垂直间隙中减去一半

水平中心由布局自动应用

如果在类中实例化另一个JFrame并使用它,则该类不应扩展JFrame

类名应该是名词,大小写混合,每个内部单词的第一个字母大写

建议在将所有小部件添加到JFrame后使其可见

为了使用SwingUtilities调用程序并使应用程序代码更干净,请让您的类扩展JFrame,编写构造函数并以这种方式调用它:

public class MainGUIJFrame extends JFrame {

    public MainGUIJFrame() {
        initComponents();
    }

    private void initComponents() {
        setTitle("P.D");
        setSize(1200, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        start_button = new JButton("Start");
        bd = start_button.getPreferredSize();
        // Take out the border around the text
        start_button.setFocusable(false);

        java.awt.Dimension d = getSize();
        start_panel = new JPanel(new java.awt.FlowLayout(FlowLayout.CENTER, 0, d.height / 2 - bd.height / 2));
        start_panel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent evt) {
                JPanel c = (JPanel) evt.getSource();
                c.setLayout(new java.awt.FlowLayout(FlowLayout.CENTER, 0, c.getSize().height / 2 - bd.height / 2));
            }
        });

        start_panel.add(start_button);
        add(start_panel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainGUIJFrame().setVisible(true);
            }
        });
    }

    private JButton start_button;
    private JPanel start_panel;

    private java.awt.Dimension bd;
}

这样可以使main保持简单和小,而bd不必再是静态的

最好的方法是使用您选择的布局,并尝试使用该布局使其工作

但是,如果您确信您的窗口不会被调整大小,或者您愿意自己处理此类事件,您可以尝试手动操作并定位按钮,例如:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Dimension;

public class main_gui {
    public static void main(String[] args) {

        // Initial window
        JFrame start_frame = new JFrame("P.D");
        int FrameWidth = 1200, FrameHeight = 800;
        start_frame.setSize(FrameWidth, FrameHeight);
        start_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Panel to hold our buttons
        JPanel start_panel = new JPanel();
        start_panel.setLayout(null);  // Now working without a layout manager
                                      // i.e, can position things manually.
        start_frame.add(start_panel);

        // Button to initialize everything
        JButton start_button = new JButton("Start");
        start_button.setFocusable(false); // Take out the border around the text
        Dimension size = start_button.getPreferredSize();
        start_button.setBounds( FrameWidth/2  - size.width/2,
                                FrameHeight/2 - size.height/2,
                                size.width,     size.height);
        start_panel.add(start_button);

        // Display the Layout after all components have been added.
        // (adding components after the frame has been set to visible
        //  may result in components not showing up reliably!)
        start_frame.setVisible(true);
    }
}

没有必要安装面板。只需将按钮直接添加到框架中

最简单的方法是使用
GridBagLayout

frame.setLayout( new GridBagLayout() );
frame.add(startButton, new GridBagConstraints());

默认情况下,组件将在
GridBagLayout
内水平和垂直居中,我不希望它占据整个空间,我希望它保持当前的大小。有什么方法可以做到这一点吗?不确定它是否会工作,但您可以尝试类似start_button.setPreferredSize(新维度(x,y))的方法。它不会工作,因为我给它尺寸,我希望它采用框架的尺寸,并将面板/按钮设置在它的中心。现在,它应该可以按照您的要求工作了。您可以更改buttonWidth和buttonHeight以获得您更喜欢的按钮大小。但是,当您最大化窗口时,它不会改变位置,它只是保持不变。不过,我想调整它的大小,我如何使用布局来实现这一点。您确实需要熟悉不同的布局,以及他们各自的专长和业务。我想您可能需要
BorderLayout
,或者
BoxLayout
,这取决于您在窗口中还需要什么?只需使用一些布局即可。很有趣:嗯。。当它运行并调整大小时,它似乎居中,但当您放大窗口,然后再次调整窗口大小时,位置低于中心位置。现在位置应该可以了。我想如果您使用传统的类命名样式,它也应该是camelcase。无论如何,回答得不错;遗憾的是,它像其他人一样被扣为人质:皮明吉是卡梅隆案。Thanx Tasos@CostisAivalis mm同样的问题仍然存在,我假设变量是维度bd,对吗?它在文件中未指定code@camickr:极好的解决方案!到目前为止,我们一直使用GridBagLayout来管理包含许多组件的复杂和巨大表单。@CostisAivalis,它是GridBagLayout的一个特性。如果没有一个组件具有权重X/y值(默认约束值为0),那么组件会聚集在中心。哇!非常感谢!