/* Vorgegebenes Hauptprogramm (GUI) zu Blatt 10, AI 2, SS 2007 * * nh - 06/2007 */ import java.awt.*; import java.awt.event.*; class MainFrame extends Frame implements ActionListener { public final static int ADD = 1; // Konstante fuer Addition public final static int SUB = 2; // Konstante fuer Subtraktion // Grafische Elemente anlegen Button btnAddition, btnSubtraction, btnReset, btnQuit; TextField txtPoly1, txtPoly2; TextArea txtResult; Label lblHeader, lblPoly1, lblPoly2; // Action-Handler public void actionPerformed(ActionEvent e) { // Addition if (e.getSource() == btnAddition) { calcPolynomials(ADD); return; } // Subtraktion if (e.getSource() == btnSubtraction) { calcPolynomials(SUB); return; } // Leeren (Reset) if (e.getSource() == btnReset) { txtPoly1.setText(""); txtPoly2.setText(""); txtResult.setText(""); txtPoly1.requestFocus(); // Cursor in Feld fuer Polynom 1 setzen return; } // Beenden if (e.getSource() == btnQuit) { dispose(); // gibt alle Ressourcen frei und schliesst das Fenster System.exit(0); // beendet die Applikation } } /* Berechnung der Polynome anstossen; * die eigentliche Berechnung erfolgt natuerlich in der Klasse Polynom */ private void calcPolynomials(int mode) { Polynomial p1, p2; Polynomial result = null; /* Umwandeln des eingegebenen Strings in das erste Textfeld in ein * Polynom - wenn das nicht funktioniert (z.B. durch ungueltige Eingabe) * wird eine entsprechende Ausnahme geworfen */ try { p1 = new Polynomial(txtPoly1.getText()); } catch (PolynomialException e) { handleError("Fehler beim ersten Polynom: " + e.getMessage()); return; } // analoges Vorgehen fuer das zweite Polynom-Textfeld try { p2 = new Polynomial(txtPoly2.getText()); } catch (PolynomialException e) { handleError("Fehler beim zweiten Polynom: " + e.getMessage()); return; } // Berechnung anstossen switch (mode) { case ADD: result = Polynomial.add(p1, p2); break; case SUB: result = Polynomial.subtract(p1, p2); break; default: break; } // Ausgabe des Ergebnisses in das Ausgabetextfeld txtResult.setText(result.toString()); } // schreibt eine Fehlermeldung in das Ausgabetextfeld private void handleError(String errStr) { txtResult.setText(errStr); } // grafische Elemente initialisieren private void initComponents() { // Header lblHeader = new Label("Der AI2-Polynom-Rechner"); lblHeader.setFont(new Font("Dialog", 1, 18)); // Schriftart und -groesse lblHeader.setAlignment(Label.CENTER); // Beschriftungen und Eingabefelder lblPoly1 = new Label("Polynom 1:"); txtPoly1 = new TextField(); lblPoly2 = new Label("Polynom 2:"); txtPoly2 = new TextField(); txtResult = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY); txtResult.setEditable(false); // keine Eingabe in das Feld zulassen // Buttons btnAddition = new Button("P1 + P2"); btnAddition.setFont(new Font("Dialog", 1, 18)); btnAddition.addActionListener(this); btnSubtraction = new Button("P1 - P2"); btnSubtraction.setFont(new Font("Dialog", 1, 18)); btnSubtraction.addActionListener(this); btnReset = new Button("Leeren"); btnReset.addActionListener(this); btnQuit = new Button("Beenden"); btnQuit.addActionListener(this); } // Hauptpanel erzeugen - setzt sich zusammen aus mehreren einzelnen Panels private Panel createMainPanel() { Panel pm = new Panel(); pm.setLayout(new GridLayout(4,1)); Panel p1 = new Panel(); p1.setLayout(new GridLayout(1,2)); p1.add(lblPoly1); p1.add(txtPoly1); pm.add(p1); Panel p2 = new Panel(); p2.setLayout(new GridLayout(1,2)); p2.add(lblPoly2); p2.add(txtPoly2); pm.add(p2); Panel p3 = new Panel(); p3.setLayout(new GridLayout(1,4)); p3.add(new Label()); // leeres Label als Lueckenfueller p3.add(btnAddition); p3.add(btnSubtraction); p3.add(new Label()); // siehe oben pm.add(p3); pm.add(txtResult); return pm; } // und noch eine Methode zum Erzeugen eines Panels private Panel createButtonPanel() { Panel p = new Panel(); p.setLayout(new GridLayout(1,2)); p.add(btnReset); p.add(btnQuit); return p; } // Konstruktur public MainFrame() { super("Der AI2-Polynom-Rechner"); initComponents(); // GUI-Komponenten erzeugen und initialisieren setSize(350, 350); setLayout(new BorderLayout()); add("North", lblHeader); add("Center", createMainPanel()); add("South", createButtonPanel()); setVisible(true); // alles anzeigen -> Programm laeuft } } public class PolynomCalcGUI { public static void main(String[] args) { new MainFrame(); } }