将 JPanel 置于 java 中其他对象的前面 (SWING)

发布于 2024-11-06 21:47:38 字数 388 浏览 4 评论 0原文

我想在应用程序处理时发出加载消息,因此我在 JTree 上使用了 JPanel。但是,当用户单击JPanel时,JTree将被选中,并且JPanel将转到后面。隐藏 JPanel 后,它再也不会显示。我不知道为什么,但它似乎永远不会出现在JTree前面。

我需要一种方法将 JPanel 放在所有内容的前面。我该怎么做?

编辑:另外我必须提到我不需要JDialog。我想在任何元素顶部使用 JPanel 来显示加载消息,直到进程完成。

I want to make a loading message when an app processes, so I used a JPanel over a JTree. But when the user clicks on the JPanel, the JTree will be selected and the JPanel will go to the back. After hiding the JPanel, it never shows again. I don't know why, but it seems it never go in front of the JTree.

I need a way to bring the JPanel in front of everything. How can I do this?

EDIT: Also I must mention that I don't want a JDialog. I want to use the JPanel on top of any element to show a loading message until a process finishes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

奢欲 2024-11-13 21:47:38

所以这里至少有两个解决方案。要么遵循@Geoff 和@sthupahsmaht 的建议。顺便说一句,也可以使用 JOptionPane,它会自动为您创建一个对话框。

另一种选择是使用框架中的 GlassPane。

或者,另一种选择是按照 @jzd 的建议使用 JLayeredPane。

编辑:
示例展示如何使用 GlassPane 捕获用户选择。
尝试以下步骤:

1.左键单击​​开始时可见的玻璃窗格。查看输出。

2.右键单击它。这隐藏了玻璃板。

3.左键单击​​内容窗格。查看输出。

4.右键单击它。转到第 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();
            }
        });
    }
}

编辑2
如果您想获得对话框的效果,可以通过将此代码适当地合并到我的示例中来实现。

        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);

So here you have at least two solutions. Either go with what @Geoff and @sthupahsmaht are suggesting. BTW also possible is to use JOptionPane which automatically creates a dialog for you.

The other option would be to use a GlassPane from a frame.

Or yet another option is to use JLayeredPane as @jzd suggests.

EDIT:
Example showing how to use GlassPane to capture user selections.
Try following steps:

1.Left clicking on the glass pane visible at start. See the output.

2.Right click it. This hides the glass pane.

3.Left clicking on the content pane. See the output.

4.Right click it. Go to point 1.
Enjoy.

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.

        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);
维持三分热 2024-11-13 21:47:38

您需要使用 JLayeredPane 用于在彼此前面移动组件。

这是一个教程:如何使用分层窗格

You need a to use a JLayeredPane for moving components in front of each other.

Here is a tutorial: How to use Layered Panes

秋意浓 2024-11-13 21:47:38

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

Disabled Glass Pane might help you out.

拿命拼未来 2024-11-13 21:47:38

目前还不清楚您的代码是如何组织的。然而,听起来您可能想要的是模式对话框。以下是包含许多参考资源的类似讨论的链接。

如何在 Swing java 中制作 JFrame 模态

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

你另情深 2024-11-13 21:47:38

使用 JXLayer 或 JIDE 可叠加

Use JXLayer or JIDE Overlayable.

我也只是我 2024-11-13 21:47:38
Jpanel main = new JPanel();
Jpanel a = new JPanel();
JPanel b = new Jpanel();

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

此时对象:

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

main.getComponentCount() = 2

main.setComponentZorder(b,0);


a -> 1
b -> 0;

b OVER
a DOWN
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
囚你心 2024-11-13 21:47:38

对于那些使用 JDialog 没有问题的人来说,这是在遇到问题时让它显示出来的可靠方法。只要确保在对话框是模态的、配置、设置焦点等时正确控制它即可。

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

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. Just make sure to control it properly if the dialog is modal, when disposing, setting focus etc.

JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文