-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
140 lines (126 loc) · 4.96 KB
/
App.java
File metadata and controls
140 lines (126 loc) · 4.96 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
public class App {
public static void main(String[] args) {
JFrame window = new JFrame("App");
window.setSize(400, 400);
window.getContentPane().setBackground(Color.darkGray);
// panel
App app = new App();
MeasurementFrame measurement = app.new MeasurementFrame();
UnitFrame unitFrame = app.new UnitFrame(measurement);
ShowButton showButton = app.new ShowButton();
ShowResult showResult = app.new ShowResult();
window.add(measurement);
window.add(unitFrame);
window.add(showButton);
window.add(showResult);
// update
window.getContentPane().setLayout(new GridLayout(4, 2));
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
measurement.setUnitFrame(unitFrame);
}
// add elements
public class MeasurementFrame extends JPanel {
private JComboBox<String> measurement;
private UnitFrame unitFrame;
public MeasurementFrame() {
setBackground(Color.lightGray);
JLabel label = new JLabel("Measurement");
add(label);
String measurementItems[] = { "Area", "Data Transfer Rate", "Digital Storage", "Energy", "Frequency",
"Fuel Economy", "Length", "Mass", "Plane Angle", "Pressure", "Speed", "Temperature", "Time",
"Volume" };
measurement = new JComboBox<>(measurementItems);
measurement.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateUnitList((String) measurement.getSelectedItem());
}
});
add(measurement);
}
public void setUnitFrame(UnitFrame unitFrame) {
this.unitFrame = unitFrame;
}
private void updateUnitList(String selectedItem) {
String[] units;
switch (selectedItem) {
case "Area":
units = new String[] { "Square Kilometre", "Square Metre", "Square Mile", "Square Yard",
"Square Foot", "Square Inch", "Hectare", "Acre" };
break;
case "Data Transfer Rate":
units = new String[] { "Bit per second", "Kilobit per second", "Kilobyte per second",
"Kibibit per second", "Megabit per second", "Megabyte per second", "Mebibit per second",
"Gigabit per second", "Gigabyte per second", "Gibibit per second", "Terabit per second",
"Terabyte per second", "Tebibit per second" };
break;
// Add cases for other measurement types
default:
units = new String[] { "" };
break;
}
unitFrame.updateUnitList(units);
}
}
public class UnitFrame extends JPanel {
private JComboBox<String> unit;
private JTextField texbox;
public UnitFrame(MeasurementFrame measurementFrame) {
setBackground(Color.lightGray);
JLabel label = new JLabel("Enter value");
add(label);
texbox = new JTextField(10);
add(texbox);
unit = new JComboBox<>(new String[] {});
add(unit);
}
public void updateUnitList(String[] units) {
unit.removeAllItems();
for (String unitName : units) {
unit.addItem(unitName);
}
}
}
public class ShowButton extends JPanel {
public ShowButton() {
setBackground(Color.lightGray);
JButton Submit = new JButton("Submit");
add(Submit);
}
}
public class ShowResult extends JPanel {
public ShowResult() {
setBackground(Color.lightGray);
JLabel label = new JLabel("Comparing values");
add(label);
JLabel label_information = new JLabel("None");
add(label_information);
}
}
// show results
public class Results {
public String measurement;
public String unit;
public Double value;
private String compareUnit;
private Double compareValue;
public Results(String measurement, String unit, Double value) {
this.measurement = measurement;
this.unit = unit;
this.value = value;
}
public String get_compareUnit() {
if (measurement.equalsIgnoreCase("Area")) {
}
}
}
}