将按钮对齐以在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);

我可能会考虑使用的原因BorderLayoutGridLayout的核心布局是它会给所有剩余空间的组件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);
    }

}

但是,如果我想使用BoxLayout进行p2练习。为了使按钮居中,我应该为参数添加什么???

由于容器的尺寸是动态的,因此您可以使用一些水平胶水来代替

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

尝试将TD中的居中对齐按钮

来自分类Dev

使用CSS将按钮中的多行文本居中对齐

来自分类Dev

使用垂直对齐将div中的按钮垂直居中

来自分类Dev

在按钮中垂直居中对齐文本

来自分类Dev

在BoxLayout中对齐JButton

来自分类Dev

将div中的<a>按钮居中

来自分类Dev

将按钮居中于DIV中

来自分类Dev

居中对齐提交按钮

来自分类Dev

无法居中对齐按钮

来自分类Dev

将页脚中的列表对齐到居中

来自分类Dev

将图形与文本右侧对齐并居中以创建按钮-无法将按钮居中

来自分类Dev

BoxLayout中的居中元素

来自分类Dev

BoxLayout中的居中元素

来自分类Dev

将引导程序按钮与随附文本水平居中对齐

来自分类Dev

将多行按钮或标签文本居中对齐吗?

来自分类Dev

将输入按钮的背景图像居中对齐

来自分类Dev

Bootstrap-使用浮动按钮将页面标题居中对齐

来自分类Dev

居中对齐按钮文本在div中动态吗?

来自分类Dev

文本对齐中心不在按钮中居中文本

来自分类Dev

如何居中对齐FlowLayoutPanel中的多个单选按钮?

来自分类Dev

按钮文字与CSS居中对齐

来自分类Dev

带引导的居中对齐按钮

来自分类Dev

无法让这个按钮居中对齐

来自分类Dev

如何将面板中的按钮居中

来自分类Dev

无法将导航栏中的按钮居中

来自分类Dev

如何将面板中的按钮居中

来自分类Dev

无法将导航栏中的按钮居中

来自分类Dev

将facebook图标与按钮中的文本对齐

来自分类Dev

将Div中的按钮对齐到底部