Stan Radner
DARKLY Regular
I'm having trouble with a program I am creating for my advanced java class. I want to call a value set as a double from two files and bring it to one file for a calculation.
I want to call the value from fNumber in NumberPanel1 and sNumber from NumberPanel2 to OperatorPanel and have them either added, subtracted, divided, etc based on what checkbox is selected.
Code:
Calculator2
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Final
*/
public class Calculator2
{
public static void main(String[] args)
{
new CalculatorGUI();
} // End of main
} // End of public class Calculator2.0
Title Panel
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Title Panel
*/
import javax.swing.*;
public class TitlePanel extends JPanel
{
private JLabel title; // To display the title
public TitlePanel()
{
title = new JLabel("Welcome to Calculator v2.0!"); // Create the title
add(title); // Add the label to this panel
} // End of TitlePanel
} // End of public class TitlePanel
CalculatorGUI (Not finished, Working on OperatorPanel)
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0
*/
import javax.swing.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class CalculatorGUI extends JFrame
{
private NumberPanel1 numberOne;
private OperatorPanel operator;
private NumberPanel2 numberTwo;
private TitlePanel title;
private JPanel buttonPanel;
private JPanel calcButton
private JPanel exitButton
public CalculatorCalcGUI()
{
setTitle("Calculator v2.0"); // Display a title
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Specify an action for the close button
setLayout(new BorderLayout()); // Create a BorderLayout manager
// Custom Panels
numberOne = new NumberPanel1(); // First number panel
operator = new OperatorPanel(); // Operator panel
numberTwo = new NumberPanel2(); // Second number panel
title = new TitlePanel(); // Title panel
buildButtonPanel(); // Create the button panel
// Add the components to the content page
add(title, BorderLayout.NORTH); // Title panel in the northern section
add(numberOne, BorderLayout.WEST); // First number panel in the western section
add(operator, BorderLayout.CENTER); // Operator panel in the center
add(numberTwo, BorderLayout.EAST); // Second number panel in the eastern section
add(buttonPanel, BorderLayout.SOUTH); // Button panel in the southern section
// Pack the contents of the window and display it
pack();
setVisible(true);
} // End of CalculatorGUI
private void buildButtonPanel() // The buildButtonPanel method builds the button panel
{
buttonPanel = new JPanel(); // Create a panel for the buttons
// Create the buttons
calcButton = new JButton("Calculate"); // Calculate button
exitButton = new JButton("Exit"); // Exit button
// Register the action listeners
calcButton.addActionListener(new CalcButtonListener());
exitButton.addActionListener(new ExitButtonListener());
// Add the buttons to the button panel
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
} // End of buildButtonPanel
private class CalcButtonListener implements ActionListener // Handles event when user clicks the calculate button
{
public void actionPerformed(ActionEvent e)
{
double finalAnswer
// Calculate the final answer
finalAnswer =
//} // End of buildButtonPanel
//} // End of CalculatorCalcGUI
} // End of public class CalculatorGUI
Number1Panel
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - First Number Panel
*/
import javax.swing.*;
import java.awt.*;
public class NumberPanel1 extends JPanel
{
// Set the values
public final double NUMBER_ZERO = 0;
public final double NUMBER_ONE = 1;
public final double NUMBER_TWO = 2;
public final double NUMBER_THREE = 3;
public final double NUMBER_FOUR = 4;
public final double NUMBER_FIVE = 5;
public final double NUMBER_SIX = 6;
public final double NUMBER_SEVEN = 7;
public final double NUMBER_EIGHT = 8;
public final double NUMBER_NINE = 9;
// Number radio buttons
private JRadioButton numberZero;
private JRadioButton numberOne;
private JRadioButton numberTwo;
private JRadioButton numberThree;
private JRadioButton numberFour;
private JRadioButton numberFive;
private JRadioButton numberSix;
private JRadioButton numberSeven;
private JRadioButton numberEight;
private JRadioButton numberNine;
// radio button group
private ButtonGroup bg;
public NumberPanel1()
{
setLayout(new GridLayout(10, 1)); // Create a GridLayout manager with 10 rows and one column
// Create the radio buttons
numberZero = new JRadioButton("0", true);
numberOne = new JRadioButton("1");
numberTwo = new JRadioButton("2");
numberThree = new JRadioButton("3");
numberFour = new JRadioButton("4");
numberFive = new JRadioButton("5");
numberSix = new JRadioButton("6");
numberSeven = new JRadioButton("7");
numberEight = new JRadioButton("8");
numberNine = new JRadioButton("9");
// Group the radio buttons
bg = new ButtonGroup();
bg.add(numberZero);
bg.add(numberOne);
bg.add(numberTwo);
bg.add(numberThree);
bg.add(numberFour);
bg.add(numberFive);
bg.add(numberSix);
bg.add(numberSeven);
bg.add(numberEight);
bg.add(numberNine);
// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("First Number"));
// Add the radio buttons to the panel
add(numberZero);
add(numberOne);
add(numberTwo);
add(numberThree);
add(numberFour);
add(numberFive);
add(numberSix);
add(numberSeven);
add(numberEight);
add(numberNine);
} // End of NuberPanel1
public double getFirstNumber()
{
double fNumber = 0.0; The first double value I want to call in OperationPanel
if (numberZero.isSelected())
fNumber = NUMBER_ZERO;
else if (numberOne.isSelected())
fNumber = NUMBER_ONE;
else if (numberTwo.isSelected())
fNumber = NUMBER_TWO;
else if (numberThree.isSelected())
fNumber = NUMBER_THREE;
else if (numberFour.isSelected())
fNumber = NUMBER_FOUR;
else if (numberFive.isSelected())
fNumber = NUMBER_FIVE;
else if (numberSix.isSelected())
fNumber = NUMBER_SIX;
else if (numberSeven.isSelected())
fNumber = NUMBER_SEVEN;
else if (numberEight.isSelected())
fNumber = NUMBER_EIGHT;
else if (numberNine.isSelected())
fNumber = NUMBER_NINE;
return fNumber;
} // End of getFirstNuber
} // End of public class NumberPanel1
Number2Panel
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Second Number Panel
*/
import javax.swing.*;
import java.awt.*;
public class NumberPanel2 extends JPanel
{
// Set the values
public final double NUMBER_ZERO = 0;
public final double NUMBER_ONE = 1;
public final double NUMBER_TWO = 2;
public final double NUMBER_THREE = 3;
public final double NUMBER_FOUR = 4;
public final double NUMBER_FIVE = 5;
public final double NUMBER_SIX = 6;
public final double NUMBER_SEVEN = 7;
public final double NUMBER_EIGHT = 8;
public final double NUMBER_NINE = 9;
// Number radio buttons
private JRadioButton numberZero;
private JRadioButton numberOne;
private JRadioButton numberTwo;
private JRadioButton numberThree;
private JRadioButton numberFour;
private JRadioButton numberFive;
private JRadioButton numberSix;
private JRadioButton numberSeven;
private JRadioButton numberEight;
private JRadioButton numberNine;
// radio button group
private ButtonGroup bg;
public NumberPanel2()
{
setLayout(new GridLayout(10, 1)); // Create a GridLayout manager with 10 rows and one column
// Create the radio buttons
numberZero = new JRadioButton("0", true);
numberOne = new JRadioButton("1");
numberTwo = new JRadioButton("2");
numberThree = new JRadioButton("3");
numberFour = new JRadioButton("4");
numberFive = new JRadioButton("5");
numberSix = new JRadioButton("6");
numberSeven = new JRadioButton("7");
numberEight = new JRadioButton("8");
numberNine = new JRadioButton("9");
// Group the radio buttons
bg = new ButtonGroup();
bg.add(numberZero);
bg.add(numberOne);
bg.add(numberTwo);
bg.add(numberThree);
bg.add(numberFour);
bg.add(numberFive);
bg.add(numberSix);
bg.add(numberSeven);
bg.add(numberEight);
bg.add(numberNine);
// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("Second Number"));
// Add the radio buttons to the panel
add(numberZero);
add(numberOne);
add(numberTwo);
add(numberThree);
add(numberFour);
add(numberFive);
add(numberSix);
add(numberSeven);
add(numberEight);
add(numberNine);
} // End of NuberPanel2
public double getSeondNumber()
{
double sNumber = 0.0; The second double value that I want to call in OperatorPanel
if (numberZero.isSelected())
sNumber = NUMBER_ZERO;
else if (numberOne.isSelected())
sNumber = NUMBER_ONE;
else if (numberTwo.isSelected())
sNumber = NUMBER_TWO;
else if (numberThree.isSelected())
sNumber = NUMBER_THREE;
else if (numberFour.isSelected())
sNumber = NUMBER_FOUR;
else if (numberFive.isSelected())
sNumber = NUMBER_FIVE;
else if (numberSix.isSelected())
sNumber = NUMBER_SIX;
else if (numberSeven.isSelected())
sNumber = NUMBER_SEVEN;
else if (numberEight.isSelected())
sNumber = NUMBER_EIGHT;
else if (numberNine.isSelected())
sNumber = NUMBER_NINE;
return sNumber;
} // End of getSecondNuber
} // End of public class NumberPanel2
OperatorPanel
/*
Programmer: Yo Momma
Date: Monday, April 28, 2014
Description: Calculator v2.0 - Operator Panel
*/
import javax.swing.*;
import java.awt.*;
public class OperatorPanel extends JPanel
{
public final char OPERATOR_ADD;
public final char OPERATOR_MINUS;
public final char OPERATOR_MULTIPLY;
public final char OPERATOR_DIVIDE;
public final char OPERATOR_POWER;
private JCheckBox add; // To select addition
private JCheckBox minus; // To select subraction
private JCheckBox multiply; // To select multiplication
private JCheckBox divide; // To select division
private JCheckBox power; // To select to the power of
public OperatorPanel()
{
setLayout(new GridLayout(5, 1)); // Create a GridLayout manager with 5 rows and one column
// Create the check boxes
add = new JCheckBox("Addition (+)");
minus = new JCheckBox("Subtraction (-)");
multiply = new JCheckBox("Multiplication (*)");
divide = new JCheckBox("Division (/)");
power = new JCheckBox("To the power of (^)");
// Add a border around the panel
setBorder(BorderFactory.createTitledBorder("Operator"));
// Add the check boxes to the panel
add(add);
add(minus);
add(multiply);
add(divide);
add(power);
} // End of OperatorPanel
public double getAnswer()
{
double output;
if (add.isSelected());
output = getFirstNumber.fNumber + getSecondNumber.sNumber; THIS IS WHERE I AM HAVING TROUBLE
} // End of getAnswer
} // End of public class Operator panel
I want to call the value from fNumber in NumberPanel1 and sNumber from NumberPanel2 to OperatorPanel and have them either added, subtracted, divided, etc based on what checkbox is selected.