-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrd.java
More file actions
45 lines (44 loc) · 1.46 KB
/
rd.java
File metadata and controls
45 lines (44 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class RadioExample implements ItemListener {
JFrame f; JLabel label;JRadioButton r1,r2,r3;
RadioExample(){
f=new JFrame("Fruits");
label= new JLabel();
label.setSize(400,100);
label.setBounds(75,200,300,90);
r1=new JRadioButton("Apple");
r2=new JRadioButton("Mango");
r3=new JRadioButton("Grapes");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
r3.setBounds(75,150,100,30);
r1.addItemListener(this);
r2.addItemListener(this);
r3.addItemListener(this);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2); bg.add(r3);
f.add(r1);f.add(r2); f.add(r3); f.add(label);
f.setSize(600,600);
f.setLayout(null);
f.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource() ==r1)
{ label.setForeground(Color.MAGENTA);
label.setText("Apple: "
+ (e.getStateChange()==1?"selected":"unselected")); }
if(e.getSource() ==r2)
{label.setForeground(Color.GREEN);
label.setText("Mango: "
+ (e.getStateChange() ==1?"selected":"unselected"));}
if(e.getSource() ==r3)
{label.setForeground(Color.RED);
label.setText("Grapes: "
+ (e.getStateChange()==1?"selected":"unselected")); }
}
public static void main(String[] args) {
new RadioExample();
}
}