Example analysis of swing menu usage in Java

  • 2020-04-01 04:07:45
  • OfStack

This article illustrates the use of swing radio boxes in Java. Share with you for your reference. The details are as follows:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test extends JApplet
implements ActionListener{
  JTextField jtf;
  public void init(){
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    JRadioButton b1 = new JRadioButton("A");
    b1.addActionListener(this);
    contentPane.add(b1);
    JRadioButton b2 = new JRadioButton("B");
    b2.addActionListener(this);
    contentPane.add(b2);
    JRadioButton b3 = new JRadioButton("C");
    b3.addActionListener(this);
    contentPane.add(b3);
    ButtonGroup bg = new ButtonGroup();
    bg.add(b1);
    bg.add(b2);
    bg.add(b3);
    jtf = new JTextField(15);
    contentPane.add(jtf);
  }
  public void actionPerformed(ActionEvent ae){
    jtf.setText(ae.getActionCommand());
  }
}

I hope this article has been helpful to your Java programming.


Related articles: