[英]Java add more than one JPanel objects into JFrame
I am a beginer and I dont know how to add more objects into JFrame. 我是一个初学者,我不知道如何在JFrame中添加更多对象。 How could I add more than one JPanel objects into JFrame?
如何在JFrame中添加多个JPanel对象? Below is what I have tried.
以下是我的尝试。
Thanks for your help. 谢谢你的帮助。
public class Init extends JFrame{
public Init(){
super("Ball");
Buttons t = new Buttons();
JumpingBall b1 = new JumpingBall();
JumpingBall b2 = new JumpingBall();
t.addBall(b1);
t.addBall(b2);
add(b1);
add(b2);
setSize(500,500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
}
Assuming that JumpingBall
extends JPanel
, you might want to have a look at the java layout managers here: Link . 假设
JumpingBall
扩展了JPanel
,你可能想看看这里的java布局管理器: Link 。
The default Layout
for a JFrame
is the BorderLayout
and if you didn't specify where you want to add your component, The BorderLayout
will put it in the center by default. JFrame
的默认Layout
是BorderLayout
,如果您没有指定要添加组件的位置,则BorderLayout
默认情况下将其置于中心。 In BorderLayout
, you cannot have more that one component in the same area. 在
BorderLayout
,您不能在同一区域中拥有更多的那个组件。 So, in your example you will end up having only the second JumpingBall
panel in your frame. 因此,在您的示例中,您最终只会在帧中使用第二个
JumpingBall
面板。 If you want to have more than one component at the center, then you will have to create a JPanel
and add those components to it using different Layout. 如果您希望在中心有多个组件,则必须创建一个
JPanel
并使用不同的Layout将这些组件添加到其中。 The common three Layouts are the BorderLayout
, FlowLayout
and GridLayout
Please have a look at the provided link above to see how the components are arranged. 常见的三个布局是
BorderLayout
, FlowLayout
和GridLayout
请查看上面提供的链接,了解组件的排列方式。
You are seeing only one because it overlapping each other. 你只看到一个,因为它相互重叠。 Just provide
setbound(x,y,x1,y1)
for you panel component and you will see your panel at location. 只需为您的面板组件提供
setbound(x,y,x1,y1)
,您将在位置看到您的面板。
or use setLayout(new FlowLayout());
或者使用
setLayout(new FlowLayout());
which is going to order your component in respective to other so you will not override each-other. 这将是您的组件相应的订购,所以你不会覆盖彼此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.