将组件放置在玻璃板上 [英] Placing component on Glass Pane

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

问题描述

我有一个 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?

终于想出了如何让简单的例子工作.谢谢,@akf.我能够使这个解决方案适应我的原始问题,允许我删除大约 60 行手动呈现 JLabel 表示的 Java2D 代码.

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

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

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