-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alternative_applet
62 lines (55 loc) · 1.69 KB
/
Alternative_applet
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MySwingApp extends JFrame implements ActionListener {
private JTextField n = new JTextField(10);
private ButtonGroup g = new ButtonGroup();
private JRadioButton m = new JRadioButton("Male", true);
private JRadioButton f = new JRadioButton("Female", false);
private JComboBox<String> c = new JComboBox<>();
private JLabel l1 = new JLabel("Enter Name: ");
private JLabel l2 = new JLabel("Select Gender: ");
private JLabel l3 = new JLabel("Age: ");
private JButton b1 = new JButton("Submit");
private String name = "", gender = "";
private int age;
public MySwingApp() {
super("My Swing App");
setLayout(new FlowLayout());
g.add(m);
g.add(f);
add(l1);
add(n);
add(l2);
add(m);
add(f);
add(l3);
c.addItem("18");
c.addItem("19");
c.addItem("20");
c.addItem("21");
add(c);
add(b1);
b1.addActionListener(this);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
name = n.getText();
gender = g.getSelection().getActionCommand();
age = Integer.parseInt((String) c.getSelectedItem());
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawString("Name: " + name, 20, 100);
g.drawString("Gender: " + gender, 20, 120);
g.drawString("Age: " + age, 20, 140);
}
public static void main(String[] args) {
new MySwingApp();
}
}