Java 将相同的组件添加到多个面板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19697996/
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
Adding the same components to multiple panels?
提问by TheEyesHaveIt
I'm using Swing and I am trying to add the same components to multiple panels. However, only the panel I add to the frame last has these buttons. I am adding the same set of labels/textfields to p2, p3, and p4. But since I added the labels/textfields to p4 last, it seems to show only p4 as having it. How can I re-use these components for multiple JPanels so I don't have to make a ton of them for each different panel?
我正在使用 Swing 并且我正在尝试将相同的组件添加到多个面板。但是,只有我最后添加到框架的面板才有这些按钮。我正在向 p2、p3 和 p4 添加相同的标签/文本字段集。但是由于我最后将标签/文本字段添加到 p4,它似乎只显示 p4 有它。如何将这些组件重用于多个 JPanel,以便我不必为每个不同的面板制作大量组件?
The code I am using:
我正在使用的代码:
public class TriangleGUI extends JFrame implements ActionListener
{
static JPanel p1, p2, p3,p4,p5;
static JButton b1, b2, b3;
static JLabel label1, label2, label3;
static JTextField tf1, tf2, tf3;
private static int side1, side2, side3, angle1, angle2, angle3;
public TriangleMadnessGUI(){
setSize(800,400);
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
b1 = new JButton("1 Side, 2 Angles"); //buttons
b2 = new JButton("1 Angle, 2 Sides");
b3 = new JButton("3 Sides");
label1 = new JLabel("Angle 1"); //labels which i intend to reuse
label2 = new JLabel("Angle 2");
label3 = new JLabel("Side 1");
tf1 = new JTextField("",5); //textfields which i intend to reuse
tf2 = new JTextField("",5);
tf3 = new JTextField("",5);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p2.add(label1); //panel 2, add set of components
p2.add(tf1);
p2.add(label2);
p2.add(tf2);
p2.add(label3);
p2.add(tf3);
p3.add(label1); //panel 3, adding set of same components
p3.add(tf1);
p3.add(label2);
p3.add(tf2);
p3.add(label3);
p3.add(tf3);
p4.add(label1); //panel 4, adding set of same components
p4.add(tf1);
p4.add(label2);
p4.add(tf2);
p4.add(label3);
p4.add(tf3);
add(p1, BorderLayout.NORTH);
add(p4, BorderLayout.CENTER); // <-----HERE, only p4 has visible buttons
// p2 and p3 are just blank if used in here.
setVisible(true);
}
}
采纳答案by Sage
However, only the panel I add to the frame last has these buttons. I am adding the same set of labels/textfields to p2, p3, and p4. But since I added the labels/textfields to p4 last, it seems to show only p4 as having it.
但是,只有我最后添加到框架的面板才有这些按钮。我正在向 p2、p3 和 p4 添加相同的标签/文本字段集。但是由于我最后将标签/文本字段添加到 p4,它似乎只显示 p4 有它。
If any component c1
of panel1
gets added to another Container
panel2
, then it first removed from the old Container(panel1
) and then gets added to panel2
. Refer to the source code of Container
class add(component)
function.
如果任何组分c1
的panel1
被添加到另一个Container
panel2
,那么它首先从旧的容器(除去panel1
),然后被添加到panel2
。参考Container
类add(component)
函数的源码。
add(p4, BorderLayout.CENTER); // <-----HERE, only p4 has visible buttons
// p2 and p3 are just blank if used in here.
Yes, when using layout we can't place two component in the same place. That doesn't make any sense. If p1
, p2
and p3
contains same JComponent
instances, there is no meaning to use it with multiple panel. However if you are thinking about Group of components with similar(not same) outlet and orientation, we can easily create our own version of component and orient other necessary component inside of it:
是的,在使用布局时,我们不能将两个组件放在同一个地方。那没有任何意义。如果p1
,p2
和p3
包含相同的JComponent
实例,则将其与多个面板一起使用是没有意义的。但是,如果您正在考虑具有相似(不相同)出口和方向的组件组,我们可以轻松创建我们自己的组件版本并在其中定位其他必要的组件:
class MyPanel extends JPanel
{
JTextFeild tf1;
JLabel label1;
JLabel label2;
JTextFeild tf2;
JLabel label3;
public MyPanel(String lab1Val, String lab2Val, String lab3Val)
{
setLayout(myLayout);
// create the component tf1, label1, label2, etc instances
add(tf1);
add(label2);
add(tf2);
add(label3);
add(tf3);
}
@Override
public Dimension getPreferredSize() {
}
}
Then we can create any number of MyPanel
instances to work with suitable layout manager.
然后我们可以创建任意数量的MyPanel
实例来使用合适的布局管理器。
回答by MadProgrammer
JFrame
uses a BorderLayout
by default.
JFrame
BorderLayout
默认使用 a 。
BorderLayout
will only show one component for each of it's five available slots. That means, only the last component added to any given spot will be visible.
BorderLayout
将只为它的五个可用插槽中的每一个显示一个组件。这意味着,只有添加到任何给定位置的最后一个组件才可见。
Take a look at Laying Out Components Within a Containerfor more ideas
查看在容器内布置组件以获得更多想法