将JPanel带到java中的其他对象(SWING)前面 [英] Bring JPanel to front of other objects in java (SWING)

查看:197
本文介绍了将JPanel带到java中的其他对象(SWING)前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在应用程序进程时进行加载,所以我在JTree上使用了JPanel。但是当用户点击JPanel时,JTree将选择并且JPanel会转到后面。隐藏jpanel之后它再也没有显示出来(我不知道为什么!但它似乎永远不会出现在jtree上)。

I want to make a loading when app process, so I used a JPanel over a JTree. But when user clicks on JPanel the JTreewill selected and the JPanel go to the back. also after hiding jpanel it never shows again(I don't know why! but it seems it never go forward of jtree).

我需要一种方法让JPanel转发任何东西。我怎么能这样做?

I need a way to bring the JPanel to forward of anything. How can I do this?

另外我必须提到我不想要JDialog。我想使用任何元素的jpanel顶部来显示加载直到进程完成。

Also I must mention that I don't want JDialog. I want use the jpanel top of any element to show a loading until a process finish.

推荐答案

所以这里至少有两个解决方案。或者选择@Geoff和@sthupahsmaht建议的内容。 BTW也可以使用JOptionPane,它会自动为你创建一个对话框。

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.

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

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

或者另一种选择是使用JLayeredPane作为@jzd建议。

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

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

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

1.点击开始时可见的玻璃窗格。查看输出。

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

2.右键单击它。这会隐藏玻璃窗格。

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

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

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

4.右键单击它。转到第1点。
享受。

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
如果你想要一个对话框的效果,你可以通过在我的例子中适当地合并这个代码来实现它。

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

这篇关于将JPanel带到java中的其他对象(SWING)前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆