Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Kalkuator;
- public class Kalkulator extends javax.swing.JFrame {
- double cyfra, wynik;
- int obliczenia;
- public Kalkulator() {
- initComponents();
- }
- public void licz(){
- switch (obliczenia)
- {
- case 1:
- wynik = cyfra + Double.parseDouble(jTextField1.getText());
- jLabel1.setText(Double.toString(wynik));
- break;
- case 2:
- wynik = cyfra - Double.parseDouble(jTextField1.getText());
- jLabel1.setText(Double.toString(wynik));
- break;
- case 3:
- wynik = cyfra * Double.parseDouble(jTextField1.getText());
- jLabel1.setText(Double.toString(wynik));
- break;
- case 4:
- wynik = cyfra / Double.parseDouble(jTextField1.getText());
- jLabel1.setText(Double.toString(wynik));
- break;
- }
- }
- /**
- * 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() {
- jFrame1 = new javax.swing.JFrame();
- jTextField1 = new javax.swing.JTextField();
- jPanel2 = new javax.swing.JPanel();
- jButton3 = new javax.swing.JButton();
- jButton1 = new javax.swing.JButton();
- jButton2 = 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();
- jButton11 = new javax.swing.JButton();
- jButton12 = new javax.swing.JButton();
- jButton13 = new javax.swing.JButton();
- jButton14 = new javax.swing.JButton();
- jButton15 = new javax.swing.JButton();
- jButton16 = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
- jFrame1.getContentPane().setLayout(jFrame1Layout);
- jFrame1Layout.setHorizontalGroup(
- jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 400, Short.MAX_VALUE)
- );
- jFrame1Layout.setVerticalGroup(
- jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 300, Short.MAX_VALUE)
- );
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jTextField1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
- jTextField1.setText(" ");
- jTextField1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1ActionPerformed(evt);
- }
- });
- jPanel2.setLayout(new java.awt.GridLayout(4, 4, 1, 1));
- jButton3.setText("7");
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton3);
- jButton1.setText("8");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton1);
- jButton2.setText("9");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton2);
- jButton4.setText("\\");
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton4);
- jButton5.setText("4");
- jButton5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton5ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton5);
- jButton6.setText("5");
- jButton6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton6ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton6);
- jButton7.setText("6");
- jButton7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton7ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton7);
- jButton8.setText("*");
- jButton8.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton8ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton8);
- jButton9.setText("1");
- jButton9.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton9ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton9);
- jButton10.setText("2");
- jButton10.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton10ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton10);
- jButton11.setText("3");
- jButton11.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton11ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton11);
- jButton12.setText("-");
- jButton12.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton12ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton12);
- jButton13.setText("0");
- jButton13.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton13ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton13);
- jButton14.setText(".");
- jButton14.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton14ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton14);
- jButton15.setText("=");
- jButton15.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton15ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton15);
- jButton16.setText("+");
- jButton16.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton16ActionPerformed(evt);
- }
- });
- jPanel2.add(jButton16);
- jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
- jLabel1.setText(" ");
- 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()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
- .addComponent(jTextField1)))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("7"));
- }
- private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("4"));
- }
- private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("1"));
- }
- private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("0"));
- }
- private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("2"));
- }
- private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("*"));
- obliczenia=3;
- }
- private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("3"));
- }
- private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("5"));
- }
- private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("6"));
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("8"));
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("9"));
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("/"));
- obliczenia =4;
- }
- private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("-"));
- obliczenia = 2;
- }
- private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("+"));
- obliczenia = 1 ;
- }
- private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
- licz();
- }
- private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText()+("."));
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Kalkulator().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton10;
- private javax.swing.JButton jButton11;
- private javax.swing.JButton jButton12;
- private javax.swing.JButton jButton13;
- private javax.swing.JButton jButton14;
- private javax.swing.JButton jButton15;
- private javax.swing.JButton jButton16;
- 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.JFrame jFrame1;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JPanel jPanel2;
- private javax.swing.JTextField jTextField1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement