Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #219

Re: MouseListener - Chang

From "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this>
Subject Re: MouseListener - Chang
Message-ID <ekgqqg$r9i$1@registered.motzarella.org> (permalink)
Newsgroups comp.lang.java.gui
References <1164700226.141122.130840@14g2000cws.googlegroups.com>
Date 2011-04-27 15:26 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
Rick schrieb:
> Here is the code I have.  Basically all I want to do is change the
> background of the frame to black when I click the mouse (mouseClicked)
> and change it back to white when I release the mouse (mouseReleased).
> I can't access frame directly to do frame.setBackground(Color.BLACK),
> and that is where I am stuck.

That's not the way it works. A frame is - hmm... a frame. What you see 
on screen within a frame is not the frame but a container. Imagine the 
frame as the window border.

A possible solution:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test {
     private JPanel getContentPanel() {
         JPanel panel = new JPanel();
         panel.addMouseListener( new MouseAdapter() {
             public void mouseClicked( MouseEvent e ) {
                 ((JComponent)e.getSource()).setBackground(Color.BLACK);
             }
         });
         panel.setPreferredSize( new Dimension(600,400) );
         return panel;
     }

     public void createAndShowGUI() {
         JFrame frame = new JFrame("Test");
         frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
         frame.setContentPane( getContentPanel() );
         frame.pack();
         frame.setVisible( true );
     }

     public static final void main( String args[] ) {
         SwingUtilities.invokeLater( new Runnable() {
             public void run() {
                 new Test().createAndShowGUI();
             }
         });
     }
}

Bye
Michael

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Find similar


Thread

MouseListener - Changing "Rick" <rick@THRWHITE.remove-dii-this> - 2011-04-27 15:26 +0000
  Re: MouseListener - Chang "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:26 +0000

csiph-web