gpt4 book ai didi

java - GlassPane 行为怪异

转载 作者:行者123 更新时间:2023-11-30 07:05:42 26 4
gpt4 key购买 nike

我创建了一个 JPanel我在里面放了一个 JCheckBox和两个按钮:

enter image description here

选中复选框时,面板上方会显示一个透明色 (alpha = 100) 的玻璃 Pane ,并包含一个显示 String 的标签“正在加载...”,问题是当显示 glassPane 时复选框的位置发生变化:

enter image description here

当我改变框架的大小时,glassPane 变得不透明,所有在 glasspane 下可见的组件都变得不可见。

enter image description here

当我将尺寸更改为更小尺寸时,glassPane 的背景发生变化并变暗,标签字符串“Loading”变得模糊

enter image description here

这是我的代码,灵感来自关于 GlassPane 的 Oracle 教程 GlassPane Tutorial

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

谁能纠正我做错了什么?

最佳答案

查看 Backgrounds With Transparency对于问题的可能原因和一些解决方案。基本上,因为您使用的是透明背景,所以您需要确保自己绘制玻璃 Pane 的背景。

所以我会创建一个自定义面板用作您的玻璃面板,这样您就可以管理背景的绘制。然后将此面板设置为框架的玻璃面板。

关于java - GlassPane 行为怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26442334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com