-
Source code:
view plaincopy to clipboardprint?
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
//####################################
//Maybe sometimes there is nothing after running the program. You need to move the mouse on the screen to make the button appear.
//As for why, I don’t understand~~it needs to be studied in the future
//#####################################
public class testEventMouse extends JFrame{ //Main class, inherits the JFrame class, because the paint() method is used, so
//Must inherit a Frame class, otherwise I, a beginner, have not learned other methods that can call paint()
Container contentPanel; //Content grid reference
JButton b1,b2,b3,b4,b5;//These are five buttons
JRadioButton backGroundRadio,foreGroundRadio; //Define two radio button boxes
ButtonGroup radioGroup; //radio button group
int xValue,yValue; //Retain mouse position (x,y)
JFrame jf=this;//Assign the current object to jf, because when I later adjusted the code, I found that the background color is the content grid or other components
//To manage, the Frame frame class does not manage the background color; but the reason why I want to get the JFrame object is because the foreground color is only the frame class
//Can be managed, other components are not qualified to manage; if you do event processing later, you will find
public testEventMouse(){ //Constructor
super("08 Jiben Cao Feng--Simulated writing pad");
b1=new JButton("Black");//Implement five buttons
b2=new JButton("Blue");
b3=new JButton("Red");
b4=new JButton("White");
b5=new JButton("Yellow");
contentPanel=this.getContentPane();//Get the content pane
contentPanel.setLayout(new FlowLayout());//Set the layout manager of the content grid
backGroundRadio=new JRadioButton("backGround"); //Instantiate the check box
foreGroundRadio=new JRadioButton("foreGround");
contentPanel.add(backGroundRadio);//Add to the content grid
contentPanel.add(foreGroundRadio);
contentPanel.add(b1);//Add button
contentPanel.add(b2);
contentPanel.add(b3);
contentPanel.add(b4);
contentPanel.add(b5);
MouseListenerHandler mou=new MouseListenerHandler(); //Implement an event listening class
b1.addMouseListener(mou); //Register as a listener for five buttons
b2.addMouseListener(mou);
b3.addMouseListener(mou);
b4.addMouseListener(mou);
b5.addMouseListener(mou);
radioGroup=new ButtonGroup(); //Instance radio button group
radioGroup.add(backGroundRadio);//Combine two radio buttons into a group
radioGroup.add(foreGroundRadio);
addMouseMotionListener( //Anonymous class
new MouseMotionAdapter(){ //Because it is called by the current object, I made an adapter to play with.
public void mouseDragged(MouseEvent e){
xValue=e.getX();
yValue=e.getY();
repaint(); //Call the paint() method
}
}
);
setSize(1000,500); //If you want to do it, make it big
setVisible(true); //Set visible
}
public class MouseListenerHandler implements MouseListener{ //Mouse event listening class
public void mousePressed(MouseEvent e){ //In fact, there is only one useful thing, which is the mouse click event
//Because there are many objects to be monitored, it is not good to use an adapter.
if(e.getSource()==b1){ //Use getSouce() to obtain the object
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.BLACK);//Here comes the point, use the content grid to set the background color
else //#####################
jf.setForeground(Color.BLACK); //Use Frame to set the foreground color
}
if(e.getSource()==b2){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.BLUE);
else
jf.setForeground(Color.BLUE);
}
if(e.getSource()==b3){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.RED);
else
jf.setForeground(Color.RED);
}
if(e.getSource()==b4){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.WHITE);
else
jf.setForeground(Color.WHITE);
}
if(e.getSource()==b5){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.YELLOW);
else
jf.setForeground(Color.YELLOW);
}
}
public void mouseClicked(MouseEvent e) { //The following things are useless, but as interface methods, empty implementation
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
public void paint(Graphics g){ //Draw some small circles to confuse the human eye, haha
g.fillOval(xValue, yValue, 4, 4); //Draw a circle
}
public static void main(String args[]){
new testEventMouse(); //Anonymous class
}