将组件放在Glass Pane上 [英] Placing component on Glass Pane

查看:91
本文介绍了将组件放在Glass Pane上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JLabel的子类,它构成了我GUI的一个组件。我已经实现了将组件从一个容器拖放到另一个容器的能力,但没有任何视觉效果。我想让这个JLabel在项目从一个容器拖动到另一个容器时跟随光标。我想我可以创建一个玻璃窗格并在那里绘制它。但是,即使我将组件添加到玻璃窗格,将组件设置为可见,并将玻璃窗格设置为可见,并将玻璃窗格设置为不透明,我仍然看不到该组件。我知道该组件有效,因为我可以将其添加到内容窗格中并显示它。

I have a subclass of JLabel that forms a component of my GUI. I have implemented the ability to drag and drop the component from one container to another, but without any visual effects. I want to have this JLabel follow the cursor during the drag of the item from one container to another. I figured that I could just create a glass pane and draw it on there. However, even after I add the component to the glass pane, set the component visible, and set the glass pane visible, and set the glass pane as opaque, I still so not see the component. I know the component works because I can add it to the content pane and have it show up.

如何将组件添加到玻璃窗格?

How do I add a component to the glass pane?

最后想出了如何让简单的例子正常工作。谢谢,@ kff。我能够使这个解决方案适应我原来的问题,允许我删除大约60行Java2D代码,这些代码手动呈现JLabel的表示。

Finally figured how to get the simple example working. Thanks, @akf. I was able to adapt this solution to my original problem, allowing me to remove ~60 lines of Java2D code that manually rendered a representation of the JLabel.

package test;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MainFrame extends JFrame {

    /**
     * @param args
     */
    public static void main(String[] args) {
        MainFrame mf = new MainFrame();
        mf.setSize(400, 400);
        mf.setLocationRelativeTo(null);
        mf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        mf.setGlassPane(new JPanel());

        JLabel l = new JLabel();
        l.setText("Hello");
        l.setBorder(new LineBorder(Color.BLACK, 1));
        l.setBounds(10, 10, 50, 20);
        l.setBackground(Color.RED);
        l.setOpaque(true);
        l.setPreferredSize(l.getSize());

        //mf.add(l);
        ((JPanel)mf.getGlassPane()).add(l);
        mf.getGlassPane().setVisible(true);

        mf.setVisible(true);
    }
}


推荐答案

除此之外指向已提供的LayerPane示例的指针,原始代码的问题在于标签首选大小的设置。你在JLabel大小之前设置它,所以你的:

Besides the pointers to the LayerPane examples already provided, the issue with your original code centers around the setting of the preferred size of your label. You set it before the JLabel has been sized, so your:

l.setPreferredSize(l.getSize());

无效。另一方面,如果您在打电话给 setBounds 后拨打该电话,您将看到所需的结果。考虑到这一点,请重新排序:

is ineffectual. If, on the other hand, you make that call after you make your call to setBounds, you will see your desired results. With that in mind, reorder this:

l.setPreferredSize(l.getSize());
l.setBounds(10, 10, 50, 20);

看起来像这样:

l.setBounds(10, 10, 50, 20);
l.setPreferredSize(l.getSize());

这篇关于将组件放在Glass Pane上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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