B
Bruce .J Sam
My two java file are list below:
/* ************************************************************ */
//MyContainer.java
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class MyContainer extends JApplet {
private MyButton[] myButton = new MyButton[2];
public MyContainer() {
myButton[0] = new MyButton(5, 5, 200, 30);
myButton[1] = new MyButton(5, 40, 200, 30);
getContentPane().setLayout(null);
for (int i = 0; i < myButton.length; i++) {
System.out.println("Add myButton " + (i+1));
getContentPane().add(myButton);
myButton.addMouseListener(myButton);
}
}
public void paint(Graphics g) {
for (int i = 0; i < myButton.length; i++) {
myButton.paint(g);
}
}
public static void main(String[] args) {
MyContainer container = new MyContainer();
JFrame f = new JFrame();
f.getContentPane().add(container);
f.pack();
f.setSize(new Dimension(300, 200));
f.show();
}
}
/* ************************************************************ */
//MyButton.java
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MyButton extends Component implements MouseListener {
private Color m_clickedColor = new Color(0,128,255);
private Color m_pressedColor = new Color(128,0,255);
private Color m_releasedColor = new Color(128,64,0);
private Color m_enteredColor = new Color(0,128,0);
private Color m_exitedColor = new Color(255,0,0);
private Color m_rectColor;
public MyButton(int x, int y, int width, int height) {
setBounds(x, y, width, height);
m_rectColor = m_exitedColor;
}
public void mouseClicked(MouseEvent arg0) {
System.out.println("Mouse is clicked!");
m_rectColor = m_clickedColor;
repaint();
}
public void mousePressed(MouseEvent arg0) {
System.out.println("Mouse is pressed!");
m_rectColor = m_pressedColor;
repaint();
}
public void mouseReleased(MouseEvent arg0) {
System.out.println("Mouse is released!");
m_rectColor = m_releasedColor;
repaint();
}
public void mouseEntered(MouseEvent arg0) {
System.out.println("Mouse is entered!");
m_rectColor = m_enteredColor;
repaint();
}
public void mouseExited(MouseEvent arg0) {
System.out.println("Mouse is exited!");
m_rectColor = m_exitedColor;
repaint();
}
public void paint(Graphics g) {
System.out.println(this);
System.out.println(m_rectColor);
g.setColor(m_rectColor);
g.fillRect(getBounds().x, getBounds().y,
getBounds().width, getBounds().height);
}
}
/* ************************************************************* */
What I want to do is when I pressed the button, clicked or so on,
the color of the button will change.But in fact, the program doesn't
work well.
/* ************************************************************ */
//MyContainer.java
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class MyContainer extends JApplet {
private MyButton[] myButton = new MyButton[2];
public MyContainer() {
myButton[0] = new MyButton(5, 5, 200, 30);
myButton[1] = new MyButton(5, 40, 200, 30);
getContentPane().setLayout(null);
for (int i = 0; i < myButton.length; i++) {
System.out.println("Add myButton " + (i+1));
getContentPane().add(myButton);
myButton.addMouseListener(myButton);
}
}
public void paint(Graphics g) {
for (int i = 0; i < myButton.length; i++) {
myButton.paint(g);
}
}
public static void main(String[] args) {
MyContainer container = new MyContainer();
JFrame f = new JFrame();
f.getContentPane().add(container);
f.pack();
f.setSize(new Dimension(300, 200));
f.show();
}
}
/* ************************************************************ */
//MyButton.java
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MyButton extends Component implements MouseListener {
private Color m_clickedColor = new Color(0,128,255);
private Color m_pressedColor = new Color(128,0,255);
private Color m_releasedColor = new Color(128,64,0);
private Color m_enteredColor = new Color(0,128,0);
private Color m_exitedColor = new Color(255,0,0);
private Color m_rectColor;
public MyButton(int x, int y, int width, int height) {
setBounds(x, y, width, height);
m_rectColor = m_exitedColor;
}
public void mouseClicked(MouseEvent arg0) {
System.out.println("Mouse is clicked!");
m_rectColor = m_clickedColor;
repaint();
}
public void mousePressed(MouseEvent arg0) {
System.out.println("Mouse is pressed!");
m_rectColor = m_pressedColor;
repaint();
}
public void mouseReleased(MouseEvent arg0) {
System.out.println("Mouse is released!");
m_rectColor = m_releasedColor;
repaint();
}
public void mouseEntered(MouseEvent arg0) {
System.out.println("Mouse is entered!");
m_rectColor = m_enteredColor;
repaint();
}
public void mouseExited(MouseEvent arg0) {
System.out.println("Mouse is exited!");
m_rectColor = m_exitedColor;
repaint();
}
public void paint(Graphics g) {
System.out.println(this);
System.out.println(m_rectColor);
g.setColor(m_rectColor);
g.fillRect(getBounds().x, getBounds().y,
getBounds().width, getBounds().height);
}
}
/* ************************************************************* */
What I want to do is when I pressed the button, clicked or so on,
the color of the button will change.But in fact, the program doesn't
work well.