Java Programming (15.1)-----Graphical user interface GUI making traffic lights ~

Source: Internet
Author: User

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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.