Merhaba arkadaşlar,
Bir önceki yazımızda java ile dinamik hesap makinesini nasıl yazacağınızı göndermiştik, Bu yazımızda ise basit bir hesap makinesi nasıl yazılır onu gösterip kodlarını paylaşacağız. Bütün butonları tek tek yerleştirip hepsine birer görev vereceğiz. yani işimiz biraz amelelik olacak.
Yazacağımız Hesap Makinesinin Ekran Görüntüsü şu şekilde olacak;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
package test; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Font; import javax.swing.SwingConstants; public class gorsel1 extends JFrame { private JPanel contentPane; private JTextField textField; int s1,s2,sonuc; int islem; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { gorsel1 frame = new gorsel1(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public gorsel1() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 308, 282); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnNewButton = new JButton("0"); btnNewButton.setFont(new Font("Lora", Font.BOLD, 14)); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+0); } }); btnNewButton.setBounds(10, 203, 161, 39); contentPane.add(btnNewButton); textField = new JTextField(); textField.setHorizontalAlignment(SwingConstants.RIGHT); textField.setBounds(10, 11, 272, 32); contentPane.add(textField); textField.setColumns(10); JButton button = new JButton("7"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+7); } }); button.setFont(new Font("Lora", Font.BOLD, 14)); button.setBounds(10, 53, 47, 39); contentPane.add(button); JButton button_1 = new JButton("8"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+8); } }); button_1.setFont(new Font("Lora", Font.BOLD, 14)); button_1.setBounds(67, 53, 47, 39); contentPane.add(button_1); JButton button_2 = new JButton("9"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+9); } }); button_2.setFont(new Font("Lora", Font.BOLD, 14)); button_2.setBounds(124, 53, 47, 39); contentPane.add(button_2); JButton button_3 = new JButton("4"); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+4); } }); button_3.setFont(new Font("Lora", Font.BOLD, 14)); button_3.setBounds(10, 103, 47, 39); contentPane.add(button_3); JButton button_4 = new JButton("5"); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+5); } }); button_4.setFont(new Font("Lora", Font.BOLD, 14)); button_4.setBounds(67, 103, 47, 39); contentPane.add(button_4); JButton button_5 = new JButton("6"); button_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+6); } }); button_5.setFont(new Font("Lora", Font.BOLD, 14)); button_5.setBounds(124, 103, 47, 39); contentPane.add(button_5); JButton button_6 = new JButton("1"); button_6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+1); } }); button_6.setFont(new Font("Lora", Font.BOLD, 14)); button_6.setBounds(10, 153, 47, 39); contentPane.add(button_6); JButton button_7 = new JButton("2"); button_7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+2); } }); button_7.setFont(new Font("Lora", Font.BOLD, 14)); button_7.setBounds(67, 153, 47, 39); contentPane.add(button_7); JButton button_8 = new JButton("3"); button_8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+3); } }); button_8.setFont(new Font("Lora", Font.BOLD, 14)); button_8.setBounds(124, 153, 47, 39); contentPane.add(button_8); JButton button_9 = new JButton("+"); button_9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s1 = Integer.parseInt(textField.getText()); islem = 1; textField.setText(""); } }); button_9.setFont(new Font("Lora", Font.BOLD, 14)); button_9.setBounds(181, 103, 47, 39); contentPane.add(button_9); JButton button_10 = new JButton("-"); button_10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s1 = Integer.parseInt(textField.getText()); islem = 2; textField.setText(""); } }); button_10.setFont(new Font("Lora", Font.BOLD, 14)); button_10.setBounds(238, 103, 47, 39); contentPane.add(button_10); JButton button_11 = new JButton("*"); button_11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s1 = Integer.parseInt(textField.getText()); islem = 3; textField.setText(""); } }); button_11.setFont(new Font("Lora", Font.BOLD, 14)); button_11.setBounds(181, 153, 47, 39); contentPane.add(button_11); JButton button_12 = new JButton("/"); button_12.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s1 = Integer.parseInt(textField.getText()); islem = 4; textField.setText(""); } }); button_12.setFont(new Font("Lora", Font.BOLD, 14)); button_12.setBounds(238, 153, 47, 39); contentPane.add(button_12); JButton btnSil = new JButton("S\u0130L"); btnSil.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(""); } }); btnSil.setFont(new Font("Lora", Font.BOLD, 14)); btnSil.setBounds(181, 53, 101, 39); contentPane.add(btnSil); JButton button_13 = new JButton("="); button_13.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s2 = Integer.parseInt(textField.getText()); if(islem==1) { sonuc = s1 + s2; textField.setText(Integer.toString(sonuc)); } else if(islem==2) { sonuc = s1 - s2; textField.setText(Integer.toString(sonuc)); } else if(islem==3) { sonuc = s1 * s2; textField.setText(Integer.toString(sonuc)); } else if(islem==4) { sonuc = s1 / s2; textField.setText(Integer.toString(sonuc)); } } }); button_13.setFont(new Font("Lora", Font.BOLD, 14)); button_13.setBounds(181, 203, 104, 39); contentPane.add(button_13); } } |
[…] Java ile Yazdığımız Basit Hesap Makinesini Gitmek için TIKLAYINIZ. […]