如何从上至下排列JPanels? [英] How do I arrange JPanels from top to bottom?

查看:190
本文介绍了如何从上至下排列JPanels?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习Java.我正在学习图形用户界面(GUI)编程.

I'm currently self-studying Java. I'm learning Graphical User Interface(GUI) programming.

我希望JPanelJFrame中从上到下排列.首先,我在第一个JPanel中添加了JLabel.第二个JPanel具有5个JRadioButtions.第三个JPanel具有JButtonJLabel.

I want JPanels to be arranged from top to bottom in a JFrame.First of all,I have a JLabel added to the first JPanel. The second JPanel has 5 JRadioButtions. The third JPanel has a JButton and a JLabel.

按下JButton时,第3个 JPanel中的JLabel显示一些文本.

When the JButton is pressed,the JLabel in the 3rd JPanel shows some text.

我将BoxLayout(BoxLayout.X_AXIS)用于所有JPanels,并将所有三个添加到具有FlowLayout()JFrame中.这是一小段代码:

I used BoxLayout(BoxLayout.X_AXIS) for all the JPanels and added all 3 of them into a JFrame which has FlowLayout(). Here is a small piece of code:

class GUI extends JFrame implements ActionListener {

  JPanel pan1,pan2,pan3;                   //3 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 FlowLayout());  //set FlowLayout to JFrame
      setBounds(350,325,600,125);
      setResizable(false);

      creator();
      adder();
      commander();

      add(pan1);
      add(pan2);
      add(pan3); //Add all 3 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)); //Layout for all 3 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(button);
    pan3.add(label);
  }

   private void creator()
   {
      pan1=new JPanel();
      pan2=new JPanel();
      pan3=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....
   }


}

这是我选择第一个单选按钮时得到的输出(忘记绿色):

This is the output I'm getting when I select the first radiobutton(Forget the green colour):

我希望将非常容易绕过"文本放在继续前进"按钮上方和所有JRadioButtons下方.我可以增加JFrame的大小,以便有足够的空间.我的问题是:

I want the "Very easy to Bypass" text to be placed above the "Move on" button and below all the JRadioButtons. I can increase the size of the JFrame so that there will be enough space. My questions are:

  • 我应该使用哪个布局来实现这一目标?
  • 此布局应仅应用于JFrame还是全部3个JPanels?
  • Which Layout should I use to achieve this?
  • Should this layout be applied just for the JFrame or all 3 JPanels?

推荐答案

使用GridLayout

GridLayout layout = new GridLayout(0, 1, 0, 5);
setLayout(layout);

要添加5个JPanel,我该怎么做:

What I would do to add 5 JPanels:

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class PanelAdd extends JFrame {

    JPanel [] panels  ;

    public PanelAdd() {

        GridLayout layout = new GridLayout(0, 1, 0, 5);
        setLayout(layout);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        setSize(400, 350);
    }

    public static void main(String [] args) {

        PanelAdd add = new PanelAdd();
        add.addPanels();
        add.setVisible(true);

    }

    private void addPanels() {

        panels = new JPanel[5];
        for (int i = 0 ; i < panels.length ; i++) {

            panels[i] = new JPanel();
            panels[i].add(new JLabel("This Is Panel "+i));

            add(panels[i]);

        }

    }

}

在此示例中,我制作了一个由5个JPanels组成的数组,并通过循环将它们添加. 我用GridLayout做这份工作.

In this example, I made an array of 5 JPanels and add them through a loop. I used GridLayout for the job.

这仅是您答案的提示

这篇关于如何从上至下排列JPanels?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆