written in front:
Top two blogs we introduced the basic structure of the simple Java GUI and the event monitoring mechanism respectively. This time we will introduce the dual event (multi event) listening mechanism and introduce the inner class. 1. Design Tasks
Design a GUI, including basic components: button (two), label (one), Random color Circle-panel (one), request to click on one of the buttons can change the label text, click on another button can change the color of the circle, realize double event monitoring. 2. Mission Analysis
The difficulty with this task is how to monitor both events simultaneously. We already know that to realize the event listening, we must implement the ActionListener interface and implement the Actionperformed method concretely. But note that for any class that implements the ActionListener interface, only one actionperformed method can be implemented, so how to monitor two different buttons and have different actionperformed methods to handle it. We use internal classes to solve this problem. The form of the inner class is as follows:
Class Outer {
int outer_int;
Class Inner {
int inner_int;
}
One advantage of using an inner class is that in an inner class, you can directly use the properties and methods in the external class. We use the following code to try to implement this design task. 3. Code Version1
Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;
public class Newtwobuttons {JFrame frame;
JLabel label;
public static void Main (string[] args) {newtwobuttons TB = new Newtwobuttons ();
Tb.go ();
public void Go () {frame = new JFrame ();
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
label = new JLabel ("I am Waiting for you!");
JButton Labelbutton = new JButton ("Change a label");
Labelbutton.addactionlistener (New Labellistener ());
JButton Circlebutton = new JButton ("Change a Circle");
Circlebutton.addactionlistener (New Circlelistener ());
Mydrawpanel Mypanel = new Mydrawpanel ();
Frame.getcontentpane (). Add (Borderlayout.east, Labelbutton);
Frame.getcontentpane (). Add (Borderlayout.south, Circlebutton);
Frame.getcontentpane (). Add (borderlayout.west, label);
Frame.getcontentpane (). Add (Borderlayout.center, Mypanel); frame.seTsize (300, 300);
Frame.setvisible (TRUE);
Class Labellistener implements ActionListener {public void actionperformed (ActionEvent event) {
Label.settext ("ohch!");
} class Circlelistener implements ActionListener {public void actionperformed (ActionEvent event) {
Frame.repaint (); '}} class Mydrawpanel extends JPanel {public void paintcomponent (Graphics g) {//The ' is ' called every
Time the button is clicked G.fillrect (0, 0, this.getwidth (), This.getheight ());
int red = (int) (Math.random () * 255);
int green = (int) (Math.random () * 255);
int blue = (int) (Math.random () * 255);
Color Randomcolor = new color (red, green, blue); G.setcolor (Randomcolor); Set random Color G.filloval (70, 70, 100, 100); Make a oval (circle)}}
4. Test results
Size the window appropriately to show all the components, as shown in the following illustration.
Click the "Change a Circle" button and the results are as follows:
Explains that the change a circle button works fine.
Next click on the "Change a Label" button, and the result is as shown in the following figure:
We see that the label text on the left side of the screen changes, stating that the "Change a label" button works fine. No. Wait a minute. We were surprised to find that the color of the circle changed, but we did not click on the "Change a Circle" button.
Why is that so? 5. What is the problem
In fact, when we try to pull the window resize, the color of the circle will change, as shown below:
Then it can be inferred that the Paintcomponent function executes when the Panel component size changes (for example, by clicking the "Change a Label" button to widen the left area of the panel or directly pull the window widened). What we want is that, in addition to the first generation of graphics, the Paintcomponent function is executed when and only when the "Change a Circle" button is clicked. 6. The Solution
From the above analysis we can initially give a solution: Set flag, when flag is false, do not perform the paintcomponent function, only if it is true. The code looks like this: 7. Code Version2
Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;
public class Newtwobuttons {JFrame frame;
JLabel label;
Boolean flag = false;
public static void Main (string[] args) {newtwobuttons TB = new Newtwobuttons ();
Tb.go ();
public void Go () {frame = new JFrame ();
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
label = new JLabel ("I am Waiting for you!");
JButton Labelbutton = new JButton ("Change a label");
Labelbutton.addactionlistener (New Labellistener ());
JButton Circlebutton = new JButton ("Change a Circle");
Circlebutton.addactionlistener (New Circlelistener ());
Flag = true;
Mydrawpanel Mypanel = new Mydrawpanel ();
Frame.getcontentpane (). Add (Borderlayout.east, Labelbutton);
Frame.getcontentpane (). Add (Borderlayout.south, Circlebutton);
Frame.getcontentpane (). Add (borderlayout.west, label); Frame.getcontentpane (). Add (BOrderlayout.center, Mypanel);
Frame.setsize (300, 300);
Frame.setvisible (TRUE);
Class Labellistener implements ActionListener {public void actionperformed (ActionEvent event) {
Label.settext ("ohch!");
} class Circlelistener implements ActionListener {public void actionperformed (ActionEvent event) {
Flag = true;
Frame.repaint (); } class Mydrawpanel extends JPanel {public void paintcomponent (Graphics g) {//This method is Cal
Led every time the button is clicked G.fillrect (0, 0, this.getwidth (), This.getheight ());
if (flag = = true) {int red = (int) (Math.random () * 255);
int green = (int) (Math.random () * 255);
int blue = (int) (Math.random () * 255);
Color Randomcolor = new color (red, green, blue); G.setcolor (Randomcolor);
Set Random Color G.filloval (70, 70, 100, 100);
Make a oval (circle) flag = false;
}
}
}
}
8. There are still problems.
The test found that although now only click on the button to change the color of the circle, but when the window size changes, the panel circle does not show (that is, the Paintcomponent function only executes a statement), which is not what we want to result. 9. Final Programme
To solve this problem, our final solution is to make color a public variable, so that when the window is extended, the paintcomponent function is still executed, but the color of the drawing does not change the previous time. The code looks like this: 10. Code Version3
Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;
public class Newtwobuttons {JFrame frame;
JLabel label;
Boolean flag = false;
int red = 0, green = 0, blue = 0;
public static void Main (string[] args) {newtwobuttons TB = new Newtwobuttons ();
Tb.go ();
public void Go () {frame = new JFrame ();
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
label = new JLabel ("I am Waiting for you!");
JButton Labelbutton = new JButton ("Change a label");
Labelbutton.addactionlistener (New Labellistener ());
JButton Circlebutton = new JButton ("Change a Circle");
Circlebutton.addactionlistener (New Circlelistener ());
Flag = true;
Mydrawpanel Mypanel = new Mydrawpanel ();
Frame.getcontentpane (). Add (Borderlayout.east, Labelbutton);
Frame.getcontentpane (). Add (Borderlayout.south, Circlebutton); Frame.getcontentpane (). Add (borderlayout.west, label);
Frame.getcontentpane (). Add (Borderlayout.center, Mypanel);
Frame.setsize (300, 300);
Frame.setvisible (TRUE);
Class Labellistener implements ActionListener {public void actionperformed (ActionEvent event) {
Label.settext ("ohch!");
} class Circlelistener implements ActionListener {public void actionperformed (ActionEvent event) {
Flag = true;
Frame.repaint (); } class Mydrawpanel extends JPanel {public void paintcomponent (Graphics g) {//This method is called
Every time the button is clicked G.fillrect (0, 0, this.getwidth (), This.getheight ());
if (flag = = true) {red = (int) (Math.random () * 255);
Green = (int) (Math.random () * 255);
blue = (int) (Math.random () * 255);
Color Randomcolor = new color (red, green, blue); G.setcolor (Randomcolor); //Set Random Color G.filloval (70, 70, 100, 100);
Make a oval (circle) flag = false;
else {color RandomColor2 = new color (red, green, blue); G.setcolor (RANDOMCOLOR2); Set random Color G.filloval (70, 70, 100, 100);
Make a oval (circle)}}}