-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61_RadioButton
More file actions
71 lines (58 loc) · 2.08 KB
/
61_RadioButton
File metadata and controls
71 lines (58 loc) · 2.08 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// JRadioButton = One or more buttons in a grouping in which only 1 may be selected
<Main.java>
public class Main{
public static void main(String[] args){
new myFrame();
}
}
<myFrame.java>
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class myFrame extends JFrame implements ActionListener {
JRadioButton pizzaButton;
JRadioButton hamburgerButton;
JRadioButton hotdogButton;
ImageIcon pizzaIcon;
ImageIcon hamburgerIcon;
ImageIcon hotdogIcon;
myFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
pizzaIcon = new ImageIcon("pizza.png");
hamburgerIcon = new ImageIcon("hamburger.png");
hotdogIcon = new ImageIcon("hotdog.jfif");
pizzaButton = new JRadioButton("pizza");
hamburgerButton = new JRadioButton("hamburger");
hotdogButton = new JRadioButton("hotdog");
//add icon to button
pizzaButton.setIcon(pizzaIcon);//or .setSelectedIcon
hamburgerButton.setIcon(hamburgerIcon);
hotdogButton.setIcon(hotdogIcon);
//put in same button group so can only select one
ButtonGroup group = new ButtonGroup();
group.add(pizzaButton);
group.add(hamburgerButton);
group.add(hotdogButton);
//addActionListener to function(print) at terminal
pizzaButton.addActionListener(this);
hamburgerButton.addActionListener(this);
hotdogButton.addActionListener(this);
this.add(pizzaButton);
this.add(hamburgerButton);
this.add(hotdogButton);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==pizzaButton){
System.out.println("You ordered pizza!");
}else if(e.getSource()==hamburgerButton){
System.out.println("You ordered a hamburger!");
}else if(e.getSource()==hotdogButton){
System.out.println("You ordered a hotdog!");
}
}
}