Java JPanel 中的 JPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18202415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPanels in JPanel
提问by PETI258
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?
我有 Java JPanels 的问题。我想将 2 个具有不同布局的 JPanel 放入一个也有布局的 JPanel 中。甚至有可能让它工作吗?
BibP()
setLayout(new GridLayout(5, 1)); //The big JPanel
add(new A(), new FlowLayout(4));
add(new B(), new GridLayout(7,2));
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.
A 和 B 都是作为 JPanel 扩展的类,无论我更改或评论什么,B 总是出现在 1 行中。我有 4 个元素添加到 A 和 14 个添加到 B(JLabels 和 JTextAreas),它们中没有太多代码,只有添加和一些计算。
The problem might be in the JFrame where I am trying to put the big JPanel.
问题可能出在我试图放置大 JPanel 的 JFrame 中。
JFrame.this.add(new BigP(),BorderLayout.CENTER);
Edit:
编辑:
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));
}
}
Thanks for helping.
谢谢你的帮助。
After a few tries:
经过几次尝试:
If I used this:
如果我使用这个:
add(new B(), new GridLayout(7,2));
I get this in B when I printlned the layout:
当我打印布局时,我在 B 中得到了这个:
java.awt.FlowLayout[hgap=5,vgap=5,align=center]
java.awt.FlowLayout[hgap=5,vgap=5,align=center]
If I set the Layout in B:
如果我在 B 中设置布局:
setLayout(new GridLayout(7, 2));
The information is correct:
信息正确:
java.awt.GridLayout[hgap=0,vgap=0,rows=7,cols=2]
java.awt.GridLayout[hgap=0,vgap=0,rows=7,cols=2]
But there is only 2 JTextAreas vissible where should be 14 elements.
但是只有 2 个 JTextAreas 可见,其中应该有 14 个元素。
采纳答案by MadProgrammer
Is it even possible to make it work?
甚至有可能让它工作吗?
Yes, it's commonly know as compound layouts.
是的,它通常被称为复合布局。
The problem you have is you are trying to supply the Layout
s as constraints to the current containers layout manager (which in your case, your lucky it wasn't expecting any)...
您遇到的问题是您正试图将Layout
s 作为约束提供给当前的容器布局管理器(在您的情况下,您很幸运它没有期待任何)...
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 Containerfor more details...
查看在容器内布置组件以了解更多详细信息...
Update with example
用例子更新
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));
}
}
}
This setBorder(new EmptyBorder(3,-160,0,0));
also looks wrong. EmptyBorder
should NEVER have negative values
这setBorder(new EmptyBorder(3,-160,0,0));
看起来也是错误的。 EmptyBorder
永远不应该有负值
回答by Aniket Thakur
Is it even possible to make it work?
Yes it is possible to do this. In the following line
是的,有可能做到这一点。在以下行中
JFrame.this.add(new BigP(),BorderLayout.CENTER);
I would suggest do
我建议做
mainFrame.getContentPane.add(new BigP());
and set the layout of your mainFrame before this
并在此之前设置 mainFrame 的布局
mainFrame.setLayout(new GridLayout(5, 1));