By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

change panels color on mouseover

P: 3
Hi,
if somebody coud help me .
It`s a beginner`s question but I`ve tryed to solve it for three days.
I have several panels and I need to change their color when the
mouse is over them.
If someone has any ideas......
Thanks.
Dec 4 '06 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
Hi,
if somebody coud help me .
It`s a beginner`s question but I`ve tryed to solve it for three days.
I have several panels and I need to change their color when the
mouse is over them.
If someone has any ideas......
Thanks.
Post the code you have so far so we have something to work with.
Dec 5 '06 #2

P: 3
here is the code :

Expand|Select|Wrap|Line Numbers
  1. package myprojects.SokSzinesPanel;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.lang.*;
  5.  
  6. public class SokSzinesPanelFrame extends JFrame {
  7.  
  8.         public static void main(String args[])
  9.     {
  10.         SokSzinesPanelFrame win = new SokSzinesPanelFrame ();
  11.  
  12.         java.awt.EventQueue.invokeLater(new Runnable() {
  13.             public void run()
  14.             {
  15.                 new SokSzinesPanelFrame().setVisible(true);
  16.  
  17.             }
  18.         });
  19.     }
  20.  
  21.      public SokSzinesPanelFrame() {
  22.          super ("Ablak");
  23.          addMenus();
  24.          addComponents();
  25.         pack();
  26.          resize(400,400);
  27.          show();
  28.          }
  29.         void addMenus() {
  30.          MenuBar menubar = new MenuBar();
  31.          Menu file = new Menu("File");
  32.          file.add("Quit");
  33.          menubar.add(file);
  34.          setMenuBar(menubar);
  35.         }        
  36.  
  37.    void addComponents() {
  38.  
  39.         JPanel panel =new JPanel(); 
  40.         panel.setBackground(java.awt.Color.cyan);
  41.         setLayout(new GridLayout(2,1));
  42.         add("North",panel);
  43.         JPanel panel1 =new JPanel();
  44.         panel1.setBackground(java.awt.Color.green);
  45.         panel1.setSize(100,100);
  46.         add("South",panel1);
  47.         JPanel panel2 =new JPanel();
  48.         panel2.setBackground(java.awt.Color.magenta);
  49.         panel2.setSize(100,100);
  50.         add("Center",panel2);
  51.         JPanel panel3 =new JPanel();
  52.         panel3.setBackground(Color.orange);
  53.         panel3.setSize(100,100);
  54.         add("West",panel3);
  55.  
  56.  
  57.          addMouseListener(new MouseAdapter() {
  58.  
  59.  
  60.             public void mouseEntered(MouseEvent e)
  61.             {
  62.  
  63.                 Color c = new Color((float)Math.random(), (float)Math.random(), 
  64.                         (float)Math.random());
  65.  
  66.  
  67.                 getContentPane().setBackground(c);
  68.             }
  69.         });
  70.  
  71.  
  72.       }
  73.  
  74.  
  75.  
  76. /*public boolean handleEvent(Event event) {
  77.     if (event.id==Event.WINDOW_DESTROY){
  78.         System.exit(0);
  79.         return true;
  80.     }else if (event.id==Event.ACTION_EVENT &&
  81.        event.target instanceof MenuItem) {
  82.            if ("Quit".equals(event.arg)) {
  83.                System.exit(0);
  84.                return true;
  85.            }else{
  86.                return false;
  87.            }
  88.        }else if(event.id==Event.ACTION_EVENT &&
  89.             event.target instanceof Panel){
  90.                 return true;
  91.        }else return false;
  92. }*/
  93.  
  94.  
  95. }
  96.  
Dec 5 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.