Question:
Java changing color Code?
John
2012-08-19 16:31:37 UTC
Hello am trying to write Java code to make four labels change color on mouse rollover or click, Don't Know How to start

import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Color;
import java.util.Random;


public class Color_Change extends JPanel implements MouseListener{

private JLabel one, two, three, four;

Random colorz = new Random();

Color_Change(){

this.setLayout(new BorderLayout());
this.setBackground(Color.white);

JPanel centerPanel = new JPanel(new FlowLayout());

one = new JLabel();
Dimension myLabel = new Dimension(100, 100);
one.setPreferredSize(myLabel);
one.setBackground(Color.white);
one.setOpaque(true);
one.addMouseListener(this);
centerPanel.add(one);

two = new JLabel();
Dimension myLabel2 = new Dimension(100, 100);
two.setPreferredSize(myLabel2);
two.setBackground(Color.red);
two.setOpaque(true);
two.addMouseListener(this);
centerPanel.add(two);

three = new JLabel();
Dimension myLabel3 = new Dimension(100, 100);
three.setPreferredSize(myLabel3);
three.setBackground(Color.blue);
three.setOpaque(true);
three.addMouseListener(this);
centerPanel.add(three);

four = new JLabel();
Dimension myLabel4 = new Dimension(100, 100);
four.setPreferredSize(myLabel4);
four.setBackground(Color.pink);
four.setOpaque(true);
four.addMouseListener(this);
centerPanel.add(four);

this.add(centerPanel, BorderLayout.CENTER);
}

public void mouseEntered(MouseEvent me)
{
if(me.getSource().equals (one))
{

}
if(me.getSource().equals (two))
{

}
if(me.getSource().equals (three))
{

}
if(me.getSource().equals (four))
{

}
}

public void mouseExited(MouseEvent me)
{

}

public void mousePressed(MouseEvent me)
{

}

public void mouseReleased(MouseEvent me)
{

}

public void mouseClicked(MouseEvent me)
{

}

public static void main(String[] args)
{
JFrame application = new JFrame(" ? ");
Color_Change panel = new Color_Change();
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(300,300);
application.setVisible(true);
}
}

Thank You
Four answers:
Rick
2012-08-20 04:58:31 UTC
From what i can see you have



- Created 4 labels with different background colours

- Created a mouse listener

- Assigned a frame with panels to add these labels to

- Created a random number generator



If i am reading this code correctly and assuming the mouse over works as a function (ive never collected focus on a jlabel like that before) then you should just be able to look inside the following parts



if(me.getSource().equals (one))

{



}



the above code section and the other three like it control the action to be performed when the mouse hits the identified jlabel.



you could then use a case statement and the random number generator to assign a random colour on mouse over



e.g



switch(colorz.Next(3)) //Where 3 means the result of the random will be 0,1 or 2

{

case 0:{one.setBackground(Color.red); break;}

case 1:{one.setBackground(Color.blue); break;}

case 2:{one.setBackground(Color.green); break;}

}



hopefully this will help but i will test it shortly and make sure im not giving you a completely wrong answer...



Good luck!

...



EDIT: just fixed some of the capital letter issues and the comment i left in a stupid place but otherwise what im trying to tell you to do is working. If you need more detail yell, but obviously try to figure it out from what ive given if its a learning excercise and consider reading up conditional statements in java to better understand why it works the way it does
AlduinIsBeast
2012-08-19 16:47:12 UTC
Double check ur syntax. Create some IFs on certain environment changes. Then either make the outcome be the changed color in that class file. Or create a seperate class.



19 Year Programmer and Video Game Designer



(Fluent in C, C++, C#, Java, Ajax, Basic (I know its useless), HTML (Up to HTML5), CSS, JavaScript, SQl, PHP, Objective C, Robotic C, and Action Script) - Come to me with programming questions.
saltsman
2016-08-01 12:45:21 UTC
The prior answerer is right.. You ought to add an action listener, like he said. The other thing fallacious is you do not use 'getContentPane()' with a JApplet.. That's for JFrames most effective. As a substitute create a new JPanel and add your stuff onto that. Subsequently use 'add(your_JPanel)' to include it within the JApplet. Excellent success!
Kevin B
2012-08-19 16:33:45 UTC
Everything is correct looks finshed


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...