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

时间:2012-06-08 19:29:29

标签: java swing jlabel

编辑(不是导致问题的opaque属性,它正在更新JLabel的background属性): 我正在使用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开始透明/略带灰色,然后随着鼠标移动而变化,不透明,全黑。透明度以背景颜色确定。

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

我希望它能保持整个时间的颜色(它在启动时的颜色)。

3 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:3)

对于从JLabel#repaint()解雇的每个Opaque(true),您必须致电Opaque(false)以便从MouseEvents切换到(Xxx)MouseListeners,反之亦然,因为此方法错过了API,休息是here with description by @kleapatra

答案 2 :(得分:3)

我不确定你为什么要更改标签的transparency。制作标签opaque并调整其背景saturation可能就足够了,如下所示。

关于你的实施的几点说明:

  • 赞赏使用event dispatch thread
  • 让布局完成工作;使用setSize() sparingly
  • 不要不必要地混合AWT和Swing组件。
  • 不要吞下例外。

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class MouseTester extends JFrame {
    private static MouseTester mt;
    private static Color labelBackgroundColor = Color.gray;
    private static Color labelForegroundColor = Color.white;
    private JLabel mouseLocation;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MouseTester.createMouseTester();
            }
        });
    }

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

    private MouseTester() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mouseLocation = new JLabel("Lat/Long", JLabel.CENTER);
        mouseLocation.setOpaque(true);
        mouseLocation.setBackground(labelBackgroundColor);
        mouseLocation.setForeground(labelForegroundColor);
        mouseLocation.setToolTipText("The MGRS coordinates.");
        JTextArea textArea = new JTextArea(
            "Move mouse here to see mouse motion info...");
        textArea.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseMoved(MouseEvent me) {
                mouseLocation.setText("Mouse moved ["
                    + me.getX() + ", " + me.getY() + "]");
            }
        });
        this.add(textArea, BorderLayout.CENTER);
        this.add(mouseLocation, BorderLayout.SOUTH);
        this.pack();
        this.setSize(320, 240); // content placeholder
        this.setLocationRelativeTo(null);
    }
}