Constructors of the JRadioButton class:
-JRadioButton()
creates an empty radio button.
-JRadioButton(String text)
creates a radio button with specified description text.
-JRadioButton(String text, boolean selected)
creates a radio button with specified description text and selection state.
-JRadioButton(Icon icon)
creates a radio button with specified image icon and no text associated.
-JRadioButton(Icon icon, boolean selected)
creates a radio button with specified image icon and selection state.
-JRadioButton(String Text, Icon icon)
creates a radio button with specified image icon and text.
-JRadioButton(String Text, Icon icon, boolean selected)
creates a radio button with specified image icon, text, and selection.
Useful methods of the JRadioButton class:
-getText():String
reads the text of the radio button.
-isSelected(): boolean
determines whether or not the radio button is selected.
-setText(String text): void
sets the text of the radio button.
Example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
class JRadioButtonShow extends JFrame implements ItemListener{
JLabel lbl;
JRadioButtonShow (String title){
setTitle(title);
setSize(400,300);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setBackground(Color.CYAN);
String[] choices={"int x;","float x","integer x;","double x;"};
JPanel p=new JPanel();
p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
contpane.add(new JLabel("How to you declare an integer variable x?"), BorderLayout.NORTH);
for(String ch:choices){
p.add(new JRadioButton(ch));
}
ButtonGroup bg=new ButtonGroup();
for(int i=0;i<p.getComponentCount();i++)
{
JRadioButton rb=(JRadioButton)p.getComponent(i);
rb.addItemListener(this);
bg.add(rb);
}
contpane.add(p,BorderLayout.CENTER);
lbl=new JLabel();
contpane.add(lbl,BorderLayout.SOUTH);
setVisible(true);
}
public void itemStateChanged(ItemEvent e){
JRadioButton rb=(JRadioButton)e.getSource();
if(rb.isSelected())
lbl.setText(rb.getText());
else
lbl.setText(lbl.getText().replaceAll(rb.getText(),""));
}
}
public class JFrameSwing {
public static void main(String[] args){
new JRadioButtonShow ("JRadioButton");
}
}
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
class JRadioButtonShow extends JFrame implements ItemListener{
JLabel lbl;
JRadioButtonShow (String title){
setTitle(title);
setSize(400,300);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setBackground(Color.CYAN);
String[] choices={"int x;","float x","integer x;","double x;"};
JPanel p=new JPanel();
p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
contpane.add(new JLabel("How to you declare an integer variable x?"), BorderLayout.NORTH);
for(String ch:choices){
p.add(new JRadioButton(ch));
}
ButtonGroup bg=new ButtonGroup();
for(int i=0;i<p.getComponentCount();i++)
{
JRadioButton rb=(JRadioButton)p.getComponent(i);
rb.addItemListener(this);
bg.add(rb);
}
contpane.add(p,BorderLayout.CENTER);
lbl=new JLabel();
contpane.add(lbl,BorderLayout.SOUTH);
setVisible(true);
}
public void itemStateChanged(ItemEvent e){
JRadioButton rb=(JRadioButton)e.getSource();
if(rb.isSelected())
lbl.setText(rb.getText());
else
lbl.setText(lbl.getText().replaceAll(rb.getText(),""));
}
}
public class JFrameSwing {
public static void main(String[] args){
new JRadioButtonShow ("JRadioButton");
}
}
No comments:
Post a Comment