java嵌套面板相互重叠打印 import java.awt.*; 导入javax.swing.*; 公共类面板扩展了JApplet { 私有容器c=getContentPane(); 公共void init() { BorderLayout bl=新的BorderLayout(); 设置布局(bl); 添加(新JButton(“东”),BorderLayout.East; 添加(新JButton(“西”),BorderLayout.West; 添加(新JButton(“北”),BorderLayout.North; 添加(新JButton(“南”),BorderLayout.South; 添加中心面板(); } void addCenterPanel() { JPanel p=新的JPanel(); setLayout(新的BorderLayout()); 添加(新JButton(“右”),BorderLayout.EAST; 添加(新JButton(“左”),BorderLayout.WEST; 添加(新的JButton(“Up”),BorderLayout.NORTH); 添加(新的JButton(“向下”),BorderLayout.SOUTH); addinnermospanel(); } void addInnermostPanel() { JPanel中心=新的JPanel(); setLayout(新的BorderLayout()); 添加(新的JButton(“>”),BorderLayout.EAST); 添加(新的JButton(“”),BorderLayout.EAST); 添加(新的JButton(“”),BorderLayout.EAST); 添加(新的JButton(“
我希望面板显示在彼此的内部(在中心区域),但它们打印在彼此的顶部,无论出于什么原因,我都无法找出我遗漏了什么。提前感谢,如果您将所有组件直接添加到小程序本身,而不是添加到java嵌套面板相互重叠打印 import java.awt.*; 导入javax.swing.*; 公共类面板扩展了JApplet { 私有容器c=getContentPane(); 公共void init() { BorderLayout bl=新的BorderLayout(); 设置布局(bl); 添加(新JButton(“东”),BorderLayout.East; 添加(新JButton(“西”),BorderLayout.West; 添加(新JButton(“北”),BorderLayout.North; 添加(新JButton(“南”),BorderLayout.South; 添加中心面板(); } void addCenterPanel() { JPanel p=新的JPanel(); setLayout(新的BorderLayout()); 添加(新JButton(“右”),BorderLayout.EAST; 添加(新JButton(“左”),BorderLayout.WEST; 添加(新的JButton(“Up”),BorderLayout.NORTH); 添加(新的JButton(“向下”),BorderLayout.SOUTH); addinnermospanel(); } void addInnermostPanel() { JPanel中心=新的JPanel(); setLayout(新的BorderLayout()); 添加(新的JButton(“>”),BorderLayout.EAST); 添加(新的JButton(“”),BorderLayout.EAST); 添加(新的JButton(“”),BorderLayout.EAST); 添加(新的JButton(“,java,swing,Java,Swing,我希望面板显示在彼此的内部(在中心区域),但它们打印在彼此的顶部,无论出于什么原因,我都无法找出我遗漏了什么。提前感谢,如果您将所有组件直接添加到小程序本身,而不是添加到JPanelp中,我们将非常感谢您的帮助。该面板未添加到小程序本身。在addInnermostPanel中,组件再次添加到小程序中,而中心JPanel永远不会添加到容器中在addCenterPanel中,您将所有组件直接添加到小程序本身中,而不是添加到JPanel中。该面板未添加到小程序本身。在addInnermostPanel
JPanel
p
中,我们将非常感谢您的帮助。该面板未添加到小程序本身。在addInnermostPanel
中,组件再次添加到小程序中,而中心JPanel
永远不会添加到容器中在addCenterPanel
中,您将所有组件直接添加到小程序本身中,而不是添加到JPanel
中。该面板未添加到小程序本身。在addInnermostPanel
中,组件再次添加到小程序中,而中心JPanel
永远不会添加到容器中在addCenterPanel
中,您将所有组件直接添加到小程序本身中,而不是添加到JPanel
中。该面板未添加到小程序本身。在addInnermostPanel
中,组件再次添加到小程序中,而中心JPanel
永远不会添加到容器中在addCenterPanel
中,您将所有组件直接添加到小程序本身中,而不是添加到JPanel
中。该面板未添加到小程序本身。在addInnermostPanel
中,组件再次添加到小程序中,而中心JPanel
永远不会添加到容器中这里是您的代码应该是这样的
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);
}
}
import java.awt.*;
导入javax.swing.*;
公共类面板扩展了JFrame
{
私有容器c=getContentPane();
公共void init()
{
BorderLayout bl=新的BorderLayout();
设置布局(bl);
设置默认关闭操作(3);
setLocationRelativeTo(空);
添加(新JButton(“东”),BorderLayout.East;
添加(新JButton(“西”),BorderLayout.West;
添加(新JButton(“北”),BorderLayout.North;
添加(新JButton(“南”),BorderLayout.South;
添加(添加中心面板(),“中心”);
}
JPanel addCenterPanel()
{
JPanel p=新的JPanel();
p、 setLayout(新的BorderLayout());
p、 添加(新JButton(“右”),BorderLayout.EAST;
p、 添加(新JButton(“左”),BorderLayout.WEST;
p、 添加(新的JButton(“Up”),BorderLayout.NORTH);
p、 添加(新的JButton(“向下”),BorderLayout.SOUTH);
p、 添加(addInnermostPanel(),“Center”);
返回p;
}
JPanel Addinnermospanel()
{
JPanel中心=新的JPanel();
setLayout(新的BorderLayout());
添加(新的JButton(“>”),BorderLayout.EAST);
添加(新的JButton(“<”),BorderLayout.WEST);
添加(新的JButton(“^”),BorderLayout.NORTH;
添加(新的JButton(“v”),BorderLayout.SOUTH);
添加(新的JButton(“O”),BorderLayout.center);
返回中心;
}
公共静态void main(字符串…参数){
新面板().setVisible(真);
}
公共事务委员会(){
init();
包装();
}
}
注意:我使用了JFrame
而不是applet。
另外,我刚刚发布了代码,因为它需要很多时间来描述整个程序,我认为只有代码就足够了
如果你有问题,请写在评论中。这里是你的答案代码应该是这样的
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);
}
}
import java.awt.*;
导入javax.swing.*;
公共类面板扩展了JFrame
{
私有容器c=getContentPane();
公共void init()
{
BorderLayout bl=新的BorderLayout();
设置布局(bl);
设置默认关闭操作(3);
setLocationRelativeTo(空);
添加(新JButton(“东”),BorderLayout.East;
添加(新JButton(“西”),BorderLayout.West;
添加(新JButton(“北”),BorderLayout.North;
添加(新JButton(“南”),BorderLayout.South;
添加(添加中心面板(),“中心”);
}
JPanel addCenterPanel()
{
JPanel p=新的JPanel();
p、 setLayout(新的BorderLayout());
p、 添加(新JButton(“右”),BorderLayout.EAST;
p、 添加(新JButton(“左”),BorderLayout.WEST;
p、 添加(新的JButton(“Up”),BorderLayout.NORTH);
p、 添加(新的JButton(“向下”),BorderLayout.SOUTH);
p、 添加(addInnermostPanel(),“Center”);
返回p;
}
JPanel Addinnermospanel()
{
JPanel中心=新的JPanel();
setLayout(新的BorderLayout());
添加(新的JButton(“>”),BorderLayout.EAST);
添加(新的JButton(“<”),BorderLayout.WEST);
添加(新的JButton(“^”),BorderLayout.NORTH;
添加(新的JButton(“v”),BorderLayout.SOUTH);
添加(新的JButton(“O”),BorderLayout.center);
返回中心;