java - 如何在具有 GridLayout 的 JPanels 中居中对齐所有内容

标签 java swing jframe jpanel grid-layout

最近,我有 asked a question关于如何使 JPanelJFrame 中从上到下对齐。用户告诉我使用 GridLayout()。我让一切正常,但我忘了提到我想将 JPanel 中的所有组件居中对齐。

我的第一个 JPanel 有一个 JLabel
我的第二个 JPanel 有 5 个 JRadioButton
我的第三个 JPanel 有一个 JLabel
我的第四个 JPanel 有一个 JButton

选择任何 JRadioButton 都会更改第三个 JPanel 中的 JLabel 的文本。程序启动时,JLabel 没有文本。

我的 JFrameGridLayout。所有 4 个具有 BoxLayout(BoxLayout.X_Axis) 的 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....
   }


}

这是我得到的输出(忽略绿线): My program

我希望所有内容都居中而不是左侧对齐。我应该怎么做才能对齐它?

最佳答案

改变:

pan1.setLayout(new BoxLayout(pan1,BoxLayout.X_AXIS));

类似于:

pan1.setLayout(new FlowLayout(FlowLayout.CENTER));

..其余的框布局也是如此。

关于java - 如何在具有 GridLayout 的 JPanels 中居中对齐所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27589763/

相关文章:

java - 如何迭代bean/对象中的所有属性/字段,包括引用对象/Bean

java - Unix 中的 jvisualvm

Java Swing、 Action 监听器

java - JCombobox 只接受字母

java - 如何将另一个类的文本添加到 JMessage 框中?

java - 是否可以将 JFrame 放在前面而不是焦点?

java - java中正则表达式的近似匹配

java - 在 Liferay 中生成指向不同 portlet 的 URL

java - JMenuBar抽象按钮工厂不添加按钮

java - 如何修复位于另一个预先绘制的 JPanel 之上的 JPanel 上的动态移动图像的闪烁?