简体   繁体   English

GlassPane表现怪异

[英]GlassPane behaves weird

I created a JPanel inside it i put a JCheckBox and two buttons: 我在其中创建了一个JPanel ,并放置了一个JCheckBox和两个按钮:

在此处输入图片说明

when checkbox is checked a glassPane with transparent color (alpha = 100) is showed above the panel and contains a label displaying the String "Loading...", the problem is that the location of checkbox changes when glassPane is displayed: 当选中复选框时,面板上方显示带有透明颜色(alpha = 100)的glassPane,并且包含显示String “ Loading ...”的标签,问题是当显示glassPane时,复选框的位置会更改:

在此处输入图片说明

and when i change the size of the frame the glassPane become opaque and all component which were visible under glasspane become invisible. 当我更改框架的大小时,glassPane变得不透明,并且在玻璃窗格下可见的所有组件都不可见。

在此处输入图片说明

and when i change the size to smaller size, the background of the glassPane changes and get darker and the label string "Loading" become blurred 当我将尺寸更改为较小尺寸时,glassPane的背景将更改并变暗,并且标签字符串“ Loading”变得模糊

在此处输入图片说明

Here is my code which is inspired by Oracle tutorial about GlassPane GlassPane Tutorial 这是我的代码,它是从有关GlassPane的Oracle教程启发而来的

package com.training.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class Training extends JFrame
{
    JCheckBox changeGlass;
    JButton button1, button2;

    public Training()
    {
        setBounds(100, 100, 400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Test Glass Pane");
        getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));

        JPanel glass = (JPanel) getGlassPane();
        glass.setOpaque(true);
        glass.setBackground(new Color(120, 120, 120, 100));
        glass.setLayout(new BorderLayout());
        glass.add(new JLabel("Loading ..."));

        changeGlass = new JCheckBox("Show Glass Pane", false);

        changeGlass.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                glass.setVisible(e.getStateChange() == ItemEvent.SELECTED);
            }
        });

        button1 = new JButton("Select");
        button2 = new JButton("Change");

        getContentPane().add(changeGlass);
        getContentPane().add(button1);
        getContentPane().add(button2);

        glass.addMouseListener(new MouseListener()
        {
            public void mouseReleased(MouseEvent e)
            {
                redispatchEvent(e, (JPanel) getContentPane(), glass);
            }

            public void mousePressed(MouseEvent e)
            {
                redispatchEvent(e, (JPanel) getContentPane(), glass);
            }

            public void mouseExited(MouseEvent e)
            {
                redispatchEvent(e, (JPanel) getContentPane(), glass);
            }

            public void mouseEntered(MouseEvent e)
            {
                redispatchEvent(e, (JPanel) getContentPane(), glass);
            }

            public void mouseClicked(MouseEvent e)
            {
                redispatchEvent(e, (JPanel) getContentPane(), glass);
            }
        });
    }

    public void redispatchEvent(MouseEvent e, JPanel panel, JPanel glass)
    {
        Point panelPoint = SwingUtilities.convertPoint(glass, e.getPoint(), panel);
        Component c = SwingUtilities.getDeepestComponentAt(panel, (int) panelPoint.getX(), (int) panelPoint.getY());

        Point componentPoint = SwingUtilities.convertPoint(panel, panelPoint, c);

        if (c != null && c.equals(changeGlass))
        {
            c.dispatchEvent(new MouseEvent(c, e.getID(), e.getWhen(), e.getModifiers(), (int) componentPoint.getX(), (int) componentPoint.getY(), e.getClickCount(), e
                    .isPopupTrigger()));
        }
        else
        {
            e.consume();
        }
    }
}

Can anyone correct me what i am doing wrong? 谁能纠正我我在做什么错?

Check out Backgrounds With Transparency for the probable reason for the problem and a couple of solutions. 请查看“ 透明背景” ,了解问题的可能原因和一些解决方案。 Basically because you are using a transparent background you need to make sure you paint the background of the glass pane yourself. 基本上因为您使用的是透明背景,所以需要确保自己绘制玻璃窗格的背景。

So I would create a custom panel to use as your glass pane so you can manage the painting of the background. 因此,我将创建一个自定义面板用作您的玻璃窗格,以便您可以管理背景的绘制。 Then you set this panel as the glass pane of the frame. 然后,将该面板设置为框架的玻璃窗格。

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

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