Commit 49aff2e7 by Patryk Czarnik

Kalkulator Swing z zajęć

parent 97c526e0
package p30_swing.kalkulator_prosty;
/**
* @author patryk
* Wyklikane w NetBeans.
*/
public class OknoKalkulatora extends javax.swing.JFrame {
/**
* Creates new form OknoKalkulatora
*/
public OknoKalkulatora() {
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() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jTextField3 = new javax.swing.JTextField();
jButton5 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N
jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jTextField2.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N
jTextField2.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jButton1.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jButton1.setText("+");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jButton2.setText("-");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jButton3.setText("*");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jButton4.setText("/");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jTextField3.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N
jTextField3.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jTextField3.setText("0");
jButton5.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jButton5.setText("%");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
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(jTextField3)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(46, 46, 46)
.addComponent(jButton2)
.addGap(42, 42, 42)
.addComponent(jButton3)
.addGap(32, 32, 32)
.addComponent(jButton4)
.addGap(41, 41, 41)
.addComponent(jButton5))
.addComponent(jTextField1)
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE))
.addContainerGap(20, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3, jButton4, jButton5});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3)
.addComponent(jButton4)
.addComponent(jButton5))
.addGap(18, 18, 18)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2, jButton3, jButton4, jButton5});
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
var liczba1 = Long.parseLong(jTextField1.getText());
var liczba2 = Long.parseLong(jTextField2.getText());
var wynik = liczba1 + liczba2;
jTextField3.setText(String.valueOf(wynik));
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
var liczba1 = Long.parseLong(jTextField1.getText());
var liczba2 = Long.parseLong(jTextField2.getText());
var wynik = liczba1 - liczba2;
jTextField3.setText(String.valueOf(wynik));
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
var liczba1 = Long.parseLong(jTextField1.getText());
var liczba2 = Long.parseLong(jTextField2.getText());
var wynik = liczba1 * liczba2;
jTextField3.setText(String.valueOf(wynik));
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
var liczba1 = Long.parseLong(jTextField1.getText());
var liczba2 = Long.parseLong(jTextField2.getText());
var wynik = liczba1 / liczba2;
jTextField3.setText(String.valueOf(wynik));
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
var liczba1 = Long.parseLong(jTextField1.getText());
var liczba2 = Long.parseLong(jTextField2.getText());
var wynik = liczba1 % liczba2;
jTextField3.setText(String.valueOf(wynik));
}
/**
* @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>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new OknoKalkulatora().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
}
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