Membuat kalkulator sederhana dengan memanfaatkan fasilitas GUI pada JAVA...
Berikut adalah scrinshotnya.....
Berikut adalah scrinshotnya.....
Langsung saja...
Untuk membuat program seperti diatas silahkan anda buat desain seperti dibawah ini:Kemudian berikut source codenya:
package MyKalkulator;
public class fhitung extends javax.swing.JFrame {
/** Creates new form fhitung */
public fhitung() {
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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextNil1 = new javax.swing.JTextField();
jTextNil2 = new javax.swing.JTextField();
jTextHasil = new javax.swing.JTextField();
tambah = new javax.swing.JButton();
kali = new javax.swing.JButton();
bagi = new javax.swing.JButton();
kurang = new javax.swing.JButton();
bersih = new javax.swing.JButton();
keluar = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Form Hitung");
setBackground(new java.awt.Color(255, 255, 255));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
getContentPane().setLayout(null);
jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel1.setForeground(new java.awt.Color(51, 51, 51));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Kalkulator By Indra Al Sasak");
getContentPane().add(jLabel1);
jLabel1.setBounds(40, 10, 290, 17);
jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
jLabel2.setText("Nilai 2");
getContentPane().add(jLabel2);
jLabel2.setBounds(10, 100, 50, 30);
jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
jLabel3.setText("Hasilnya");
getContentPane().add(jLabel3);
jLabel3.setBounds(10, 130, 70, 30);
jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
jLabel4.setText("Nilai 1");
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 70, 50, 30);
jTextNil1.setText("jTextField1");
getContentPane().add(jTextNil1);
jTextNil1.setBounds(90, 70, 80, 20);
jTextNil2.setText("jTextField2");
getContentPane().add(jTextNil2);
jTextNil2.setBounds(90, 100, 80, 20);
jTextHasil.setText("jTextField3");
getContentPane().add(jTextHasil);
jTextHasil.setBounds(90, 130, 80, 20);
tambah.setText("Tambah");
tambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tambahActionPerformed(evt);
}
});
getContentPane().add(tambah);
tambah.setBounds(190, 50, 80, 40);
kali.setText("Kali");
kali.setMaximumSize(new java.awt.Dimension(71, 23));
kali.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kaliActionPerformed(evt);
}
});
getContentPane().add(kali);
kali.setBounds(270, 50, 80, 40);
bagi.setText("Bagi");
bagi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bagiActionPerformed(evt);
}
});
getContentPane().add(bagi);
bagi.setBounds(270, 90, 80, 40);
kurang.setText("Kurang");
kurang.setMaximumSize(new java.awt.Dimension(71, 23));
kurang.setPreferredSize(new java.awt.Dimension(71, 23));
kurang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kurangActionPerformed(evt);
}
});
getContentPane().add(kurang);
kurang.setBounds(190, 90, 80, 40);
bersih.setText("Bersih");
bersih.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bersihActionPerformed(evt);
}
});
getContentPane().add(bersih);
bersih.setBounds(190, 130, 80, 40);
keluar.setText("Keluar");
keluar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
keluarActionPerformed(evt);
}
});
getContentPane().add(keluar);
keluar.setBounds(270, 130, 80, 40);
jLabel5.setFont(new java.awt.Font("Times New Roman", 3, 11)); // NOI18N
jLabel5.setText("Created by Indra Al Sasak");
getContentPane().add(jLabel5);
jLabel5.setBounds(240, 190, 130, 13);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-382)/2, (screenSize.height-246)/2, 382, 246);
}// </editor-fold>
private void tambahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int a = Integer.parseInt(jTextNil1.getText());
int b = Integer.parseInt(jTextNil2.getText());
int c = a+b;
jTextHasil.setText(Integer.toString(c));
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
jTextNil1.setText("");
jTextNil2.setText("");
jTextHasil.setText("");
}
private void kaliActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int a = Integer.parseInt(jTextNil1.getText());
int b = Integer.parseInt(jTextNil2.getText());
int c = a*b;
jTextHasil.setText(Integer.toString(c));
}
private void kurangActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int a = Integer.parseInt(jTextNil1.getText());
int b = Integer.parseInt(jTextNil2.getText());
int c = a-b;
jTextHasil.setText(Integer.toString(c));
}
private void bagiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int a = Integer.parseInt(jTextNil1.getText());
int b = Integer.parseInt(jTextNil2.getText());
int c = a/b;
jTextHasil.setText(Integer.toString(c));
}
private void bersihActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextNil1.setText("");
jTextNil2.setText("");
jTextHasil.setText("");
}
private void keluarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new fhitung().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bagi;
private javax.swing.JButton bersih;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField jTextHasil;
private javax.swing.JTextField jTextNil1;
private javax.swing.JTextField jTextNil2;
private javax.swing.JButton kali;
private javax.swing.JButton keluar;
private javax.swing.JButton kurang;
private javax.swing.JButton tambah;
// End of variables declaration
}
Semoga bermanfaat.....
EmoticonEmoticon