The radio button for Java

Source: Internet
Author: User

The concept of the

radio buttons in GUI programming comes from the mechanical buttons of old-fashioned tube car radios: When we press a button, the other buttons bounce. So it allows us to force a single choice from a multitude of choices.
AWT does not have a separate class that describes radio buttons; instead, the duplicate checkbox. However, place the check box in the radio buttons group (and modify its shape to make it look different from the General check box) We must use a special builder like an independent variable to function on the CheckboxGroup object. (We can also call the Setcheckboxgroup () method After creating the check box.)
A checkbox group does not have a builder's argument; the only reason it exists is by aggregating some check boxes into the radio button group. A check box object must set its state to true before we attempt to display the radio button group, otherwise we will get an exception at runtime. If we set more than one radio buttons to true, only the last one can be set to true.
Here's a simple example of using radio buttons. Note that we can capture radio buttons events like other components:
 

: Radiobutton1.java
//Using radio buttons
import java.awt.*;
Import java.applet.*;

public class RadioButton1 extends Applet {
  TextField t = 
    new TextField ("Radio button 2");
  CheckboxGroup g = new CheckboxGroup ();
  CheckBox 
    CB1 = new CheckBox ("One", G, false),
    CB2 = new CheckBox ("Two", G, true),
    cb3 = new CheckBox ("Three ", G, false);
  public void init () {
    t.seteditable (false);
    Add (t); 
    Add (CB1); Add (CB2); Add (CB3); 
  }
  Public boolean action (Event evt, Object Arg) {
    if (evt.target.equals (CB1))
      t.settext ("Radio button 1");
    else if (evt.target.equals (CB2))
      t.settext ("Radio button 2");
    else if (evt.target.equals (CB3))
      t.settext ("Radio button 3");
    else return 
      super.action (EVT, arg);
    return true;
  }
///:~

The state of the display is a text field in use. This field is set to not editable because it is used to display data instead of collection. This demonstrates a desirable way to use labels. Note that the text in the field is initialized by the first selected radio button "Radio button 2".
We can have quite a few check box groups in the form.

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.