Commit 6d6ce9c0 by Patryk Czarnik

Kalkulator i przelicznik paliwa

parent e41b5e1c
package swing.kalkulator;
public class LogikaKalkulatora {
public static long oblicz(long arg1, long arg2, String operacja) {
switch(operacja) {
case "+": return arg1 + arg2;
case "-": return arg1 - arg2;
case "*": return arg1 * arg2;
case "/": return arg1 / arg2;
default : return 0;
}
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package swing.kalkulator;
import java.awt.Color;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
/**
*
* @author patryk
*/
public class OknoKalkulatora extends javax.swing.JFrame {
/**
* Creates new form OknoKalkulatora
*/
public OknoKalkulatora() {
initComponents();
mojeDodatki();
}
private void mojeDodatki() {
DocumentListener dl = new DocumentListener() {
public void insertUpdate(DocumentEvent evt) {
przelicz();
}
public void removeUpdate(DocumentEvent evt) {
przelicz();
}
public void changedUpdate(DocumentEvent evt) {
przelicz();
}
};
jTextField1.getDocument().addDocumentListener(dl);
jTextField2.getDocument().addDocumentListener(dl);
}
/**
* 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">//GEN-BEGIN:initComponents
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox<>();
jButton1 = new javax.swing.JButton();
jTextField3 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Kalkulator");
setResizable(false);
jTextField1.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jTextField2.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jTextField2.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jComboBox1.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "+", "-", "*", "/" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jButton1.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jButton1.setText("Oblicz");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField3.setEditable(false);
jTextField3.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jTextField3.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jTextField3.setText("0");
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.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(169, 169, 169)
.addComponent(jTextField3))
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextField1, jTextField2});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(20, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jComboBox1, jTextField1, jTextField2, jTextField3});
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
przelicz();
}//GEN-LAST:event_jButton1ActionPerformed
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
przelicz();
}//GEN-LAST:event_jComboBox1ActionPerformed
private void przelicz() {
if(jTextField1.getText().isEmpty() || jTextField2.getText().isEmpty()) {
jTextField3.setText("");
} else try {
long liczba1 = Long.parseLong(jTextField1.getText());
long liczba2 = Long.parseLong(jTextField2.getText());
// String operacja = jComboBox1.getItemAt(jComboBox1.getSelectedIndex());
String operacja = (String)jComboBox1.getSelectedItem();
long wynik = LogikaKalkulatora.oblicz(liczba1, liczba2, operacja);
jTextField3.setText(String.valueOf(wynik));
jTextField3.setForeground(Color.BLUE);
} catch(Exception e) {
jTextField3.setText("BŁĄD");
jTextField3.setForeground(Color.RED);
}
}
/**
* @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(OknoKalkulatora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(OknoKalkulatora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(OknoKalkulatora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(OknoKalkulatora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new OknoKalkulatora().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration//GEN-END:variables
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment