Munkin kali ini sobat ingin membuat
calculator dengan mengunakan program java
mungkin ini merupakn salah satu hasil dari program java
pos:Muhammad Tri
Membuat Kalkulator sederhana :
Berikut source codenya>>
// untuk class ke satu nya
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* CalculatorForm.java
*/
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">//GEN-BEGIN:initComponents
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();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstan ts.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("Monotype Corsiva", 1, 24)); // NOI18N
answerLabel.setText("Jawabannya adalah ?");
jLabel3.setText("By : Teten Nugraha Ph.D.");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(107, 107, 107)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING, false)
.addComponent(secondNumberText, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(firstNumberText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(98, 98, 98)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addGroup(layout.createSequentialGroup()
.addComponent(plusButton)
.addGap(32, 32, 32)
.addComponent(minusButton))))
.addGroup(layout.createSequentialGroup()
.addGap(98, 98, 98)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(answerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(firstNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(22, 22, 22)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(secondNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(plusButton)
.addComponent(minusButton))
.addGap(18, 18, 18)
.addComponent(answerLabel)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 45, Short.MAX_VALUE)
.addComponent(jLabel3)
.addGap(44, 44, 44))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void plusButtonActionPerformed(java.awt.event.ActionEve nt evt) {//GEN-FIRST:event_plusButtonActionPerformed
int number1,number2;
try{
number1=Integer.parseInt(
this.firstNumberText.getText());
}catch(Exception e) {
JOptionPane.showMessageDialog(this,"Asupkeuna angka , lain huruf !",
"Error",JOptionPane.ERROR_MESSAGE);
return;
}
try{
number2=Integer.parseInt(
this.secondNumberText.getText());
}catch(Exception e) {
JOptionPane.showMessageDialog(this,"Bad first number",
"Error",JOptionPane.ERROR_MESSAGE);
return;
}
int answer=number1+number2;
this.answerLabel.setText(
"Jawabannya adalah ?"+answer);
}//GEN-LAST:event_plusButtonActionPerformed
private void minusButtonActionPerformed(java.awt.event.ActionEv ent evt) {//GEN-FIRST:event_minusButtonActionPerformed
int number1, number2;
try {
number1 = Integer.parseInt(
this.firstNumberText.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad first number",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
number2 = Integer.parseInt(
this.secondNumberText.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad first number",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
int answer = number1 - number2;
this.answerLabel.setText(
"Jawabannya adalah ?" + answer);
}//GEN-LAST:event_minusButtonActionPerformed
/**
* @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//GEN-BEGIN:variables
private javax.swing.JLabel answerLabel;
private javax.swing.JTextField firstNumberText;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JButton minusButton;
private javax.swing.JButton plusButton;
private javax.swing.JTextField secondNumberText;
// End of variables declaration//GEN-END:variables
}
// untuk class keduanya
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CalculatorForm form=new CalculatorForm();
form.setVisible(true);
}
}
Membuat Kalkulator sederhana :
Berikut source codenya>>
// untuk class ke satu nya
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* CalculatorForm.java
*/
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">//GEN-BEGIN:initComponents
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();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstan ts.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("Monotype Corsiva", 1, 24)); // NOI18N
answerLabel.setText("Jawabannya adalah ?");
jLabel3.setText("By : Teten Nugraha Ph.D.");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(107, 107, 107)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING, false)
.addComponent(secondNumberText, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(firstNumberText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(98, 98, 98)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addGroup(layout.createSequentialGroup()
.addComponent(plusButton)
.addGap(32, 32, 32)
.addComponent(minusButton))))
.addGroup(layout.createSequentialGroup()
.addGap(98, 98, 98)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(answerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(firstNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(22, 22, 22)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(secondNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(plusButton)
.addComponent(minusButton))
.addGap(18, 18, 18)
.addComponent(answerLabel)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 45, Short.MAX_VALUE)
.addComponent(jLabel3)
.addGap(44, 44, 44))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void plusButtonActionPerformed(java.awt.event.ActionEve nt evt) {//GEN-FIRST:event_plusButtonActionPerformed
int number1,number2;
try{
number1=Integer.parseInt(
this.firstNumberText.getText());
}catch(Exception e) {
JOptionPane.showMessageDialog(this,"Asupkeuna angka , lain huruf !",
"Error",JOptionPane.ERROR_MESSAGE);
return;
}
try{
number2=Integer.parseInt(
this.secondNumberText.getText());
}catch(Exception e) {
JOptionPane.showMessageDialog(this,"Bad first number",
"Error",JOptionPane.ERROR_MESSAGE);
return;
}
int answer=number1+number2;
this.answerLabel.setText(
"Jawabannya adalah ?"+answer);
}//GEN-LAST:event_plusButtonActionPerformed
private void minusButtonActionPerformed(java.awt.event.ActionEv ent evt) {//GEN-FIRST:event_minusButtonActionPerformed
int number1, number2;
try {
number1 = Integer.parseInt(
this.firstNumberText.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad first number",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
number2 = Integer.parseInt(
this.secondNumberText.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad first number",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
int answer = number1 - number2;
this.answerLabel.setText(
"Jawabannya adalah ?" + answer);
}//GEN-LAST:event_minusButtonActionPerformed
/**
* @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//GEN-BEGIN:variables
private javax.swing.JLabel answerLabel;
private javax.swing.JTextField firstNumberText;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JButton minusButton;
private javax.swing.JButton plusButton;
private javax.swing.JTextField secondNumberText;
// End of variables declaration//GEN-END:variables
}
// untuk class keduanya
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CalculatorForm form=new CalculatorForm();
form.setVisible(true);
}
}
selamat mencoba
Tags
totarial tkj