Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure why you're changing the label's <a href="https://stackoverflow.com/a/2234020/230513">transparency</a>. Making the label <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props" rel="nofollow noreferrer">opaque</a> and adjusting it's background <a href="https://stackoverflow.com/questions/2123841/javax-swing-timer-repeats-fine-but-actionlistener-doesnt-do-anything/2124507#2124507">saturation</a> may be sufficient, as shown below.</p> <p>A few notes on your implmentation:</p> <ul> <li>Kudos for using the <a href="http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html" rel="nofollow noreferrer">event dispatch thread</a>.</li> <li>Let the layout do the work; use <code>setSize()</code> <a href="https://stackoverflow.com/a/7229662/230513">sparingly</a>.</li> <li>Don't mix AWT and Swing components needlessly. </li> <li>Don't swallow exceptions.</li> </ul> <p><img src="https://i.stack.imgur.com/MnBKC.png" alt="enter image description here"></p> <pre><code>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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload