Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Calculator extends javax.swing.JFrame {
- /** Creates new form Calculator */
- //zmienne
- double plusminus;
- double firstDouble;
- double secondDouble;
- double totalDouble;
- //sprawdzanie dla przycisnięcia danego buttonika
- int plusClick;
- int minusClick;
- int multiplyClick;
- int divideClick;
- int decimalClick;
- public Calculator() {
- 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() {
- jPanel1 = new javax.swing.JPanel();
- display = new javax.swing.JTextField();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- jButton3 = new javax.swing.JButton();
- jButton4 = new javax.swing.JButton();
- jButton5 = new javax.swing.JButton();
- jButton6 = new javax.swing.JButton();
- jButton7 = new javax.swing.JButton();
- jButton8 = new javax.swing.JButton();
- jButton9 = new javax.swing.JButton();
- jButton10 = new javax.swing.JButton();
- clear = new javax.swing.JButton();
- decimal = new javax.swing.JButton();
- plus = new javax.swing.JButton();
- minus = new javax.swing.JButton();
- multiplay = new javax.swing.JButton();
- divide = new javax.swing.JButton();
- posneg = new javax.swing.JButton();
- equals = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Kalkulator v1.0");
- jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
- display.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton1.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton1.setText("1");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton2.setText("2");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jButton3.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton3.setText("3");
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- jButton4.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton4.setText("4");
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jButton5.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton5.setText("5");
- jButton5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton5ActionPerformed(evt);
- }
- });
- jButton6.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton6.setText("6");
- jButton6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton6ActionPerformed(evt);
- }
- });
- jButton7.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton7.setText("7");
- jButton7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton7ActionPerformed(evt);
- }
- });
- jButton8.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton8.setText("8");
- jButton8.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton8ActionPerformed(evt);
- }
- });
- jButton9.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton9.setText("9");
- jButton9.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton9ActionPerformed(evt);
- }
- });
- jButton10.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jButton10.setText("0");
- jButton10.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton10ActionPerformed(evt);
- }
- });
- clear.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- clear.setText("C");
- clear.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- clearActionPerformed(evt);
- }
- });
- decimal.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- decimal.setText(".");
- decimal.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- decimalActionPerformed(evt);
- }
- });
- plus.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- plus.setText("+");
- plus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- plusActionPerformed(evt);
- }
- });
- minus.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- minus.setText("-");
- minus.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- minusActionPerformed(evt);
- }
- });
- multiplay.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- multiplay.setText("x");
- multiplay.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- multiplayActionPerformed(evt);
- }
- });
- divide.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- divide.setText("/");
- divide.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- divideActionPerformed(evt);
- }
- });
- posneg.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- posneg.setText("+/-");
- posneg.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- posnegActionPerformed(evt);
- }
- });
- equals.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- equals.setText("=");
- equals.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- equalsActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(22, 22, 22)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(multiplay, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(10, 10, 10)
- .addComponent(equals, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))))))
- .addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addComponent(posneg, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(20, 20, 20))
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(11, 11, 11)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(multiplay, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(10, 10, 10)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(posneg, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(equals, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(12, Short.MAX_VALUE))
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(25, 25, 25)
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(43, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void clearActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText("");
- decimalClick=0;
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- //display.getText(display.getText()+jButton1);
- display.setText(display.getText()+jButton1.getText());
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText(display.getText()+jButton2.getText());
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText(display.getText()+jButton3.getText());
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText(display.getText()+jButton4.getText());
- }
- private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText(display.getText()+jButton5.getText());
- }
- private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText(display.getText()+jButton6.getText());
- }
- private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText(display.getText()+jButton7.getText());
- }
- private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText(display.getText()+jButton8.getText());
- }
- private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText(display.getText()+jButton9.getText());
- }
- private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
- display.setText(display.getText()+jButton10.getText());
- }
- private void decimalActionPerformed(java.awt.event.ActionEvent evt) {
- if(decimalClick==0){
- display.setText(display.getText()+decimal.getText());
- decimalClick=1;
- }
- }
- private void posnegActionPerformed(java.awt.event.ActionEvent evt) {
- plusminus=(Double.parseDouble(String.valueOf(display.getText())));
- plusminus=plusminus*(-1);
- display.setText(String.valueOf(plusminus));
- }
- private void plusActionPerformed(java.awt.event.ActionEvent evt) {
- firstDouble=(Double.parseDouble(String.valueOf(display.getText())));
- display.setText("");
- plusClick=1;
- decimalClick=0;
- }
- private void minusActionPerformed(java.awt.event.ActionEvent evt) {
- firstDouble=(Double.parseDouble(String.valueOf(display.getText())));
- display.setText("");
- minusClick=1;
- decimalClick=0;
- }
- private void multiplayActionPerformed(java.awt.event.ActionEvent evt) {
- firstDouble=(Double.parseDouble(String.valueOf(display.getText())));
- display.setText("");
- multiplyClick=1;
- decimalClick=0;
- }
- private void divideActionPerformed(java.awt.event.ActionEvent evt) {
- firstDouble=(Double.parseDouble(String.valueOf(display.getText())));
- display.setText("");
- divideClick=1;
- decimalClick=0;
- }
- private void equalsActionPerformed(java.awt.event.ActionEvent evt) {
- secondDouble=(Double.parseDouble(String.valueOf(display.getText())));
- if(plusClick>0){
- totalDouble=firstDouble+secondDouble;
- display.setText((String.valueOf(totalDouble)));
- firstDouble=0;
- secondDouble=0;
- plusClick=0;
- }
- if(minusClick>0){
- totalDouble=firstDouble-secondDouble;
- display.setText((String.valueOf(totalDouble)));
- firstDouble=0;
- secondDouble=0;
- minusClick=0;
- }
- if(multiplyClick>0){
- totalDouble=firstDouble*secondDouble;
- display.setText((String.valueOf(totalDouble)));
- firstDouble=0;
- secondDouble=0;
- multiplyClick=0;
- }
- if(divideClick>0){
- totalDouble=firstDouble/secondDouble;
- display.setText((String.valueOf(totalDouble)));
- firstDouble=0;
- secondDouble=0;
- divideClick=0;
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Calculator().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton clear;
- private javax.swing.JButton decimal;
- private javax.swing.JTextField display;
- private javax.swing.JButton divide;
- private javax.swing.JButton equals;
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton10;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JButton jButton4;
- private javax.swing.JButton jButton5;
- private javax.swing.JButton jButton6;
- private javax.swing.JButton jButton7;
- private javax.swing.JButton jButton8;
- private javax.swing.JButton jButton9;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JButton minus;
- private javax.swing.JButton multiplay;
- private javax.swing.JButton plus;
- private javax.swing.JButton posneg;
- // End of variables declaration
- }
Add Comment
Please, Sign In to add comment