简体   繁体   English

将JPanel带到Java中其他对象的前面(SWING)

[英]Bring JPanel to front of other objects in java (SWING)

I want to make a loading message when an app processes, so I used a JPanel over a JTree . 我想在应用程序处理时发出加载消息,因此我在JTree使用了JPanel But when the user clicks on the JPanel , the JTree will be selected and the JPanel will go to the back. 但是,当用户单击JPanel ,将选择JTree ,并且JPanel将转到后面。 After hiding the JPanel , it never shows again. 隐藏JPanel ,它将不再显示。 I don't know why, but it seems it never go in front of the JTree . 我不知道为什么,但是似乎它永远不会出现在JTree前面。

I need a way to bring the JPanel in front of everything. 我需要一种将JPanel置于一切之上的方法。 How can I do this? 我怎样才能做到这一点?

EDIT: Also I must mention that I don't want a JDialog . 编辑:另外,我必须提到,我不需要JDialog I want to use the JPanel on top of any element to show a loading message until a process finishes. 我想在任何元素之上使用JPanel来显示加载消息,直到过程完成为止。

So here you have at least two solutions. 因此,这里至少有两个解决方案。 Either go with what @Geoff and @sthupahsmaht are suggesting. 要么按照@Geoff和@sthupahsmaht的建议去做。 BTW also possible is to use JOptionPane which automatically creates a dialog for you. 顺便说一句,也可以使用JOptionPane,它会自动为您创建一个对话框。

The other option would be to use a GlassPane from a frame. 另一种选择是使用框架中的GlassPane。

Or yet another option is to use JLayeredPane as @jzd suggests. 或另一个选择是使用@@ zzd建议的JLayeredPane。

EDIT: Example showing how to use GlassPane to capture user selections. 编辑:显示如何使用GlassPane捕获用户选择的示例。 Try following steps: 请尝试以下步骤:

1.Left clicking on the glass pane visible at start. 1.左键单击​​开始时可见的玻璃窗格。 See the output. 查看输出。

2.Right click it. 2.右键单击它。 This hides the glass pane. 这将隐藏玻璃窗格。

3.Left clicking on the content pane. 3.左键单击​​内容窗格。 See the output. 查看输出。

4.Right click it. 4.右键单击它。 Go to point 1. Enjoy. 转到第1点。尽情享受。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class OverPanel extends JPanel
{   
    private static void createAndShowGUI()
    {
        final JFrame f = new JFrame();
        f.setPreferredSize(new Dimension(400, 300));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        JPanel glassPanel = new JPanel();
        glassPanel.setBackground(Color.RED);        
        glassPanel.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                System.out.println("f.getGlassPane() mousePressed");
                if(e.getButton() == MouseEvent.BUTTON3)
                    f.getGlassPane().setVisible(false);
            }
        });
        f.setGlassPane(glassPanel);     

        f.getContentPane().setBackground(Color.GREEN);
        f.getContentPane().addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                System.out.println("f.getContentPane() mousePressed");
                if(e.getButton() == MouseEvent.BUTTON3)
                    f.getGlassPane().setVisible(true);
            }
        });
        f.getGlassPane().setVisible(true);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

EDIT2 : If you want to have an effect of a dialog, you can achieve it by incorporating appropriately this code into my example. EDIT2 :如果您想产生对话框效果,可以通过适当地将此代码合并到我的示例中来实现。

        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        panel.setBackground(Color.YELLOW);
        panel.add(new JLabel("I am message Label"));
        panel.add(new JButton("CLOSE"));
        JPanel glassPanel = new JPanel(new GridBagLayout());
        glassPanel.setOpaque(false);
        glassPanel.add(panel);

You need a to use a JLayeredPane for moving components in front of each other. 您需要使用JLayeredPane将组件移到彼此前面。

Here is a tutorial: How to use Layered Panes 这是一个教程: 如何使用分层窗格

禁用玻璃窗格可能会帮助您。

It's not really clear how your code is organized. 还不清楚代码的组织方式。 However, it sounds like what you might want is a modal dialog. 但是,听起来您可能想要模态对话框。 Here's a link to a similar discussion with a number of referenced resources. 这是带有大量参考资源的类似讨论的链接。

How to make a JFrame Modal in Swing java 如何在Swing Java中制作JFrame模态

Jpanel main = new JPanel();
Jpanel a = new JPanel();
JPanel b = new Jpanel();

main.add(a);
main.add(b);

at this point the object: 此时对象:

a -> 0 ( index)
b -> 1 (index)

main.getComponentCount() = 2

main.setComponentZorder(b,0);


a -> 1
b -> 0;

b OVER
a DOWN

For those who have no problem using a JDialog , this is a sure way to get it to show up if you're having issues. 对于那些使用JDialog没问题的人,这是一种确定的方法,可以在出现问题时显示它。 Just make sure to control it properly if the dialog is modal, when disposing, setting focus etc. 只要确保对话框是模态对话框,布置,设置焦点等时正确控制它即可。

JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);

使用JXLayer或JIDE Overlayable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM