如何在垂直线上将按钮放在框架的中心?

时间:2014-04-08 09:54:10

标签: java swing button center

我会将这些按钮放在框架的中央并且彼此上方,就像这样。

                                     BUTTON
                                     BUTTON
                                     BUTTON

我在这个论坛上搜索了多个主题但是我尝试过的所有内容到目前为止都没有用。我希望有人有解决方案。

这是我目前的代码:

package ípsen1;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Paneel extends JPanel implements ActionListener {
Image achtergrond;
private JButton spelHervatten;
private JButton spelOpslaan;
private JButton spelAfsluiten;


public Paneel(){
    //buttons   
    spelHervatten = new JButton("Spel hervatten");
    spelHervatten.setPreferredSize(new Dimension(380, 65));

    spelOpslaan = new JButton("Spel opslaan");
    spelOpslaan.setPreferredSize(new Dimension(380, 65));

    spelAfsluiten = new JButton("Spel afsluiten");  
    spelAfsluiten.setPreferredSize(new Dimension(380, 65));


    //object Paneel luistert naar button events
    spelAfsluiten.addActionListener(this);

    add (spelHervatten);
    add (spelOpslaan);
    add (spelAfsluiten);
    }

public void paintComponent(Graphics g) {
    //achtergrond afbeelding zetten
    achtergrond = Toolkit.getDefaultToolkit().getImage("hout.jpg");
    //screensize
    g.drawImage(achtergrond, 0,0, 1024,768,this);
}

    //actie na klik op button
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == spelAfsluiten){
                    System.out.println("Spel afsluiten");
                    System.exit(0);
                    }
                }
            }

5 个答案:

答案 0 :(得分:6)

您可以使用GridBagLayout

Buttons

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;

add(new JButton("Button"), gbc);
add(new JButton("Button"), gbc);
add(new JButton("Button"), gbc);
add(new JButton("Button"), gbc);

有关详细信息,请参阅How to Use GridBagLayout

答案 1 :(得分:1)

BoxLayout可能是你所追求的。您可以指定要在该特定布局管理器的构造函数中沿y轴添加组件。

您可以将此行添加到Paneel类的构造函数中。

this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

至于中心对齐所有内容,我不知道这是不是很好,但你可以单独设置每个按钮的水平对齐方式。例如:

spelHervatten.setAlignmentX(CENTER_ALIGNMENT);

答案 2 :(得分:1)

对一列宽度相等的按钮使用GridLayout

随着窗口大小的增加,按钮会拉伸。要维护按钮大小,请将GridLayout作为单个组件放入GridBagLayout,不受任何限制。它会集中。

enter image description here

通过设置边距来增加按钮的大小。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

/*
 * Uses a GridLayout for a single column of buttons of equal width.
 * The buttons stretch as the window's size increases.  To maintain
 * the button size, put the GridLayout as a single component into a 
 * GridBagLayout with no constraint.  It will be centered.
 */
public class CenteredSingleColumnOfButtons {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new GridLayout(0,1,10,10));
                gui.setBorder(new EmptyBorder(20,30,20,30));

                String[] buttonLabels = {
                    "Spel hervatten",
                    "Spel opslaan",
                    "Spel afsluiten"
                };

                Insets margin = new Insets(20,150,20,150);
                JButton b = null;
                for (String s : buttonLabels) {
                    b = new JButton(s);
                    b.setMargin(margin);
                    gui.add(b);
                }

                JFrame f = new JFrame("Centered Single Column of Buttons");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                f.setMinimumSize(f.getSize());
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
                System.out.println(b.getSize());
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

答案 3 :(得分:0)

我认为没有办法做到这一点。

您应该获得面板/框架的大小,然后手动计算以找到按钮的中心位置。

答案 4 :(得分:0)

改写一些部分:

您可能想尝试将按钮放在JFrame"风向" -style BorderLayout中: http://www.leepoint.net/notes-java/GUI/layouts/20borderlayout.html

只需在CENTER中创建一个具有一个EASTWEST块的块,其周围有一定的大小。然后将按钮插入中心块内。如果您不希望它们是完整尺寸,只需添加另一个EASTWEST