-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBodyProfileMenu.java
More file actions
121 lines (107 loc) · 3.18 KB
/
BodyProfileMenu.java
File metadata and controls
121 lines (107 loc) · 3.18 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**
* BodyProfileMenu is the main menu interface for
* the Body Profile App
* @author jvelez
*/
public class BodyProfileMenu extends JFrame {
private JPanel panel;
private JLabel messageLabel;
private JRadioButton viewProfile;
private JRadioButton editProfile;
private JRadioButton calcBMI;
private JRadioButton calcBodyComp;
private ButtonGroup bg;
private final int WINDOW_WIDTH = 250;
private final int WINDOW_HEIGHT = 325;
/**
* constructor
*/
public BodyProfileMenu() {
// set the title
setTitle("Body Profile App");
// set the size
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// set close operation
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// build the panel and add it to the window
buildPanel();
add(panel);
// make the window visible
setVisible(true);
}
/**
* buildPanel adds the buttons and labels to the panel;
* also groups the buttons and assigns them action listeners
*/
private void buildPanel() {
// instantiate the panel and set its layout
panel = new JPanel();
panel.setLayout(new GridLayout(5, 1));
// create the label and add it to the panel
messageLabel = new JLabel("Choose:");
panel.add(messageLabel);
// create the buttons and add them to the button group
viewProfile = new JRadioButton("View my Profile");
editProfile = new JRadioButton("Edit my Profile");
calcBMI = new JRadioButton("Calculate BMI");
calcBodyComp = new JRadioButton("Calculate Body Composition");
bg = new ButtonGroup();
bg.add(viewProfile);
bg.add(editProfile);
bg.add(calcBMI);
bg.add(calcBodyComp);
// give the buttons action listeners
viewProfile.addActionListener(new RadioButtonListener());
editProfile.addActionListener(new RadioButtonListener());
calcBMI.addActionListener(new RadioButtonListener());
calcBodyComp.addActionListener(new RadioButtonListener());
// add the buttons to the panel
panel.add(viewProfile);
panel.add(editProfile);
panel.add(calcBMI);
panel.add(calcBodyComp);
}
/**
* private action listener class to handle operations
* after a radio button is clicked
*/
private class RadioButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// determine which radio button was selected and
// open the corresponding window
if (e.getSource() == viewProfile) {
// will display profile
new ViewProfileWindow();
}
else if (e.getSource() == editProfile) {
// will display edit profile window
new EditProfileWindow();
}
else if (e.getSource() == calcBMI) {
// display a calculated bmi based on
// other profile info
try { // for whatever reason this won't work without IOException
// handling, despite IOExceptions being handled in the
// CalcBMIWindow class
new CalcBMIWindow();
}
catch (IOException ie) {
JOptionPane.showMessageDialog(null, "Error");
}
}
else if (e.getSource() == calcBodyComp) {
// display calculate body comp window
try {
new CalcBodyCompWindow();
}
catch (IOException ie) {
JOptionPane.showMessageDialog(null, "Error");
}
}
}
}
}