Java Dilinde Alan ve Çevre Hesaplama Uygulaması

Bilgisayarcı

Çalışkan Üye
Katılım
10 Mar 2018
Mesajlar
200
Beğeniler
0
GUI temelleri için güzel bir kaynak olabilir.
Uygulama iki adet sınıf içermektedir.
AlanCevre Sınıfı
Kod:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class AlanCevre extends JFrame {

	/**
	 * Diktörtgen Alan ve Çevre Hesaplama Uygulaması GUI Sınıfı
	 * 
	 * @author Kilitbilgi
	 * @since 21.06.2013 21.02
	 */
	private static final long serialVersionUID = 1L;
	private JPanel panel;
	private JLabel en;
	private JLabel boy;
	private JTextField engiris;
	private JTextField boygiris;
	private JTextArea sonuc;
	private JButton alanbuton;
	private JButton cevrebuton;

	public AlanCevre() {
		panel = new JPanel();
		en = new JLabel("En");
		boy = new JLabel("Boy");
		engiris = new JTextField(10);
		boygiris = new JTextField(10);
		alanbuton = new JButton("Alan");
		alanbuton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AlanHesap(e);
			}
		});
		cevrebuton = new JButton("Çevre");
		cevrebuton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CevreHesap(e);
			}
		});
		sonuc = new JTextArea(5, 25);
		sonuc.setEditable(false);
		panel.add(en);
		panel.add(engiris);
		panel.add(boy);
		panel.add(boygiris);
		panel.add(alanbuton);
		panel.add(cevrebuton);
		panel.add(sonuc);
		add(panel);
	}

	private void AlanHesap(ActionEvent e) {
		try {
			double alan = Double.parseDouble(engiris.getText())
					* Double.parseDouble(boygiris.getText());
			sonuc.setText(String.valueOf(alan));
		} catch (NumberFormatException ex) {
			sonuc.setText("");
			engiris.setText("");
			boygiris.setText("");
			JOptionPane.showMessageDialog(null,
					"Lütfen parametleri doğru bir şekilde giriniz");
		} catch (Exception ex) {
			sonuc.setText("");
			engiris.setText("");
			boygiris.setText("");
			JOptionPane.showMessageDialog(null, "Hatalı işlem");
		}
	}

	private void CevreHesap(ActionEvent e) {
		try {
			double cevre = (Double.parseDouble(engiris.getText()) + Double
					.parseDouble(boygiris.getText())) * 2;
			sonuc.setText(String.valueOf(cevre));
		} catch (NumberFormatException ex) {
			sonuc.setText("");
			engiris.setText("");
			boygiris.setText("");
			JOptionPane.showMessageDialog(null,
					"Lütfen parametleri doğru bir şekilde giriniz");
		} catch (Exception ex) {
			sonuc.setText("");
			engiris.setText("");
			boygiris.setText("");
			JOptionPane.showMessageDialog(null, "Hatalı işlem");
		}
	}

}
Test Sınıfı
Kod:
import javax.swing.JFrame;

public class Test {

	/**
	 * Alan ve Çevre Hesaplama Uygulama Test Sınıfı
	 * 
	 * @author Kilitbilgi
	 * @since 21.06.2013 21.02
	 */
	public static void main(String[] args) {
		JFrame frame = new AlanCevre();
		frame.setVisible(true);
		frame.setSize(330, 200);
		frame.set********RelativeTo(null);
		frame.setTitle("Diktörtgen Alan ve Çevre Hesabı");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}
 

Şu anda bu konu'yu okuyan kullanıcılar

Benzer konular