Java 如何将JButtons(主菜单)居中放置在框架中?

Java 如何将JButtons(主菜单)居中放置在框架中?,java,swing,layout,jbutton,center,Java,Swing,Layout,Jbutton,Center,我试着使用:“button1.setAlignmentX(Component.CENTER_ALIGNMENT);”但效果不太好。有没有办法让我的按钮自动居中 外观:使用GridLayout import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import ja

我试着使用:“button1.setAlignmentX(Component.CENTER_ALIGNMENT);”但效果不太好。有没有办法让我的按钮自动居中


外观:

使用
GridLayout

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@SuppressWarnings("serial")
public class ButtonLocationDemo extends JFrame implements ActionListener{
  /// Create variables
  private JButton button;
  private JButton button1;
  private JButton button2;
  private JButton button3;
  private JButton button4;
  // private BufferedImage image;

  public ButtonLocationDemo(){
    // button1.setForeground(Color.WHITE);

    // Add buttons
    JPanel p = new JPanel();
    button = new JButton("Doorgaan");
    button1 = new JButton("Opslaan");
    button2 = new JButton("Spelregels");
    button3 = new JButton("Naar hoofdmenu");
    button4 = new JButton("Afsluiten");

    button1.setAlignmentX(Component.CENTER_ALIGNMENT);

    button1.setBackground(Color.black);
    button1.setForeground(Color.white);
    button1.setBorder(null);
    button1.setBounds(250,150,100,50);

    button.setBounds(250,50,100,50);
    button.setBackground(Color.black);
    button.setForeground(Color.white);
    button.setBorder(null);

    button2.setBounds(250,250,100,50);
    button2.setBackground(Color.black);
    button2.setForeground(Color.white);
    button2.setBorder(null);

    button3.setBounds(250,350,150,50);
    button3.setBackground(Color.black);
    button3.setForeground(Color.white);
    button3.setBorder(null);

    button4.setBounds(250,450,100,50);
    button4.setBackground(Color.black);
    button4.setForeground(Color.white);
    button4.setBorder(null);

    // Add buttons
    p.add(button);
    p.add(button1);
    p.add(button2);
    p.add(button3);
    p.add(button4);
    p.setBackground(Color.black);
    p.setLayout(null);

    getContentPane().add(p);
    //setLayout(null);
    setDefaultCloseOperation(3);
    setSize(720,720);
    setVisible(true);

    // Add actionListeners
    button.addActionListener(this);
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
  }

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

  // Action performer
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource()==this.button){
      System.out.println("Doorgaan");
    }
    if (e.getSource()==this.button1){
      System.out.println("Opslaan");    
    }
    if (e.getSource()==this.button2){
      System.out.println("Spelregels");  
    }
    if (e.getSource()==this.button3){
      System.out.println("Naar hoofdmenu");  
    }
    if (e.getSource()==this.button4){
      System.out.println("Afsluiten");  
      System.exit(0);  
    }    
  }
}
并删除所有
setBounds()
方法调用

只需将其交给布局管理器,以管理组件的位置。不要使用
null
布局


--编辑--

您也可以尝试
GridBagLayout

p.setLayout(new GridLayout(5,1));

您可以使用面板大小和按钮大小,如

p.setLayout(new GridBagLayout());
GridBagConstraints gc=new GridBagConstraints();

gc.gridx=0;
gc.weightx=0.4;
gc.insets=new Insets(5, 5, 5, 5);
//gc.anchor=GridBagConstants.NORTH;


// Add buttons
gc.gridy=0;
p.add(button,gc);
gc.gridy=1;
p.add(button1,gc);
gc.gridy=2;
p.add(button2,gc);
gc.gridy=3;
p.add(button3,gc);
gc.gridy=4;
p.add(button4,gc);

如果您想使用setBounds方法进行对齐,那么使用screenSize就可以了

button1.setSize(100,50);
button1.setPosition(p.getSize().getWidth()/2 - button1.getSize().getWidth()/2, 150);

并按照您希望的方式,根据屏幕大小的宽度和高度来排列x和y坐标。好的编程应该使用Braj告诉的布局

+1,不要硬编码布局。你想到的任何数字,都有一个合法的配置会破坏它。布局管理器可以做得很好,我不认为我需要手动设置任何组件的边界、位置或对齐方式。我尝试了你的第一种方法。它可以做到这一点,但会使按钮变得巨大。然后我尝试了你的第二种方法,效果很好!非常感谢!很好,我知道这就是我建议第二种选择的原因。一个小问题:如果我将字体改大一点,它会自动缩放吗?请阅读这里
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
    int width=screenSize.width;
    int height=screenSize.height;