Skip to content

Instantly share code, notes, and snippets.

@desrtfx
Last active August 29, 2015 14:26
Show Gist options
  • Save desrtfx/3528d3b07f8791765276 to your computer and use it in GitHub Desktop.
Save desrtfx/3528d3b07f8791765276 to your computer and use it in GitHub Desktop.
package gui;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import controller.NumListener;
/**
* JPanel for the number buttons plus comma
*
*/
public class NumberPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 4992174666314388065L;
// array for the number buttons
private JButton[] numericButtons;
// The action listener common to all number buttons
private NumberHandler numberListener;
// Create the GridbagConstraints for the layout
private GridBagConstraints gc = new GridBagConstraints();
/**
* Constructor for the number panel
*
* @param controller
* reference to the main controller that is needed for the action
* listener.
*/
public NumberPanel(NumListener controller) {
numberListener = new NumberHandler(controller);
// Create the actual buttons
createNumericButtons();
// Add the Layout manager
GridBagLayout gBL = new GridBagLayout();
gBL.columnWidths = new int[] { 0 };
gBL.rowHeights = new int[] { 0 };
gBL.columnWeights = new double[] { Double.MIN_VALUE };
gBL.rowWeights = new double[] { Double.MIN_VALUE };
setLayout(gBL);
// Add the Buttons to the panel
setupButtons();
validate();
}
/**
* Adds the buttons to the panel.
*
* The panel uses a GridBagLayout to arrange the buttons in rows and columns
*/
private void setupButtons() {
// Common for all grid cells
gc.weightx = 1;
gc.weighty = 1;
gc.fill = GridBagConstraints.BOTH;
// Numeric buttons
// Individual properties per button
for (int row = 0; row <= 2; row++) {
for (int col = 0; col <= 2; col++) {
addButton(col, row, 1, 1, numericButtons[(7 - row * 3) + col]);
}
}
// Zero Button - spans 2 columns
addButton(0, 3, 2, 1, numericButtons[GuiConstants.NB_ZERO]);
// Decimal Button - spans 1 column
addButton(2, 3, 1, 1, numericButtons[GuiConstants.NB_PERIOD]);
}
/**
* Creates the array of JButton elements and assigns the action listener
*/
private void createNumericButtons() {
numericButtons = new JButton[11];
for (int i = 0; i <= 9; i++) {
numericButtons[i] = createButton(Integer.toString(i),
Integer.toString(i), GuiConstants.COL_NUMBER,
numberListener);
}
numericButtons[GuiConstants.NB_PERIOD] = createButton(".", ".",
GuiConstants.COL_NUMBER, numberListener);
}
/**
* Helper routine to add the buttons to the panel
*
* @param gridx
* the x-position (column) in the grid
* @param gridy
* the y-position (row) in the grid
* @param gridwidth
* the column span
* @param gridheight
* the row span
* @param button
* the actual button object to be added to the panel
*/
private void addButton(int gridx, int gridy, int gridwidth, int gridheight,
JButton button) {
gc.gridx = gridx;
gc.gridy = gridy;
gc.gridwidth = gridwidth;
gc.gridheight = gridheight;
add(button, gc);
}
/**
* Helper routine to create and pre-set a JButton
*
* @param label
* the label on the button
* @param cmd
* the ActionCommand of the button (a String that is evaluated by
* the action listener)
* @param color
* the button color
* @param actionListener
* the action listener
* @return a pre-configured JButton
*/
private JButton createButton(String label, String cmd, Color color,
ActionListener actionListener) {
JButton btn = new JButton(label);
btn.setActionCommand(cmd);
btn.setBackground(color);
if (actionListener != null) {
btn.addActionListener(actionListener);
}
return btn;
}
}
package gui;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import controller.CmdListener;
/**
* The JPanel for the operation buttons
*
*/
public class OperationPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = -1305333779778055422L;
// Create the GridbagConstraints for the layout
private GridBagConstraints gc = new GridBagConstraints();
// Array for the operation buttons
private JButton[] operationButtons;
// Array for the auxiliary buttons
private JButton[] auxButtons;
// The action listener common to all buttons
private CmdHandler cmdHandler;
/**
* Constructor for the Operation Panel
*
* @param controller
* reference to the main controller that is needed for the action
* listener.
*/
public OperationPanel(CmdListener controller) {
cmdHandler = new CmdHandler(controller);
createOperatorButtons();
createAuxButtons();
// Add the Layout manager
GridBagLayout gBL = new GridBagLayout();
gBL.columnWidths = new int[] { 0 };
gBL.rowHeights = new int[] { 0 };
gBL.columnWeights = new double[] { Double.MIN_VALUE };
gBL.rowWeights = new double[] { Double.MIN_VALUE };
setLayout(gBL);
// Add the Buttons to the panel
setupButtons();
}
/**
* Adds the pre-created buttons to the panel
*/
private void setupButtons() {
// Common for all grid cells
gc.weightx = 1;
gc.weighty = 1;
gc.fill = GridBagConstraints.BOTH;
// Auxiliary Buttons - C
addButton(0, 0, 1, 1, auxButtons[GuiConstants.AB_CE]);
// Auxiliary Buttons - CE
addButton(1, 0, 1, 1, auxButtons[GuiConstants.AB_C]);
// Auxiliary Buttons - Enter
addButton(0, 4, 2, 1, auxButtons[GuiConstants.AB_ENTER]);
// Operator Buttons
for (int row = 0; row <= 2; row++) {
for (int col = 0; col <= 1; col++) {
addButton(col, 1 + row, 1, 1, operationButtons[(2 - row) * 2
+ col]);
}
}
}
/**
* Creates the operation (+, -, *, /, +/-, 1/x) buttons
*/
private void createOperatorButtons() {
operationButtons = new JButton[6];
// "+" Button
operationButtons[GuiConstants.OP_ADD] = createButton("+", "+",
GuiConstants.COL_OPERATION, cmdHandler);
// "-" Button
operationButtons[GuiConstants.OP_SUB] = createButton("-", "-",
GuiConstants.COL_OPERATION, cmdHandler);
// "*" Button
operationButtons[GuiConstants.OP_MUL] = createButton("*", "*",
GuiConstants.COL_OPERATION, cmdHandler);
// "/" Button
operationButtons[GuiConstants.OP_DIV] = createButton("/", "/",
GuiConstants.COL_OPERATION, cmdHandler);
// "1/x" Button
operationButtons[GuiConstants.OP_INV] = createButton("1/x", "INV",
GuiConstants.COL_OPERATION, cmdHandler);
// "+/-" Button
operationButtons[GuiConstants.OP_SGN] = createButton("+/-", "SGN",
GuiConstants.COL_OPERATION, cmdHandler);
}
/**
* Creates the auxiliary buttons (Enter, Backspace, Clear)
*/
private void createAuxButtons() {
auxButtons = new JButton[3];
// "Enter" Button
auxButtons[GuiConstants.AB_ENTER] = createButton("Enter", "Enter",
GuiConstants.COL_ENTER, cmdHandler);
// "C" Button
auxButtons[GuiConstants.AB_C] = createButton("C", "C",
GuiConstants.COL_AUX, cmdHandler);
// "CE" Button
auxButtons[GuiConstants.AB_CE] = createButton("\u2190", "CE",
GuiConstants.COL_AUX, cmdHandler);
}
/**
* Helper routine to add the buttons to the panel
*
* @param gridx
* the x-position (column) in the grid
* @param gridy
* the y-position (row) in the grid
* @param gridwidth
* the column span
* @param gridheight
* the row span
* @param button
* the actual button object to be added to the panel
*/
private void addButton(int gridx, int gridy, int gridwidth, int gridheight,
JButton button) {
gc.gridx = gridx;
gc.gridy = gridy;
gc.gridwidth = gridwidth;
gc.gridheight = gridheight;
add(button, gc);
}
/**
* Helper routine to create and pre-set a JButton
*
* @param label
* the label on the button
* @param cmd
* the ActionCommand of the button (a String that is evaluated by
* the action listener)
* @param color
* the button color
* @param actionListener
* the action listener
* @return a pre-configured JButton
*/
private JButton createButton(String label, String cmd, Color color,
ActionListener actionListener) {
JButton btn = new JButton(label);
btn.setActionCommand(cmd);
btn.setBackground(color);
if (actionListener != null) {
btn.addActionListener(actionListener);
}
return btn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment