-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuPanel.java
45 lines (40 loc) · 1.38 KB
/
MenuPanel.java
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
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Alex Krantz
* Due date: May 28th
* Class description: Creates a simple GUI menu.
*/
public class MenuPanel extends Panel {
JButton[] buttons;
/**
* Creates a function menu JPanel
* @param buttonNames names of the buttons
* @param listeners actionlisteners to be added to buttons
*/
public MenuPanel(String[] buttonNames, ActionListener[] listeners) {
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); // Vertical BoxLayout (sub components will be vertically stacked)
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); // Border spacing
buttons = new JButton[buttonNames.length];
for (int i = 0; i < buttonNames.length; i++) {
// Instantiate buttons using buttonNames and listeners
JButton temp = new JButton(buttonNames[i]);
temp.addActionListener(listeners[i]);
buttons[i] = temp;
panel.add(Box.createRigidArea(new Dimension(0, 10))); // Vertical spacing between buttons
panel.add(temp); // Add button to panel
temp.setAlignmentX(Component.CENTER_ALIGNMENT); // Horizontally center each button
}
}
public JButton[] getButtons() {
return buttons;
}
}