Skip to content

Instantly share code, notes, and snippets.

@Hootis
Created October 3, 2011 04:00
Show Gist options
  • Save Hootis/1258410 to your computer and use it in GitHub Desktop.
Save Hootis/1258410 to your computer and use it in GitHub Desktop.
/* Nate Boyd
* Client-Server
* Professor Christian Roberson
* Lab 3 - Simple Calculator
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class SimpleCalculator extends JFrame {
// Create text fields for First and Second Integer and
// and label to hold calculations
private JTextField jtfFirstInteger = new JTextField();
private JTextField jtfSecondInteger = new JTextField();
private JLabel jlblCalculation = new JLabel();
// Create calculator buttons
private JButton jbtAdd = new JButton("+");
private JButton jbtMultiply = new JButton("x");
private JButton jbtSubtract = new JButton("-");
private JButton jbtDivide = new JButton("/");
public SimpleCalculator() {
// Panel p1 to hold labels and text fields
JPanel p1 = new JPanel(new GridLayout(2, 1));
p1.add(new JLabel("First Integer"));
p1.add(jtfFirstInteger);
p1.add(new JLabel("Second Integer"));
p1.add(jtfSecondInteger);
p1.add(new JLabel("Calculation:"));
p1.add(jlblCalculation);
p1.setBorder(new
TitledBorder("Enter two numbers"));
// Panel p2 to hold the button
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtAdd);
p2.add(jbtMultiply);
p2.add(jbtSubtract);
p2.add(jbtDivide);
// Add the panels to the frame
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
// Register listener
jbtAdd.addActionListener(new ButtonListener());
jbtMultiply.addActionListener(new ButtonListener());
jbtSubtract.addActionListener(new ButtonListener());
jbtDivide.addActionListener(new ButtonListener());
}
/** Handle the Compute Payment button */
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get values from text fields
int firstEntry =
Integer.parseInt(jtfFirstInteger.getText());
int secondEntry =
Integer.parseInt(jtfSecondInteger.getText());
// Perform each action, depending on button pressed
// Display monthly payment and total payment
jlblCalculation.setText("Test");
}
}
public static void main(String[] args) {
SimpleCalculator frame = new SimpleCalculator();
frame.pack();
frame.setTitle("LoanCalculator");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment