Nesne Tabanlı Programlama-2 (Bahar-2016) Hafta-2/2-2
import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.*; import javax.swing.*; public class hesapmak extends JFrame implements ActionListener { JTextField jtf1, jtf2; JLabel jlab; public hesapmak() { setSize(100, 150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); Dimension dmn1 = new Dimension(90, 20); Dimension dmn2 = new Dimension(90, 30); jtf1= new JTextField(); jtf2= new JTextField(); jlab = new JLabel("-"); JButton jbtntopla= new JButton("+"); JButton jbtncikar= new JButton("-"); jbtntopla.setActionCommand("toplama"); jbtntopla.addActionListener(this); jbtncikar.addActionListener(this); jtf1.setPreferredSize(dmn1); jtf2.setPreferredSize(dmn1); jlab.setPreferredSize(dmn1); jbtntopla.setPreferredSize(dmn2); jbtncikar.setPreferredSize(dmn2); add(jtf1); add(jtf2); add(jlab); add(jbtntopla); add(jbtncikar); setVisible(true); } @Override public void actionPerformed(ActionEvent arg0) { try{ int sayi1= Integer.parseInt(jtf1.getText()); int sayi2= Integer.parseInt(jtf2.getText()); if(arg0.getActionCommand().equals("toplama")) jlab.setText("Sonuç:"+(sayi1+sayi2)); else jlab.setText("Sonuç:"+(sayi1-sayi2)); } catch(Exception ex) { JOptionPane.showMessageDialog(this, "Girilen Bilgiler Uygun Formatta Değil!","Hata!",JOptionPane.ERROR_MESSAGE); } } /** * @param args */ public static void main(String[] args) { new hesapmak(); } }