JPanel中的JPanels [英] JPanels in JPanel

查看:103
本文介绍了JPanel中的JPanels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java JPanels有问题.我想将具有不同布局的2个JPanels放入一个也具有布局的JPanel中.甚至有可能使其工作吗?

I have a problem with Java JPanels. I would like to put 2 JPanels with different layouts into one JPanel which is also has a layout. Is it even possible to make it work?

    BibP()
setLayout(new GridLayout(5, 1)); //The big JPanel
add(new A(), new FlowLayout(4));
add(new B(), new GridLayout(7,2));    

A和B都是作为JPanels扩展的类,无论我更改或评论什么B总是出现在1行中. 我在A上添加了4个元素,在B上添加了14个元素(JLabels和JTextAreas),仅添加和一些计算就没有太多代码了.

Both A and B are classes extends as JPanels and no matter what I changed or commented B is always appears in 1 row. I have 4 elements added to A and 14 to B (JLabels and JTextAreas) and there is not much code in them only the adds and some calculation.

问题可能出在我试图放置大型JPanel的JFrame中.

The problem might be in the JFrame where I am trying to put the big JPanel.

    JFrame.this.add(new BigP(),BorderLayout.CENTER);    

    public class BigP extends JPanel{
//Labels and TextAres

public class A extends JPanel{
    public A(){
        setBorder(new EmptyBorder(0, -50, 0, 0));
        //get date and add to textareas
        //add the label and textareas
}
}

public class B extends JPanel{
    public B (){
        setBorder(new EmptyBorder(0, -50, 0, 0));
        setBackground(Color.red);
        //colum longs for text areas less then 5
        //add labels and textareas
    }
}

public BigP(){
    setLayout(new GridLayout(5, 1));
    setBorder(new EmptyBorder(3,-160,0,0));

    add(new A(), new FlowLayout(4));
    add(new B(), new GridLayout(7,2));
}
}    

感谢您的帮助.

经过几次尝试:

如果我使用过此话:

    add(new B(), new GridLayout(7,2));

当我打印布局时,我在B中得到了它:

I get this in B when I printlned the layout:

java.awt.FlowLayout [hgap = 5,vgap = 5,align = center]

java.awt.FlowLayout[hgap=5,vgap=5,align=center]

如果我在B中设置版式:

If I set the Layout in B:

    setLayout(new GridLayout(7, 2));

信息正确:

java.awt.GridLayout [hgap = 0,vgap = 0,rows = 7,cols = 2]

java.awt.GridLayout[hgap=0,vgap=0,rows=7,cols=2]

但是只有2个JTextAreas可见,应该包含14个元素.

But there is only 2 JTextAreas vissible where should be 14 elements.

推荐答案

是否有可能使其正常工作?

Is it even possible to make it work?

是的,通常称为复合布局.

Yes, it's commonly know as compound layouts.

您遇到的问题是,您试图将Layout作为约束提供给当前的容器布局管理器(在您的情况下,您很幸运,没想到有什么用)...

The problem you have is you are trying to supply the Layouts as constraints to the current containers layout manager (which in your case, your lucky it wasn't expecting any)...

相反,直接将布局管理器提供给子面板...类似...

Instead, supply the layout manager directly to the child panels...something like...

setLayout(new GridLayout(5, 1)); //The big JPanel

JPanel a = new JPanel(new FlowLayout(4));
JPanel b = new JPanel(new GridLayout(7,2));
add(a);
add(b); 

看看在容器内布置组件有关更多详细信息...

Take a look at Laying Out Components Within a Container for more details...

更新示例

使用标签作为占位符的简单可运行示例.

Simple runnable example, using labels as place holders.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class ExampleLayout {

  public static void main(String[] args) {
    new ExampleLayout();
  }

  public ExampleLayout() {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
          ex.printStackTrace();
        }

        JPanel mainPane = new JPanel(new GridLayout(5, 1));
        mainPane.add(new APanel());
        mainPane.add(new BPanel());

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(mainPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    });
  }

  public class APanel extends JPanel {

    public APanel() {
      setLayout(new FlowLayout(4));
      for (int index = 0; index < 4; index++) {
        add(new JLabel(Integer.toString(index)));
      }
      setBorder(new LineBorder(Color.RED));
    }

  }

  public class BPanel extends JPanel {

    public BPanel() {
      setLayout(new GridLayout(7, 2));
      for (int index = 0; index < 14; index++) {
        add(new JLabel(Integer.toString(index)));
      }
      setBorder(new LineBorder(Color.BLUE));
    }

  }

}

setBorder(new EmptyBorder(3,-160,0,0));也看起来不对. EmptyBorder绝对不能为负值

This setBorder(new EmptyBorder(3,-160,0,0)); also looks wrong. EmptyBorder should NEVER have negative values

这篇关于JPanel中的JPanels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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