Java 如何在具有GridLayout的JPanel中居中对齐所有内容
最近,我讨论了如何使Java 如何在具有GridLayout的JPanel中居中对齐所有内容,java,swing,jframe,jpanel,grid-layout,Java,Swing,Jframe,Jpanel,Grid Layout,最近,我讨论了如何使JPanels在JFrame中从上到下对齐。用户告诉我使用GridLayout()。我让一切正常,但我忘了提到我想将JPanels中的所有组件居中对齐 我的第一个JPanel有一个JLabel 我的第二个JPanel有5个JRadioButtons. 我的第三个JPanel有一个JLabel 我的第四个JPanel有一个JButton 选择任何JRadioButton将更改第三个JPanel中JLabel的文本。当程序启动时,JLabel没有文本 我的JFrame具有Grid
JPanel
s在JFrame
中从上到下对齐。用户告诉我使用GridLayout()
。我让一切正常,但我忘了提到我想将JPanel
s中的所有组件居中对齐
我的第一个JPanel
有一个JLabel
我的第二个
JPanel
有5个JRadioButton
s.我的第三个
JPanel
有一个JLabel
我的第四个
JPanel
有一个JButton
选择任何
JRadioButton
将更改第三个JPanel
中JLabel
的文本。当程序启动时,JLabel
没有文本
我的JFrame
具有GridLayout
。所有具有BoxLayout
(BoxLayout.X_轴
)的4个JPanel
都添加到此JFrame
。以下是我的代码片段:
class GUI extends JFrame implements ActionListener {
JPanel pan1,pan2,pan3,pan4; //4 JPanels
JRadioButton rad1,rad2,rad3,rad4,rad5; //5 RadioButtons
JButton button; //A JButton
JLabel label; //A JLabel
public GUI(String header)
{
super(header);
setLayout(new GridLayout(0,1,0,6)); //set GridLayout to JFrame
setBounds(350,325,600,125);
setResizable(false);
creator();
adder();
commander();
add(pan1);
add(pan2);
add(pan3);
add(pan4) //Add all 4 panels to JFrame
}
private void adder()
{
pan1.setLayout(new BoxLayout(pan1,BoxLayout.X_AXIS));
pan2.setLayout(new BoxLayout(pan2,BoxLayout.X_AXIS));
pan3.setLayout(new BoxLayout(pan3,BoxLayout.X_AXIS));
pan4.setLayout(new BoxLayout(pan4,BoxLayout.X_AXIS)); //Layout for all 4 JPanels
pan1.add(new JLabel("Choose a Security Level"));
ButtonGroup group=new ButtonGroup();
group.add(rad1);
group.add(rad2);
group.add(rad3);
group.add(rad4);
group.add(rad5);
pan2.add(rad1);
pan2.add(rad2);
pan2.add(rad3);
pan2.add(rad4);
pan2.add(rad5);
pan3.add(label);
pan4.add(button);
}
private void creator()
{
pan1=new JPanel();
pan2=new JPanel();
pan3=new JPanel();
pan4=new JPanel();
rad1=new JRadioButton("Security Level 1");
rad2=new JRadioButton("Security Level 2");
rad3=new JRadioButton("Security Level 3");
rad4=new JRadioButton("Security Level 4");
rad5=new JRadioButton("Security Level 5");
button=new JButton("Move On");
label=new JLabel();
}
private void commander()
{
rad1.addActionListener(this);
rad2.addActionListener(this);
rad3.addActionListener(this);
rad4.addActionListener(this);
rad5.addActionListener(this);
rad1.setActionCommand("radio1");
rad2.setActionCommand("radio2");
rad3.setActionCommand("radio3");
rad4.setActionCommand("radio4");
rad5.setActionCommand("radio5");
button.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
//When button is pressed,the text in label changes
if(evt.getActionCommand().equals("radio1"))
label.setText("Very Easy to bypass");
else if(evt.getActionCommand().equals("radio2"))
label.setText("Easy to bypass");
else if(evt.getActionCommand().equals("radio3"))
label.setText("Can bypass Sometimes");
else if(evt.getActionCommand().equals("radio4"))
label.setText("Hard to bypass");
else if(evt.getActionCommand().equals("radio5"))
label.setText("Very Hard to bypass");
else
{ //Code here
}
repaint();
//More code here....
}
}
这是我得到的输出(忽略绿线):
我希望所有东西都在中间对齐,而不是在左边对齐。我应该怎么做才能对齐它?更改:
pan1.setLayout(new BoxLayout(pan1,BoxLayout.X_AXIS));
例如:
pan1.setLayout(new FlowLayout(FlowLayout.CENTER));
..与其他方框布局相同。BTW-
setBounds(350325600125)代码>应更改为setLocationByPlatform(true);包装()
和那些应该在添加了所有内容之后,但在setVisible(true)之前调用的内容
@AndrewThompson,在我的原始代码中,我在执行GUI prog=new GUI()之后调用setBounds
和setVisible
现在我想:为什么我以前没想到呢?顺便说一句,FlowLayout.CENTER
不需要,是吗?…不需要,是吗?“自从Sun将帧的默认布局从FlowLayout
更改为BorderLayout
(很久以前)以来,我一直对接受默认设置持谨慎态度。对于包含一个元素的“行”(例如包含“继续”的区域)按钮),绝对需要FlowLayout.Center
参数