gpt4 book ai didi

java - 如何使 JButtons(主菜单)在框架中居中?

转载 作者:行者123 更新时间:2023-11-30 03:56:43 25 4
gpt4 key购买 nike

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);
}
}
}

我尝试使用:“button1.setAlignmentX(Component.CENTER_ALIGNMENT);”但效果并不好。有什么办法可以让我的按钮自动居中吗?

外观:http://imgur.com/xuca5X5

最佳答案

使用GridLayout

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

并删除所有 setBounds() 方法调用。

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

<小时/>

--编辑--

您也可以尝试GridBagLayout

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);

关于java - 如何使 JButtons(主菜单)在框架中居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22995468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com