Java JPanel其他JPanel的ontop

Java JPanel其他JPanel的ontop,java,swing,layout,jpanel,overlap,Java,Swing,Layout,Jpanel,Overlap,我想在java中的另一个JPanel上弹出一个JPanel。然而,我在网上找不到任何有用的东西。我试着玩绝对定位游戏,但是像底层的按钮这样的东西会通过顶层显示出来。 我上传了一张我想做的非常难看的画。 有没有一个简单的方法可以做到这一点 编辑: 我试着按照“乌尔克拉”的建议做。以下是代码和屏幕截图: public class Main { public static void main(String[] args) { JFrame frame = new JFrame();

我想在java中的另一个JPanel上弹出一个JPanel。然而,我在网上找不到任何有用的东西。我试着玩绝对定位游戏,但是像底层的按钮这样的东西会通过顶层显示出来。 我上传了一张我想做的非常难看的画。 有没有一个简单的方法可以做到这一点

编辑: 我试着按照“乌尔克拉”的建议做。以下是代码和屏幕截图:

public class Main {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(new Dimension(400, 400));

    JPanel panel = new JPanel();

    JPanel foregroundPanel = new JPanel();      
    foregroundPanel.setVisible(false);
    foregroundPanel.setBackground(Color.BLUE);
    foregroundPanel.setLayout(new BoxLayout(foregroundPanel, BoxLayout.Y_AXIS));

    JPanel backgroungPanel = new JPanel();      
    backgroungPanel.setBackground(Color.RED);
    backgroungPanel.setLayout(new BoxLayout(backgroungPanel, BoxLayout.Y_AXIS));

    for (int i = 0; i < 10; i++) {
        backgroungPanel.add(new JButton("BackgroundBtn " + i));
    }       
    foregroundPanel.add(new JButton("ForegroundBtn 1"));

    JButton makeVisibleBtn = new JButton("+");
    makeVisibleBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            foregroundPanel.setVisible(true);
        }
    });     
    backgroungPanel.add(makeVisibleBtn);        

    JButton makeInvisibleBtn = new JButton("-");
    makeInvisibleBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            foregroundPanel.setVisible(false);
        }
    });     
    foregroundPanel.add(makeInvisibleBtn);      

    panel.add(backgroungPanel);
    panel.add(foregroundPanel);

    frame.add(panel);
}
公共类主{
公共静态void main(字符串[]args){
JFrame=新JFrame();
frame.setVisible(true);
框架设置尺寸(新尺寸(400400));
JPanel面板=新的JPanel();
JPanel foregroundPanel=新的JPanel();
前地板。设置可见(假);
前接地板。后坐地板(颜色。蓝色);
前地面板.setLayout(新的BoxLayout(前地面板,BoxLayout.Y_轴));
JPanel backgroundpanel=新的JPanel();
背景面板.立根背景(颜色.红色);
BackgroundPanel.setLayout(新的BoxLayout(BackgroundPanel,BoxLayout.Y_轴));
对于(int i=0;i<10;i++){
backgroundpanel.add(新的JButton(“BackgroundBtn”+i));
}       
添加(新的JButton(“ForegroundBtn 1”);
JButton makeVisibleBtn=新JButton(“+”);
makeVisibleBtn.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
前地面板。设置可见(真);
}
});     
backgroundpanel.add(makeVisibleBtn);
JButton makeInvisibleBtn=新JButton(“-”);
makeInvisibleBtn.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
前地板。设置可见(假);
}
});     
添加(makeInvisibleBtn);
面板。添加(背景面板);
面板。添加(前地面板);
框架。添加(面板);
}
}


为此,您必须使用一个面板(如果您使用的是NetBeans,您可以在Swing容器中找到它),并且在框架的构造函数中,您必须将其设置为不可见,因此您需要buttom来查看它:(在本例中,我将我的面板称为“jPanel1”,并在其中放置一个buttom)

(当然,如果您想将其设置为不可见,您只需执行与构造函数中相同的操作)

我对它进行了测试,结果很有效:(Imgur不工作,所以请原谅我不得不使用postimg.org)

现在,您可以放置边框,以便将新面板与其他框架区分开来:

您尝试过的代码在哪里?请显示您的代码,并给出一个确切的问题以帮助您解决。使用未修饰的JDialog作为弹出窗口。未修饰的JDialog正是我所需要的。ThanksI发现该解决方案存在问题。JDialog不会随主JFrame一起移动。它能固定在JFrame的位置吗?谢谢你的快速回答。这种解决方案的问题是,不可见的面板必须一直在那里,堵塞空间。我希望它能超越一切。就像在Gmail中,当你写一封新邮件时。在布局的其余部分上有一个弹出窗口,不会干扰它。
public Main() {
    initComponents();
    jPanel1.setVisible(false);//<-- This!
    this.setLocationRelativeTo(null);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jPanel1.setVisible(true);
}