Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * CalculatorForm.java
- *
- * Created on Jun 30, 2011, 3:21:18 PM
- */
- /**
- *
- * @author aaronmauer
- */
- public class CalculatorForm extends javax.swing.JFrame {
- /** Creates new form CalculatorForm */
- public CalculatorForm() {
- initComponents();
- }
- /** This method is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- firstNumberText = new javax.swing.JTextField();
- secondNumberText = new javax.swing.JTextField();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- plusbutton = new javax.swing.JButton();
- minusbutton = new javax.swing.JButton();
- answerLabel = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Simple Calculator");
- jLabel1.setText("First Number");
- jLabel2.setText("Second Number");
- plusbutton.setText("+");
- plusbutton.setToolTipText("Add");
- plusbutton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- plusbuttonActionPerformed(evt);
- }
- });
- minusbutton.setText("-");
- minusbutton.setToolTipText("Subtract");
- minusbutton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- minusbuttonActionPerformed(evt);
- }
- });
- answerLabel.setFont(new java.awt.Font("Stone Sans Sem ITC TT", 1, 24)); // NOI18N
- answerLabel.setText("The Answer is:");
- org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
- .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
- .add(layout.createSequentialGroup()
- .addContainerGap(76, Short.MAX_VALUE)
- .add(firstNumberText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 392, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
- .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
- .add(76, 76, 76)
- .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(secondNumberText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
- .add(layout.createSequentialGroup()
- .add(plusbutton)
- .add(28, 28, 28)
- .add(minusbutton))
- .add(answerLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE))))
- .add(59, 59, 59))
- .add(layout.createSequentialGroup()
- .add(49, 49, 49)
- .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jLabel2)
- .add(jLabel1))
- .addContainerGap(379, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(layout.createSequentialGroup()
- .add(42, 42, 42)
- .add(jLabel1)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(firstNumberText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(11, 11, 11)
- .add(jLabel2)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(secondNumberText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(18, 18, 18)
- .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(minusbutton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(plusbutton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
- .add(18, 18, 18)
- .add(answerLabel)
- .addContainerGap(83, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void plusbuttonActionPerformed(java.awt.event.ActionEvent evt) {
- int number1,;
- try (
- number1 = Integer.parseInt(
- this.firstNumberText.getText());
- )
- catch (Exception e) (
- JOptionPane.showMessageDialog(this, "Bad First Number", "Error", JOptionPane.ERROR_MESSAGE,) ;
- return;
- int number2;
- try (
- number2 = Integer.parseInt(
- this.secondNumberText.getText());
- )
- catch (Exception e) (
- JOptionPane.showMessageDialog(this, "Bad Second Number", "Error", JOptionPane.ERROR_MESSAGE,) ;
- return;
- }
- private void minusbuttonActionPerformed(java.awt.event.ActionEvent evt) {
- int number1, ;
- try
- (
- number1 = Integer.parseInt(
- this.firstNumberText.getText());
- )
- catch (Exception e)
- (
- JOptionPane.showMessageDialog(this, "Bad First Number", "Error", JOptionPane.ERROR_MESSAGE,) ;
- return;
- int number2;
- try
- (
- number2 = Integer.parseInt(
- this.secondNumberText.getText());
- )
- catch (Exception e)
- (
- JOptionPane.showMessageDialog(this, "Bad Second Number", "Error", JOptionPane.ERROR_MESSAGE,) ;
- return;
- }
- int answer = number1 + number2;
- this.answerLabel.setText(
- "The Answer is: " - answer);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new CalculatorForm().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JLabel answerLabel;
- private javax.swing.JTextField firstNumberText;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JButton minusbutton;
- private javax.swing.JButton plusbutton;
- private javax.swing.JTextField secondNumberText;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement