Facebook
From Little Capybara, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 60
  1. package SW22;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import java.text.DecimalFormat;
  7.  
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10. import javax.swing.border.EmptyBorder;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13.  
  14. import java.awt.Font;
  15. import javax.swing.SwingConstants;
  16. import javax.swing.JTextField;
  17. import java.awt.Color;
  18. import javax.swing.JButton;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.ActionEvent;
  21.  
  22. public class gui_test extends JFrame {
  23.  
  24.         private JPanel contentPane;
  25.         private JTextField tfKatheteA;
  26.         private JTextField tfHypotenuseC;
  27.         private JTextField tfKatheteB;
  28.         private JTextField tfFlaecheA;
  29.         private JTextField tfAlpha;
  30.         private JTextField tfBeta;
  31.         private JTextField tfUmfang;
  32.  
  33.         /**
  34.          * Launch the application.
  35.          */
  36.         public static void main(String[] args) {
  37.                 EventQueue.invokeLater(new Runnable() {
  38.                         public void run() {
  39.                                 try {
  40.                                         gui_test frame = new gui_test();
  41.                                         frame.setVisible(true);
  42.                                 } catch (Exception e) {
  43.                                         e.printStackTrace();
  44.                                 }
  45.                         }
  46.                 });
  47.         }
  48.  
  49.         /**
  50.          * Create the frame.
  51.          */
  52.         public gui_test()
  53.         {
  54.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.                 setBounds(100, 100, 508, 321);
  56.                 contentPane = new JPanel();
  57.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  58.                 setContentPane(contentPane);
  59.                 contentPane.setLayout(null);
  60.                
  61.                 JLabel lblNewLabel = new JLabel("Rechtwinkliges Dreieck: Gesucht Kathete b");
  62.                 lblNewLabel.setBounds(0, 11, 434, 30);
  63.                 lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
  64.                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
  65.                 contentPane.add(lblNewLabel);
  66.                
  67.                 JLabel lblNewLabel_1 = new JLabel("Eingabe");
  68.                 lblNewLabel_1.setBounds(25, 42, 96, 20);
  69.                 lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
  70.                 contentPane.add(lblNewLabel_1);
  71.                
  72.                 JLabel lblNewLabel_2 = new JLabel("Kathete a [cm]:");
  73.                 lblNewLabel_2.setBounds(35, 73, 101, 14);
  74.                 lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
  75.                 contentPane.add(lblNewLabel_2);
  76.                
  77.                 JLabel lblHypotenuseCcm = new JLabel("Hypotenuse c [cm]");
  78.                 lblHypotenuseCcm.setBounds(35, 108, 121, 14);
  79.                 lblHypotenuseCcm.setFont(new Font("Tahoma", Font.PLAIN, 12));
  80.                 contentPane.add(lblHypotenuseCcm);
  81.                
  82.                 tfKatheteA = new JTextField();
  83.                 tfKatheteA.setBounds(159, 71, 52, 20);
  84.                 tfKatheteA.setBackground(new Color(154, 205, 50));
  85.                 tfKatheteA.setFont(new Font("Tahoma", Font.PLAIN, 12));
  86.                 contentPane.add(tfKatheteA);
  87.                 tfKatheteA.setColumns(10);
  88.                
  89.                 tfHypotenuseC = new JTextField();
  90.                 tfHypotenuseC.setBounds(159, 106, 52, 20);
  91.                 tfHypotenuseC.setBackground(new Color(154, 205, 50));
  92.                 tfHypotenuseC.setFont(new Font("Tahoma", Font.PLAIN, 12));
  93.                 tfHypotenuseC.setColumns(10);
  94.                 contentPane.add(tfHypotenuseC);
  95.                
  96.                 JLabel lblAusgabe = new JLabel("Ausgabe");
  97.                 lblAusgabe.setBounds(259, 47, 96, 20);
  98.                 lblAusgabe.setFont(new Font("Tahoma", Font.PLAIN, 14));
  99.                 contentPane.add(lblAusgabe);
  100.                
  101.                 tfKatheteB = new JTextField();
  102.                 tfKatheteB.setFont(new Font("Tahoma", Font.PLAIN, 12));
  103.                 tfKatheteB.setColumns(10);
  104.                 tfKatheteB.setBackground(new Color(230, 230, 250));
  105.                 tfKatheteB.setBounds(407, 70, 52, 20);
  106.                 contentPane.add(tfKatheteB);
  107.                
  108.                 JLabel label = new JLabel("Kathete b [cm]:");
  109.                 label.setFont(new Font("Tahoma", Font.PLAIN, 12));
  110.                 label.setBounds(269, 73, 101, 14);
  111.                 contentPane.add(label);
  112.                
  113.                 JLabel lblFlcheAcm = new JLabel("Fl\u00E4che A  [cm\u00B2]:");
  114.                 lblFlcheAcm.setFont(new Font("Tahoma", Font.PLAIN, 12));
  115.                 lblFlcheAcm.setBounds(269, 108, 101, 14);
  116.                 contentPane.add(lblFlcheAcm);
  117.                
  118.                 JLabel lblWinkelAlpha = new JLabel("Winkel Alpha [\u00B0]");
  119.                 lblWinkelAlpha.setFont(new Font("Tahoma", Font.PLAIN, 12));
  120.                 lblWinkelAlpha.setBounds(269, 144, 101, 14);
  121.                 contentPane.add(lblWinkelAlpha);
  122.                
  123.                 JLabel lblWinkelBeta = new JLabel("Winkel Beta [\u00B0]");
  124.                 lblWinkelBeta.setFont(new Font("Tahoma", Font.PLAIN, 12));
  125.                 lblWinkelBeta.setBounds(269, 178, 101, 14);
  126.                 contentPane.add(lblWinkelBeta);
  127.                
  128.                 JLabel lblUmfangUcm = new JLabel("Umfang U [cm]:");
  129.                 lblUmfangUcm.setFont(new Font("Tahoma", Font.PLAIN, 12));
  130.                 lblUmfangUcm.setBounds(269, 207, 101, 14);
  131.                 contentPane.add(lblUmfangUcm);
  132.                
  133.                 tfFlaecheA = new JTextField();
  134.                 tfFlaecheA.setFont(new Font("Tahoma", Font.PLAIN, 12));
  135.                 tfFlaecheA.setColumns(10);
  136.                 tfFlaecheA.setBackground(new Color(230, 230, 250));
  137.                 tfFlaecheA.setBounds(407, 106, 52, 20);
  138.                 contentPane.add(tfFlaecheA);
  139.                
  140.                 tfAlpha = new JTextField();
  141.                 tfAlpha.setFont(new Font("Tahoma", Font.PLAIN, 12));
  142.                 tfAlpha.setColumns(10);
  143.                 tfAlpha.setBackground(new Color(230, 230, 250));
  144.                 tfAlpha.setBounds(407, 142, 52, 20);
  145.                 contentPane.add(tfAlpha);
  146.                
  147.                 tfBeta = new JTextField();
  148.                 tfBeta.setFont(new Font("Tahoma", Font.PLAIN, 12));
  149.                 tfBeta.setColumns(10);
  150.                 tfBeta.setBackground(new Color(230, 230, 250));
  151.                 tfBeta.setBounds(407, 176, 52, 20);
  152.                 contentPane.add(tfBeta);
  153.                
  154.                 tfUmfang = new JTextField();
  155.                 tfUmfang.setFont(new Font("Tahoma", Font.PLAIN, 12));
  156.                 tfUmfang.setColumns(10);
  157.                 tfUmfang.setBackground(new Color(230, 230, 250));
  158.                 tfUmfang.setBounds(407, 205, 52, 20);
  159.                 contentPane.add(tfUmfang);
  160.                
  161.                 JButton btnBerechnen = new JButton("Berechnen");
  162.                 btnBerechnen.addActionListener(new ActionListener() {
  163.                         public void actionPerformed(ActionEvent e) {
  164.                                 berechnen();
  165.                         }
  166.                 });
  167.                 btnBerechnen.setBounds(32, 248, 89, 23);
  168.                 contentPane.add(btnBerechnen);
  169.                
  170.                 JButton btnLoeschen = new JButton("L\u00F6schen");
  171.                 btnLoeschen.addActionListener(new ActionListener() {
  172.                         public void actionPerformed(ActionEvent e) {
  173.                                 leeren();
  174.                         }
  175.                 });
  176.                 btnLoeschen.setBounds(184, 248, 89, 23);
  177.                 contentPane.add(btnLoeschen);
  178.                
  179.                 JButton btnEnde = new JButton("Ende");
  180.                 btnEnde.addActionListener(new ActionListener() {
  181.                         public void actionPerformed(ActionEvent e) {
  182.                                 beenden();
  183.                         }
  184.                 });
  185.                 btnEnde.setBounds(370, 248, 89, 23);
  186.                 contentPane.add(btnEnde);
  187.         }   // Ende der Gui-Erstellung
  188.         // Ab hier : Eigene Funktionen
  189.        
  190.         private void beenden() {
  191.                 System.exit(0);
  192.         }
  193.        
  194.         private void leeren() {
  195.                 tfAlpha.setText(null);
  196.                 tfBeta.setText(null);
  197.                 tfFlaecheA.setText(null);
  198.                 tfHypotenuseC.setText(null);
  199.                 tfKatheteA.setText(null);
  200.                 tfKatheteB.setText(null);
  201.                 tfUmfang.setText(null);
  202.  
  203.         }
  204.        
  205.         private void berechnen() {
  206.                
  207.                 DecimalFormat nk3;
  208.                 nk3 = new DecimalFormat("#.###");
  209.                
  210.                 double a, b, c, umfang, flaeche, alpha, beta;
  211.                
  212.                 a = Double.parseDouble(tfKatheteA.getText());
  213.                
  214.                 c = Double.parseDouble(tfHypotenuseC.getText());
  215.                
  216.                 // b = Double.parseDouble(tfKatheteB.getText());
  217.                
  218.                 if (c > a){
  219.                        
  220.                 // Verarbeitung (Formeln)
  221.                 b = Math.sqrt(Math.pow(c,2)-Math.pow(a,2));
  222.                 flaeche = a * b / 2;
  223.                 umfang = a + b + c;
  224.                 alpha = Math.atan(a / b) * 180 / Math.PI;
  225.                 beta = 90 - alpha;
  226.  
  227.                 // Ausgabe der Ergebnisse
  228.                 tfKatheteB.setText(nk3.format(b));
  229.                 tfKatheteA.setText(nk3.format(a));
  230.                 tfFlaecheA.setText(nk3.format(flaeche));
  231.                 tfHypotenuseC.setText(nk3.format(c));
  232.                 tfUmfang.setText(nk3.format(umfang));
  233.                 tfAlpha.setText(nk3.format(alpha));
  234.                 tfBeta.setText(nk3.format(beta));
  235.  
  236.                 }else {
  237.                         JOptionPane.showMessageDialog(null, "c muss größer sein!", "Fehlerhafte Eingabe!", JOptionPane.WARNING_MESSAGE);
  238.                         tfKatheteA.setText("" + c);
  239.                         tfHypotenuseC.setText("" + a);
  240.                         JOptionPane.showMessageDialog(null, "Werte wurden getauscht!");
  241.  
  242.                 }
  243.                
  244.         }
  245.        
  246.        
  247. }
  248.