-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet6_GUIUnits.java
More file actions
69 lines (49 loc) · 2.04 KB
/
Copy pathSet6_GUIUnits.java
File metadata and controls
69 lines (49 loc) · 2.04 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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Set6_GUIUnits {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JLabel label_no_of_units = new JLabel("Number of Units:");
JTextField textfield_units = new JTextField();
JLabel label_billamount = new JLabel("Bill Amount");
JButton btn_calculate_bill = new JButton("Calculate Bill");
JPanel top_panel = new JPanel();
JPanel center_panel = new JPanel();
JPanel bottom_panel = new JPanel();
top_panel.setLayout(new GridLayout(2, 1));
center_panel.setLayout(new FlowLayout(0, 0, 200));
top_panel.add(label_no_of_units);
top_panel.add(textfield_units);
center_panel.add(label_billamount);
bottom_panel.add(btn_calculate_bill);
frame.add(top_panel, BorderLayout.NORTH);
frame.add(center_panel, BorderLayout.CENTER);
frame.add(bottom_panel, BorderLayout.SOUTH);
frame.setSize(500, 500);
frame.setTitle("Bill-Calculator!!");
frame.setVisible(true);
btn_calculate_bill.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String user_input = textfield_units.getText();
int bill = calculateBill(Integer.parseInt(user_input));
label_billamount.setText("Unit Consumption : " + user_input + " Bill : " + bill);
throw new UnsupportedOperationException("Unimplemented method 'actionPerformed'");
}
});
}
public static int calculateBill(int units) {
int charge = 1;
if (units <= 200) {
charge = 3;
} else if (units > 200 && units < 300) {
charge = 4;
} else {
charge = 5;
}
return units * charge;
}
}