Java 在BoxLayout中将按钮对齐到中心
我正在尝试使用Java 在BoxLayout中将按钮对齐到中心,java,swing,layout-manager,boxlayout,Java,Swing,Layout Manager,Boxlayout,我正在尝试使用Box.createHorizontalStruct()方法将按钮设置到中间。但是,如果我使用这个.getWidth()/2是不起作用的。我怎样才能使它在框架中居中 代码 包装ch17; 导入java.awt.Color; 导入java.awt.Container; 导入java.awt.FontMetrics; 导入java.awt.Graphics; 导入java.awt.GridLayout; 导入java.awt.event.ActionEvent; 导入java.awt.
Box.createHorizontalStruct()
方法将按钮设置到中间。但是,如果我使用这个.getWidth()/2
是不起作用的。我怎样才能使它在框架中居中
代码
包装ch17;
导入java.awt.Color;
导入java.awt.Container;
导入java.awt.FontMetrics;
导入java.awt.Graphics;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
导入javax.swing.border.TitledBorder;
公共类Q17_1扩展JFrame{
JButton left=新JButton(“”);
JPanel p1=新的JPanel();
JRadioButton rb1=新的JRadioButton(“红色”);
JRadioButton rb2=新的JRadioButton(“黄色”);
JRadioButton rb3=新的JRadioButton(“白色”);
JRadioButton rb4=新的JRadioButton(“灰色”);
JRadioButton rb5=新的JRadioButton(“绿色”);
JPanel p2=新的JPanel();
消息m=新消息(“欢迎使用Java”);
公共问题17_1(){
setLayout(新网格布局(3,1));
p1.设置顺序(新标题边框(“选择消息面板背景”);
ButtonGroup=新建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.设置记忆法('N');
p1.设置布局(新的网格布局(1,5,5));
p1.添加(rb1);p1.添加(rb2);p1.添加(rb3);p1.添加(rb4);p1.添加(rb5);
p2.设置布局(新的BoxLayout(p2,BoxLayout.X_轴));
添加(p1);
加(m);
p2.增加(框.创建水平支柱(250));
p2.添加(左);
p2.增加(框。创建水平支柱(5));
p2.添加(右);
添加(p2);
左。addActionListener((ActionEvent)->{
m、 左移();
重新油漆();
});
右。addActionListener((ActionEvent)->{
m、 moveRight();
重新油漆();
});
rb1.addActionListener(m);
rb2.addActionListener(m);
rb3.addActionListener(m);
rb4.addActionListener(m);
rb5.addActionListener(m);
}
公共静态void main(字符串[]args){
Q17_1框架=新Q17_1();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
}
我试过,
this.getWidth()/2
,p2.getWidth()/2
,等等。但是它们不起作用,按钮仍然从左侧的开始处开始。您可以使用边框布局
,流程布局
或网格布局
总结如下:
setLayout(new BorderLayout());
//...
p2.setLayout(new FlowLayout());
add(p1, BorderLayout.NORTH);
add(m);
//...
add(p2, BorderLayout.SOUTH);
<> >我可以考虑使用<代码> BooDrays <代码> GridLayout <代码>为核心布局,它将将所有剩余空间赋予<<代码>中心< /代码>位置中的组件。这可能不是你想要的,但这就是我使用它的原因
GridBagLayout
和FlowLayout
都将围绕容器中心布置容器,GridBagLayout
垂直和水平操作,FlowLayout
仅水平操作(默认情况下)
使用
BorderLayout
和FlowLayout
的组合,但是如果我想使用BoxLayout来练习p2。为了使按钮居中,我应该为参数设置什么?那时我想练习boxlayout。但是,当试图获得面板的宽度时,它不起作用。面板的宽度
在布局之前是不知道的。你有一个第二十二条军规的问题。相反,对容器应用动态填充,使其能够占用计算组件空间后剩余的空间。记住,任何容器的大小不仅取决于它的内容,还取决于它在父容器中的布局……尽量避免在绝对容器中工作,它总是会烧坏你对不起,绝对是什么意思?那么我应该什么时候使用BoxLayout???另外,如果我要使用boxLayout并想执行以下操作,我应该怎么做?按钮1按钮2
setLayout(new BorderLayout());
//...
p2.setLayout(new FlowLayout());
add(p1, BorderLayout.NORTH);
add(m);
//...
add(p2, BorderLayout.SOUTH);
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);
}
}
p2.add(Box.createHorizontalGlue());
p2.add(left);
p2.add(right);
p2.add(Box.createHorizontalGlue());