NetBeans Ortamında Görsel Olarak Hazırladığım Basit Bir Java Programı
Bu program sadece bir sayının başka bir sayı ile bölümünden kalanı buluyor. Yani o sayının modunu alıyor. Biliyorum program çok basit ama burda önemli olan görsel öğelerin tasarlanması ve uygulaması. Normalde Java dilinde kalan hesaplamak için % operatörü(Mod operatörü) kullanılır. Mesela 24'ün mod 5'e göre değeri yani 24'ün 5'e bölümünden kalan 4'tür. Bunun Java dilindeki ifadesi 24%5 şeklindedir. Ancak ben bu programda başka bir yol kullandım. Kalanı hesaplayan algoritmanın denklemi şu şekildedir.
Kalan = Bölünen -(Bölünen/Bölen)*Bölen
Burada Bölünen ve Bölen Integer tipinde olduğundan bilgisayar hesaplamayı yaparken Bölünen/Bölen işlemi sonucunda bulunan ondalıklı sayının sadece tam kısmını alacak, ondalıklı kısmını atacaktır. Sonuç olarak denklem kalanı ya da diğer adıyla o sayının modunu vercektir.
NetBeans görsel tasarım ortamı aşağıdaki gibidir.
Program işe şu şekildedir
Programda iki ayrı Java Class'ı var ilki KalanHesaplama.java;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kalanhesapla;
/**
*
* @author turgut
*/
public class KalanHesaplama extends javax.swing.JFrame {
/**
* Creates new form KalanHesaplama
*/
public KalanHesaplama() {
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")
//
private void initComponents() {
jDesktopPane1 = new javax.swing.JDesktopPane();
hakkinda = new javax.swing.JInternalFrame();
jLabel1 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
kalanhesapla = new javax.swing.JInternalFrame();
bolunenyaz = new javax.swing.JTextField();
bolenyaz = new javax.swing.JTextField();
bolumyaz = new javax.swing.JTextField();
kalanyaz = new javax.swing.JTextField();
hesaplat = new javax.swing.JButton();
sil = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
Baslat = new javax.swing.JMenuItem();
cikis = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
hakkindamenu = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Kalan Heaplayıcı");
hakkinda.setClosable(true);
hakkinda.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
hakkinda.setTitle("Hakkında");
hakkinda.setVisible(false);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel1.setText("Kalan Hesapla");
jLabel4.setText("Kişisel Java tabanlıeEğitim yazılımları çalışmalarım");
jTextArea1.setBackground(new java.awt.Color(227, 235, 247));
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
jTextArea1.setForeground(new java.awt.Color(102, 102, 102));
jTextArea1.setRows(5);
jTextArea1.setTabSize(6);
jTextArea1.setText("Java eğitim yazılımı çalışmaları\n\nİletişim :\naarslan_tturgut@hotmail.com");
jTextArea1.setAutoscrolls(false);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout hakkindaLayout = new javax.swing.GroupLayout(hakkinda.getContentPane());
hakkinda.getContentPane().setLayout(hakkindaLayout);
hakkindaLayout.setHorizontalGroup(
hakkindaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(hakkindaLayout.createSequentialGroup()
.addContainerGap()
.addGroup(hakkindaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(hakkindaLayout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(100, 100, 100)
.addComponent(jLabel4))
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
hakkindaLayout.setVerticalGroup(
hakkindaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(hakkindaLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(hakkindaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 14, Short.MAX_VALUE))
);
hakkinda.setBounds(10, 20, 300, 150);
jDesktopPane1.add(hakkinda, javax.swing.JLayeredPane.DEFAULT_LAYER);
kalanhesapla.setClosable(true);
kalanhesapla.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
kalanhesapla.setTitle("Kalan Hesapla");
kalanhesapla.setVisible(false);
bolunenyaz.setText("Bölünen");
bolunenyaz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bolunenyazActionPerformed(evt);
}
});
bolenyaz.setText("Bölen");
bolenyaz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bolenyazActionPerformed(evt);
}
});
bolumyaz.setEditable(false);
bolumyaz.setText("Bölüm");
kalanyaz.setEditable(false);
kalanyaz.setText("Kalan");
kalanyaz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kalanyazActionPerformed(evt);
}
});
hesaplat.setText("Hesapla");
hesaplat.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
hesaplatActionPerformed(evt);
}
});
sil.setText("Sil");
sil.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
silActionPerformed(evt);
}
});
javax.swing.GroupLayout kalanhesaplaLayout = new javax.swing.GroupLayout(kalanhesapla.getContentPane());
kalanhesapla.getContentPane().setLayout(kalanhesaplaLayout);
kalanhesaplaLayout.setHorizontalGroup(
kalanhesaplaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(kalanhesaplaLayout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(kalanhesaplaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bolunenyaz, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bolenyaz, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bolumyaz, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(kalanyaz, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(kalanhesaplaLayout.createSequentialGroup()
.addComponent(hesaplat)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sil)))
.addContainerGap(32, Short.MAX_VALUE))
);
kalanhesaplaLayout.setVerticalGroup(
kalanhesaplaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(kalanhesaplaLayout.createSequentialGroup()
.addContainerGap()
.addComponent(bolunenyaz, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bolenyaz, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bolumyaz, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(kalanyaz, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(kalanhesaplaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(hesaplat)
.addComponent(sil))
.addContainerGap())
);
kalanhesapla.setBounds(70, 20, 190, 240);
jDesktopPane1.add(kalanhesapla, javax.swing.JLayeredPane.DEFAULT_LAYER);
jMenu1.setText("File");
Baslat.setText("Başla");
Baslat.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BaslatActionPerformed(evt);
}
});
jMenu1.add(Baslat);
cikis.setText("Çıkış");
cikis.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cikisActionPerformed(evt);
}
});
jMenu1.add(cikis);
jMenuBar1.add(jMenu1);
jMenu2.setText("Yardım");
hakkindamenu.setText("Hakkında");
hakkindamenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
hakkindamenuActionPerformed(evt);
}
});
jMenu2.add(hakkindamenu);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jDesktopPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);
pack();
}//
private void hakkindamenuActionPerformed(java.awt.event.ActionEvent evt) {
hakkinda.show();
}
private void BaslatActionPerformed(java.awt.event.ActionEvent evt) {
kalanhesapla.show();
}
private void cikisActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void bolenyazActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void bolunenyazActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void kalanyazActionPerformed(java.awt.event.ActionEvent evt) {
}
private void silActionPerformed(java.awt.event.ActionEvent evt) {
kalanyaz.setText("Kalan");
bolunenyaz.setText("Bölünen");
bolumyaz.setText("Bölüm");
bolenyaz.setText("Bölen");
}
private void hesaplatActionPerformed(java.awt.event.ActionEvent evt) {
islem calis=new islem();
String sonucu=calis.sonuc(bolunenyaz.getText(), bolenyaz.getText());
kalanyaz.setText("Kalan : "+sonucu);
bolumyaz.setText("Bölüm : "+Integer.toString(calis.bolum));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//
/*
* 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(KalanHesaplama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(KalanHesaplama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(KalanHesaplama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(KalanHesaplama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new KalanHesaplama().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Baslat;
private javax.swing.JTextField bolenyaz;
private javax.swing.JTextField bolumyaz;
private javax.swing.JTextField bolunenyaz;
private javax.swing.JMenuItem cikis;
private javax.swing.JInternalFrame hakkinda;
private javax.swing.JMenuItem hakkindamenu;
private javax.swing.JButton hesaplat;
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel4;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JInternalFrame kalanhesapla;
private javax.swing.JTextField kalanyaz;
private javax.swing.JButton sil;
// End of variables declaration
}
İkinci Java Class'ı ise islem.java;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package kalanhesapla;
/**
*
* @author turgut
*/
public class islem {
int bolunen;
int bolen;
int bolum;
int kalan;
public String sonuc(String b,String b1){
String kalani="";
bolunen=Integer.parseInt(b);
bolen=Integer.parseInt(b1);
bolum=bolunen/bolen;
kalan=bolunen-(bolunen/bolen)*bolen;
kalani=Integer.toString(kalan);
return kalani;
}
}
Turgut Arslan