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

时间:2014-04-10 17:52:19

标签: java swing layout jbutton center

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

您可以使用面板尺寸和按钮尺寸,如..

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

答案 2 :(得分:0)

如果你想使用setBounds方法进行对齐,那么使用screenSize就可以了,就像这里一样

Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
    int width=screenSize.width;
    int height=screenSize.height;

并像参考屏幕尺寸的宽度和高度一样排列x和y坐标。 Braj告诉我们,良好的编程应该使用布局。

相关问题