GUI Graphics user Interrface graphical UI that allows users to do what they want by clicking the mouse.
The main is the practice interface settings Interface layout Add component settings component interface Rewrite listener
To make a semaphore, click on the radio button below to specify the signal lights, the code is as follows:
Package Com.lovo.homework1;import Java.awt.color;import Java.awt.graphics;import java.awt.event.itemevent;import Java.awt.event.itemlistener;import Javax.swing.buttongroup;import Javax.swing.jframe;import javax.swing.jradiobutton;/** * Class: Traffic light * @author Abe * Click the button to change the color */@SuppressWarnings ("Serial") public class TrafficLight Exte NDS JFrame implements Itemlistener{private jradiobutton[] lights = new Jradiobutton[3];p rivate string[] lightcolor = {"Red light" , "Yellow lamp", "green light"};p rivate color[] colors = {Color.red,color.yellow,color.green};p rivate int n = 0;/** * constructor */public Trafficlig HT () {//Window construction This.setsize (+-); This.setresizable (false); this.setdefaultcloseoperation (Exit_on_close); This.setlocationrelativeto (null); This.setlayout (null);//button Group Construction buttongroup Group = new Buttongroup (); for (int i = 0; i < Lights.length; i++) {Lights[i] = new Jradiobutton (lightcolor[i],true); Group.add (Lights[i]); This.add (Lights[i]); Lights[i]. Additemlistener (this); Lights[i].setbounds (I, 280, 60, 100);}} /** * Rewrite: PaiNT callback method */@Overridepublic void Paint (Graphics g) {super.paint (g); G.setcolor (Colors[n]); G.filloval (70, 60, 60); G.setcolor (Color.Black), G.drawrect (70, 60, 60); for (int i = 0; i < 3;i++) {G.drawoval (+/-) (+);}} /** * Main method: Set window visible */public static void Main (string[] args) {new TrafficLight (). setvisible (True); /** * Method: Monitor Button change */@Overridepublic void itemstatechanged (ItemEvent e) {Object obj = E.getsource (); if (obj = = Lights[0]) {n = 0;} else if (obj = = lights[1]) {n = 1;} else if (obj = = lights[2]) {n = 2;} Repaint ();}}
Java Programming (15.1)-----Graphical user interface GUI making traffic lights ~