[英]java nested panels printing on top of each other
import java.awt.*;
import javax.swing.*;
public class Panels extends JApplet
{
private Container c = getContentPane();
public void init()
{
BorderLayout bl = new BorderLayout();
setLayout(bl);
add(new JButton("East "), BorderLayout.EAST);
add(new JButton("West "), BorderLayout.WEST);
add(new JButton("North "), BorderLayout.NORTH);
add(new JButton("South "), BorderLayout.SOUTH);
addCenterPanel();
}
void addCenterPanel()
{
JPanel p = new JPanel();
setLayout(new BorderLayout());
add(new JButton("Right "), BorderLayout.EAST);
add(new JButton("Left "), BorderLayout.WEST);
add(new JButton("Up "), BorderLayout.NORTH);
add(new JButton("Down "), BorderLayout.SOUTH);
addInnermostPanel();
}
void addInnermostPanel()
{
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
add(new JButton("> "), BorderLayout.EAST);
add(new JButton("< "), BorderLayout.WEST);
add(new JButton("^ "), BorderLayout.NORTH);
add(new JButton("v "), BorderLayout.SOUTH);
add(new JButton("O"), BorderLayout.CENTER);
}
}
I want the panels to display inside of each other (in the CENTER region) , but they are printing on top of each other and for whatever reason I cant figure out what Im missing. 我希望这些面板在彼此之间显示(在CENTER区域中),但是它们却在彼此之间打印,并且出于任何原因,我都无法弄清楚Im缺少了什么。 Thanks in advance, any help is appreciated
在此先感谢您的帮助
Here is you're code should be like this; 这是您的代码,应该像这样;
import java.awt.*;
import javax.swing.*;
public class Panels extends JFrame
{
private Container c = getContentPane();
public void init()
{
BorderLayout bl = new BorderLayout();
setLayout(bl);
setDefaultCloseOperation(3);
setLocationRelativeTo(null);
add(new JButton("East "), BorderLayout.EAST);
add(new JButton("West "), BorderLayout.WEST);
add(new JButton("North "), BorderLayout.NORTH);
add(new JButton("South "), BorderLayout.SOUTH);
add(addCenterPanel(),"Center");
}
JPanel addCenterPanel()
{
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JButton("Right "), BorderLayout.EAST);
p.add(new JButton("Left "), BorderLayout.WEST);
p.add(new JButton("Up "), BorderLayout.NORTH);
p.add(new JButton("Down "), BorderLayout.SOUTH);
p.add(addInnermostPanel(),"Center");
return p;
}
JPanel addInnermostPanel()
{
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
center.add(new JButton("> "), BorderLayout.EAST);
center.add(new JButton("< "), BorderLayout.WEST);
center.add(new JButton("^ "), BorderLayout.NORTH);
center.add(new JButton("v "), BorderLayout.SOUTH);
center.add(new JButton("O"), BorderLayout.CENTER);
return center;
}
public static void main(String ...args){
new Panels().setVisible(true);
}
public Panels(){
init();
pack();
}
}
Note : I used JFrame
instead applet. 注意 :我使用
JFrame
代替applet。 Also I just posted the code because it takes many time to describe the whole program, I think only the code will be enough. 我也刚刚发布了代码,因为描述整个程序需要花费很多时间,我认为仅代码就足够了。
If you have a question, write it in the comment. 如果有问题,请在评论中写下。
In addCenterPanel
youre adding all components directly to the applet itself rather than to the JPanel
p
. 在
addCenterPanel
您可以将所有组件直接添加到applet本身,而不是添加到JPanel
p
。 That panel is not being added to the applet itself. 该面板未添加到小程序本身。 In
addInnermostPanel
the components are again added to the applet and the center
JPanel
is never added to the container 在
addInnermostPanel
,组件再次添加到applet中,而center
JPanel
从不添加到容器中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.