- package SW22;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.text.DecimalFormat;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import java.awt.Font;
- import javax.swing.SwingConstants;
- import javax.swing.JTextField;
- import java.awt.Color;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class gui_test extends JFrame {
- private JPanel contentPane;
- private JTextField tfKatheteA;
- private JTextField tfHypotenuseC;
- private JTextField tfKatheteB;
- private JTextField tfFlaecheA;
- private JTextField tfAlpha;
- private JTextField tfBeta;
- private JTextField tfUmfang;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- gui_test frame = new gui_test();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public gui_test()
- {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 508, 321);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JLabel lblNewLabel = new JLabel("Rechtwinkliges Dreieck: Gesucht Kathete b");
- lblNewLabel.setBounds(0, 11, 434, 30);
- lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
- lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
- contentPane.add(lblNewLabel);
- JLabel lblNewLabel_1 = new JLabel("Eingabe");
- lblNewLabel_1.setBounds(25, 42, 96, 20);
- lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
- contentPane.add(lblNewLabel_1);
- JLabel lblNewLabel_2 = new JLabel("Kathete a [cm]:");
- lblNewLabel_2.setBounds(35, 73, 101, 14);
- lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
- contentPane.add(lblNewLabel_2);
- JLabel lblHypotenuseCcm = new JLabel("Hypotenuse c [cm]");
- lblHypotenuseCcm.setBounds(35, 108, 121, 14);
- lblHypotenuseCcm.setFont(new Font("Tahoma", Font.PLAIN, 12));
- contentPane.add(lblHypotenuseCcm);
- tfKatheteA = new JTextField();
- tfKatheteA.setBounds(159, 71, 52, 20);
- tfKatheteA.setBackground(new Color(154, 205, 50));
- tfKatheteA.setFont(new Font("Tahoma", Font.PLAIN, 12));
- contentPane.add(tfKatheteA);
- tfKatheteA.setColumns(10);
- tfHypotenuseC = new JTextField();
- tfHypotenuseC.setBounds(159, 106, 52, 20);
- tfHypotenuseC.setBackground(new Color(154, 205, 50));
- tfHypotenuseC.setFont(new Font("Tahoma", Font.PLAIN, 12));
- tfHypotenuseC.setColumns(10);
- contentPane.add(tfHypotenuseC);
- JLabel lblAusgabe = new JLabel("Ausgabe");
- lblAusgabe.setBounds(259, 47, 96, 20);
- lblAusgabe.setFont(new Font("Tahoma", Font.PLAIN, 14));
- contentPane.add(lblAusgabe);
- tfKatheteB = new JTextField();
- tfKatheteB.setFont(new Font("Tahoma", Font.PLAIN, 12));
- tfKatheteB.setColumns(10);
- tfKatheteB.setBackground(new Color(230, 230, 250));
- tfKatheteB.setBounds(407, 70, 52, 20);
- contentPane.add(tfKatheteB);
- JLabel label = new JLabel("Kathete b [cm]:");
- label.setFont(new Font("Tahoma", Font.PLAIN, 12));
- label.setBounds(269, 73, 101, 14);
- contentPane.add(label);
- JLabel lblFlcheAcm = new JLabel("Fl\u00E4che A [cm\u00B2]:");
- lblFlcheAcm.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lblFlcheAcm.setBounds(269, 108, 101, 14);
- contentPane.add(lblFlcheAcm);
- JLabel lblWinkelAlpha = new JLabel("Winkel Alpha [\u00B0]");
- lblWinkelAlpha.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lblWinkelAlpha.setBounds(269, 144, 101, 14);
- contentPane.add(lblWinkelAlpha);
- JLabel lblWinkelBeta = new JLabel("Winkel Beta [\u00B0]");
- lblWinkelBeta.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lblWinkelBeta.setBounds(269, 178, 101, 14);
- contentPane.add(lblWinkelBeta);
- JLabel lblUmfangUcm = new JLabel("Umfang U [cm]:");
- lblUmfangUcm.setFont(new Font("Tahoma", Font.PLAIN, 12));
- lblUmfangUcm.setBounds(269, 207, 101, 14);
- contentPane.add(lblUmfangUcm);
- tfFlaecheA = new JTextField();
- tfFlaecheA.setFont(new Font("Tahoma", Font.PLAIN, 12));
- tfFlaecheA.setColumns(10);
- tfFlaecheA.setBackground(new Color(230, 230, 250));
- tfFlaecheA.setBounds(407, 106, 52, 20);
- contentPane.add(tfFlaecheA);
- tfAlpha = new JTextField();
- tfAlpha.setFont(new Font("Tahoma", Font.PLAIN, 12));
- tfAlpha.setColumns(10);
- tfAlpha.setBackground(new Color(230, 230, 250));
- tfAlpha.setBounds(407, 142, 52, 20);
- contentPane.add(tfAlpha);
- tfBeta = new JTextField();
- tfBeta.setFont(new Font("Tahoma", Font.PLAIN, 12));
- tfBeta.setColumns(10);
- tfBeta.setBackground(new Color(230, 230, 250));
- tfBeta.setBounds(407, 176, 52, 20);
- contentPane.add(tfBeta);
- tfUmfang = new JTextField();
- tfUmfang.setFont(new Font("Tahoma", Font.PLAIN, 12));
- tfUmfang.setColumns(10);
- tfUmfang.setBackground(new Color(230, 230, 250));
- tfUmfang.setBounds(407, 205, 52, 20);
- contentPane.add(tfUmfang);
- JButton btnBerechnen = new JButton("Berechnen");
- btnBerechnen.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- berechnen();
- }
- });
- btnBerechnen.setBounds(32, 248, 89, 23);
- contentPane.add(btnBerechnen);
- JButton btnLoeschen = new JButton("L\u00F6schen");
- btnLoeschen.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- leeren();
- }
- });
- btnLoeschen.setBounds(184, 248, 89, 23);
- contentPane.add(btnLoeschen);
- JButton btnEnde = new JButton("Ende");
- btnEnde.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- beenden();
- }
- });
- btnEnde.setBounds(370, 248, 89, 23);
- contentPane.add(btnEnde);
- } // Ende der Gui-Erstellung
- // Ab hier : Eigene Funktionen
- private void beenden() {
- System.exit(0);
- }
- private void leeren() {
- tfAlpha.setText(null);
- tfBeta.setText(null);
- tfFlaecheA.setText(null);
- tfHypotenuseC.setText(null);
- tfKatheteA.setText(null);
- tfKatheteB.setText(null);
- tfUmfang.setText(null);
- }
- private void berechnen() {
- DecimalFormat nk3;
- nk3 = new DecimalFormat("#.###");
- double a, b, c, umfang, flaeche, alpha, beta;
- a = Double.parseDouble(tfKatheteA.getText());
- c = Double.parseDouble(tfHypotenuseC.getText());
- // b = Double.parseDouble(tfKatheteB.getText());
- if (c > a){
- // Verarbeitung (Formeln)
- b = Math.sqrt(Math.pow(c,2)-Math.pow(a,2));
- flaeche = a * b / 2;
- umfang = a + b + c;
- alpha = Math.atan(a / b) * 180 / Math.PI;
- beta = 90 - alpha;
- // Ausgabe der Ergebnisse
- tfKatheteB.setText(nk3.format(b));
- tfKatheteA.setText(nk3.format(a));
- tfFlaecheA.setText(nk3.format(flaeche));
- tfHypotenuseC.setText(nk3.format(c));
- tfUmfang.setText(nk3.format(umfang));
- tfAlpha.setText(nk3.format(alpha));
- tfBeta.setText(nk3.format(beta));
- }else {
- JOptionPane.showMessageDialog(null, "c muss größer sein!", "Fehlerhafte Eingabe!", JOptionPane.WARNING_MESSAGE);
- tfKatheteA.setText("" + c);
- tfHypotenuseC.setText("" + a);
- JOptionPane.showMessageDialog(null, "Werte wurden getauscht!");
- }
- }
- }