forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ2Lesson4.java
More file actions
107 lines (97 loc) · 3.23 KB
/
J2Lesson4.java
File metadata and controls
107 lines (97 loc) · 3.23 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Java. Level 2. Lesson 4. Swing
* Examples for lesson
*
* @author Sergey Iryupin
* @version dated Sep 08, 2017
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class J2Lesson4 extends JFrame {
public static void main(String[] args) {
new J2Lesson4();
}
J2Lesson4() {
setTitle("GUI Demo");
setBounds(500, 200, 800, 600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 2));
JPanel[] jp = new JPanel[4];
for (int i = 0; i < 4; i++) {
jp[i] = new JPanel();
add(jp[i]);
jp[i].setBackground(new Color(100 + i*40, 100 + i*40, 100 + i*40));
}
// left up panel
jp[0].setLayout(new BorderLayout());
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
jp[0].add(jsp);
// right up panel
jp[1].setLayout(new FlowLayout());
JRadioButton[] jrb = new JRadioButton[4];
ButtonGroup bgr = new ButtonGroup();
for (int i = 0; i < jrb.length; i++) {
jrb[i] = new JRadioButton("Option #" + i);
bgr.add(jrb[i]);
jp[1].add(jrb[i]);
}
JCheckBox[] jcb = new JCheckBox[4];
for (int i = 0; i < jcb.length; i++) {
jcb[i] = new JCheckBox("Check #" + i);
jp[1].add(jcb[i]);
}
// left down panel
jp[2].setLayout(new FlowLayout());
String[] comboStr = {"Item #1", "Item #2", "Item #3", "Item #4"};
JComboBox<String> jcombo1 = new JComboBox<String>(comboStr);
JComboBox<String> jcombo2 = new JComboBox<String>(comboStr);
jp[2].add(jcombo1);
jp[2].add(jcombo2);
jcombo1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(jcombo1.getSelectedItem().toString());
}
});
// right down panel
jp[3].setLayout(null);
JSlider js = new JSlider();
JLabel jlab = new JLabel("Value: 50");
js.setMaximum(100);
js.setMinimum(0);
js.setValue(50);
jp[3].add(jlab);
jp[3].add(js);
js.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
jlab.setText("Value: " + js.getValue());
}
});
jlab.setBounds(10,10,100,20);
js.setBounds(20, 40, 300, 100);
js.setBackground(new Color(160,255,160));
// drop-down menu
JMenuBar mainMenu = new JMenuBar();
JMenu mFile = new JMenu("File");
JMenu mEdit = new JMenu("Edit");
JMenuItem miFileNew = new JMenuItem("New");
JMenuItem miFileExit = new JMenuItem("Exit");
setJMenuBar(mainMenu);
mainMenu.add(mFile);
mainMenu.add(mEdit);
mFile.add(miFileNew);
mFile.addSeparator();
mFile.add(miFileExit);
miFileExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setVisible(true);
}
}