java - JLabel 的背景颜色在更新 setText() 时发生变化

标签 java swing jlabel

编辑(导致问题的不是不透明属性,而是更新 JLabel 的背景属性): 我正在使用 MouseMotionListener 将 JLabel 的 setText() 设置为鼠标的当前位置。 JLabel 在程序第一次运行时以正确的背景颜色/透明度开始。每当更新 text/mouseMotion 时,JLabel 就不再透明。

更新的可运行代码:

例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class MouseTester extends JFrame {
public static void main(String[] args) {
try {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            MouseTester.createMouseTester();
        }
    });
} catch (Throwable t) {
    System.exit(1);
}
}
private static MouseTester mt = null;
private JLabel mouseLocation = null;
private static Color labelBackgroundColor = new Color(0, 0, 0, 127);
private static Color labelForegroundColor = Color.WHITE;

public static void createMouseTester() {
      if (mt != null)
          return;
      mt = new MouseTester();
      mt.setVisible(true);
}

private MouseTester() {
      super();
      mt = this;
      setResizable(true);
      Dimension dScreen = Toolkit.getDefaultToolkit().getScreenSize();
      setMinimumSize(new Dimension(Math.min(800, dScreen.width), Math.min(590,
      dScreen.height)));
      setSize(getMinimumSize());
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      mouseLocation = new JLabel(" Lat/Long ");
      mouseLocation.setOpaque(true);
      mouseLocation.setBackground(labelBackgroundColor);
      mouseLocation.setForeground(labelForegroundColor);
      mouseLocation.setToolTipText("The MGRS coordinates.");

      Component textArea = new TextArea("Move mouse here to see mouse motion info...");

      // Add a mouse motion listener to capture mouse motion events
      textArea.addMouseMotionListener(new MouseMotionAdapter() {

public void mouseMoved(MouseEvent evt) {
      TextArea source = (TextArea) evt.getSource();
          // Process current position of cursor while all mouse buttons are up.
            mouseLocation.setText(source.getText() + "\nMouse moved [" + 
          evt.getPoint().x + "," + evt.getPoint().y + "]");
            mouseLocation.setBackground(labelBackgroundColor);
            mouseLocation.setForeground(labelForegroundColor);
            mouseLocation.setOpaque(true);
            mouseLocation.repaint();

      }
      public void mouseDragged(MouseEvent evt) {

      }
  });

  // Add the components to the frame; by default, the frame has a border layout
        mt.add(textArea, BorderLayout.NORTH);
        mouseLocation.setOpaque(true);
        mouseLocation.setBackground(labelBackgroundColor);
        mouseLocation.setForeground(labelForegroundColor);
        mt.add(mouseLocation, BorderLayout.SOUTH);

        int width = 300;
        int height = 300;
        mt.setSize(width, height);
  }
}

JLabel 开始时透明/略带灰色,然后随鼠标移动变为不透明和完全黑色。透明度由背景颜色决定。

我几乎尝试过改变我能想到的所有地方的背景颜色,但它不起作用..

我希望它一直保持颜色(启动时的颜色)。

最佳答案

您已声明您的 JLabel 是不透明的,这意味着它完全负责绘制自己的背景。但是您已将其背景颜色设置为半透明颜色。这是一个矛盾,也是你的问题的原因。

您可以使用 mt.repaint(); 而不是 mouseLocation.repaint(); 来修复 JLabel 的外观,从而强制重新绘制该区域JLabel 后面的整个面板(灰色),然后以半透明颜色重新绘制 JLabel。

如果您想避免重新绘制整个 mt 对象的成本,那么您需要将 JLabel 嵌套在一些可以快速重新绘制的较小组件中。

关于java - JLabel 的背景颜色在更新 setText() 时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10954975/

相关文章:

Swing GUI 不等待用户输入

java - 求新数字的基本计算器方法

java - 褪色算法?

java - 从组件更改 JLabel ICON

java - 更改 JLabel 的文本 - 初学者

java - 从复杂的空元素中解析 XML 属性

java - LogCat 垃圾邮件 GC_FOR_MALLOC 但只使用了 50% 的内存

java - 重复数字

java - 如何从 Mac Os X 将图标添加到 Java (NetBeans) 中?

java - Spark 将 JavaPairDStream 流式传输到文本文件