The Jradiobutton of Java game development

Source: Internet
Author: User

? Copyright NOTICE: This article is the original blogger article, reproduced please indicate the source

1. button (JButton)

The button in swing is JButton, which is a subclass of the Javax.swing.AbstractButton class, the button in swing can display the image, and the button can be set as the default icon for the window, and multiple images can also be assigned to a button

Common construction Methods for JButton:

JButton icon: Icons displayed on buttons

JButton (String text): Text displayed on the button

JButton (String Text, icon icon): Icon is displayed on the button and text is displayed

Common methods of JButton:

SetText (String text): Sets the label text of the button

SetIcon (Icon DefaultIcon): Sets the picture that the button displays in the default state

Setrollovericon (Icon Rollovericon): Sets the picture to display when the cursor moves over the button

Setpressedicon (Icon Pressedicon): Sets the picture to display when the button is pressed

Setcontentareafilled (Boolean B): Sets the background of the button to be transparent, which is transparent when set to false, and is opaque by default

Setborderpainted (Boolean B): Set to not draw the border of the button, when set to false means no drawing, default to draw

2. Radio button (Jradiobutton)

A radio button implemented by the Jradiobutton component. Can be used alone or in conjunction with the Buttongroup class, when used alone, the radio button can be selected and deselected; When used in conjunction with Buttongroup, the user can select only one radio button in a group of buttons

Common methods of Jradiobutton:

SetText (String text): Sets the label text of a radio button

SetSelected (Boolean B): Sets the state of a radio button that is not selected by default and indicates that a radio button is selected when set to True

Add (AbstractButton B): Adds a button to a group of buttons

Remove (AbstractButton B): Remove a button from a group of buttons

Getbuttoncount (): Returns the number of buttons contained in a group of buttons

GetElements (): Returns an object of type enumeration that can traverse all button objects contained in a group of buttons

IsSelected (): Returns the status of the radio button

SetSelected (Boolean B): Sets the state of a radio button

3. Case: Select the city that the user likes

3.1 Project Structure

3.2 Pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId> Org.swing</groupid> <artifactId>swing</artifactId> <version>0.0.1-snapshot</version > <packaging>jar</packaging> <name>swing</name> <url>http://maven.apache.org< /url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </ properties> <dependencies> <dependency> <groupId>junit</groupId> <ARTIFACTID&G t;junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependen Cy> </dependencies></project> 

3.3 jradiobuttontest.java

Package Org.swing.jradiobutton;import Java.awt.container;import Java.awt.flowlayout;import java.awt.GridLayout; Import Java.awt.event.windowadapter;import Java.awt.event.windowevent;import Javax.swing.borderfactory;import Javax.swing.buttongroup;import Javax.swing.jframe;import Javax.swing.jpanel;import Javax.swing.JRadioButton; public class Jradiobuttontest {JFrame f = null;public jradiobuttontest () {f = new JFrame ("Radio Box Example");//Create a JFrame window object contain Er contentpane = F.getcontentpane ();//Create a Content panel container contentpane.setlayout (new FlowLayout ());//Set the layout of the window JPanel p1 = new JPanel ();//Create a Panel object P1.setlayout (new GridLayout (1, 3));//Set Layout manager format P1.setborder (Borderfactory.createtitledborder ( "Choose the city You Like");//define 3 Jradiobutton radio buttons Jradiobutton r1 = new Jradiobutton ("Beijing"); Jradiobutton r2 = new Jradiobutton ("Shanghai"); Jradiobutton R3 = new Jradiobutton ("Guangzhou");//Create a button group object, bind the button to a group, the implementation can only select a buttongroup group = new Buttongroup (); Group.add ( R1); Group.add (R2); Group.add (R3);//Add the button object to the Panel P1.add (R1);p 1.add (R2);p 1.add (R3); R1.SetSelected (TRUE);//Set R1 radio button status to select//Add Panel object to Content panel container contentpane.add (p1); F.pack ();//auto-adjust window size f.setvisible (TRUE); /Display window F.addwindowlistener (new Windowadapter () {//Add a window listener @overridepublic void windowclosing (WindowEvent e) { System.exit (0);}}); public static void Main (string[] args) {new jradiobuttontest ();}}

3.4 Effect Preview

Reference: "Java games into principles and practical tutorials"

The Jradiobutton of Java game development

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.