在 BoxLayout 中将按钮对齐到中心

Aligning buttons to center in BoxLayout

我正在尝试使用 Box.createHorizontalStrut() 方法将按钮设置到中心。但是,如果我使用 this.getWidth()/2 是行不通的。我怎样才能使它在框架中居中。

代码

package ch17;
import java.awt.Color;
import java.awt.Container;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class Q17_1 extends JFrame{
    JButton left = new JButton("<=");
    JButton right = new JButton("=>");
    JPanel p1 = new JPanel();
    JRadioButton rb1 = new JRadioButton("Red");
    JRadioButton rb2 = new JRadioButton("Yellow");
    JRadioButton rb3 = new JRadioButton("White");
    JRadioButton rb4 = new JRadioButton("Gray");
    JRadioButton rb5 = new JRadioButton("Green");
    JPanel p2 = new JPanel();
    Message m = new Message("Welcome to Java");
    public Q17_1(){
        setLayout(new GridLayout(3,1));
        p1.setBorder(new TitledBorder("Select Message Panel Background"));
        ButtonGroup group = new ButtonGroup();
        group.add(rb1);group.add(rb2);group.add(rb3);group.add(rb4);group.add(rb5);
        rb1.setMnemonic('R');rb2.setMnemonic('Y');rb3.setMnemonic('W');rb4.setMnemonic('G');
        rb5.setMnemonic('N');
        p1.setLayout(new GridLayout(1,5,5,5));
        p1.add(rb1);p1.add(rb2);p1.add(rb3);p1.add(rb4);p1.add(rb5);
        p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
        add(p1);
        add(m);
        p2.add(Box.createHorizontalStrut(250));
        p2.add(left); 
        p2.add(Box.createHorizontalStrut(5));
        p2.add(right); 

        add(p2);
        
        left.addActionListener((ActionEvent) -> {
            m.moveLeft();
            repaint();
        });
        
        right.addActionListener((ActionEvent)-> {
            m.moveRight();
            repaint();
        });
        
        rb1.addActionListener(m);
        rb2.addActionListener(m);
        rb3.addActionListener(m);
        rb4.addActionListener(m);
        rb5.addActionListener(m);
    }
    
    public static void main(String[] args) {
        Q17_1 frame = new Q17_1();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
  
}

我试过了,this.getWidth()/2p2.getWidth()/2,等等。但是它们不起作用,按钮仍然从左侧开始。

您可以使用 BorderlayoutFlowLayoutGridBagLayout

的组合

摘要:

setLayout(new BorderLayout());
//...
p2.setLayout(new FlowLayout());
add(p1, BorderLayout.NORTH);
add(m);
//...
add(p2, BorderLayout.SOUTH);

我可能会考虑使用 BorderLayout of GridLayout 作为核心布局的原因是它会将所有剩余的 space 分配给 CENTER 位置的组件。这可能不是您想要的,但这就是我使用它的原因。

GridBagLayoutFlowLayout 都将其容器围绕容器的中心进行布局,GridBagLayout 垂直和水平进行,FlowLayout 仅水平进行(通过默认)

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

public class Q17_1 extends JFrame {

    JButton left = new JButton("<=");
    JButton right = new JButton("=>");
    JPanel p1 = new JPanel();
    JRadioButton rb1 = new JRadioButton("Red");
    JRadioButton rb2 = new JRadioButton("Yellow");
    JRadioButton rb3 = new JRadioButton("White");
    JRadioButton rb4 = new JRadioButton("Gray");
    JRadioButton rb5 = new JRadioButton("Green");
    JPanel p2 = new JPanel();
    JLabel m = new JLabel("Welcome to Java");
//  Message m = new Message("Welcome to Java");

    public Q17_1() {
        setLayout(new BorderLayout());

        p1.setBorder(new TitledBorder("Select Message Panel Background"));
        ButtonGroup group = new ButtonGroup();
        group.add(rb1);
        group.add(rb2);
        group.add(rb3);
        group.add(rb4);
        group.add(rb5);
        rb1.setMnemonic('R');
        rb2.setMnemonic('Y');
        rb3.setMnemonic('W');
        rb4.setMnemonic('G');
        rb5.setMnemonic('N');

        p1.setLayout(new GridLayout(1, 5, 5, 5));
        p1.add(rb1);
        p1.add(rb2);
        p1.add(rb3);
        p1.add(rb4);
        p1.add(rb5);

        p2.setLayout(new FlowLayout());
        add(p1, BorderLayout.NORTH);
        add(m);
        p2.add(left);
        p2.add(right);

        add(p2, BorderLayout.SOUTH);

        left.addActionListener((ActionEvent) -> {
//          m.moveLeft();
//          repaint();
        });

        right.addActionListener((ActionEvent) -> {
//          m.moveRight();
//          repaint();
        });

//      rb1.addActionListener(m);
//      rb2.addActionListener(m);
//      rb3.addActionListener(m);
//      rb4.addActionListener(m);
//      rb5.addActionListener(m);
    }

    public static void main(String[] args) {
        Q17_1 frame = new Q17_1();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

But If I want to use BoxLayout for practice for p2. What should I put for the arguments in order to center the buttons???

由于容器的大小是动态的,您可以使用一些水平胶水代替

    p2.add(Box.createHorizontalGlue());
    p2.add(left);
    p2.add(right);
    p2.add(Box.createHorizontalGlue());