1

編集(問題を引き起こしているのは不透明な属性ではなく、JLabelの背景属性を更新しています):マウスの現在の位置が何であれ、JLabelのsetText()にMouseMotionListenerを使用しています。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 は透明/わずかにグレーで始まり、マウスの動きによって透明ではなく完全に黒に変わります。透明度は背景色で決まります。

思いついた場所の背景色を変更しようとしましたが、うまくいきません..

ずっと色のままにしたい(起動時の色)。

4

3 に答える 3

4

JLabel が不透明であることを宣言しました。これは、独自の背景を描画する責任があることを意味します。それでも、背景色を半透明色に設定しました。それは矛盾であり、問​​題の原因です。

JLabelの背後にあるパネル全体の領域 (グレー) を強制的に再描画し、続いて半透明の色で JLabel を再描画するmt.repaint();代わりに、を使用して JLabel の外観を修正できます。mouseLocation.repaint();

オブジェクト全体を再描画するコストを回避したい場合は、mtすばやく再描画できる小さなコンポーネント内に JLabel をネストする必要があります。

于 2012-06-09T00:25:20.633 に答える
3

ラベルの透明度を変更する理由がわかりません。以下に示すように、ラベルを不透明にし、背景の彩度を調整するだけで十分な場合があります。

実装に関するいくつかの注意事項:

  • イベント ディスパッチ スレッドを使用していただきありがとうございます。
  • レイアウトに任せましょう。setSize() 控えめに使用してください。
  • AWT と Swing コンポーネントを不必要に混ぜないでください。
  • 例外を飲み込まないでください。

ここに画像の説明を入力

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);
    }
}
于 2012-06-09T14:17:49.367 に答える
3

から起動されたものごとに、 からへJLabel#repaint()の切り替えを呼び出す必要があります。このメソッドは API で欠落しているため、残りは@kleapatraによる説明とともにここにありますOpaque(true)Opaque(false)MouseEvents(Xxx)MouseListeners

于 2012-06-08T20:07:09.790 に答える