GUI fertig

This commit is contained in:
2021-04-19 15:09:32 +02:00
parent d531bf0d4f
commit 320a48bfea
3 changed files with 184 additions and 109 deletions

45
GUI.form Normal file
View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="GUI">
<grid id="27dc6" binding="panelMain" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="36781" class="javax.swing.JTextField" binding="textFieldRechenterm">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="6de4a" class="javax.swing.JButton" binding="buttonBerechnen">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Berechnen"/>
</properties>
</component>
<component id="4aaff" class="javax.swing.JLabel" binding="labelGibRechentermEin">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Bitte gib einen Rechenterm ein."/>
</properties>
</component>
<component id="d95b1" class="javax.swing.JLabel" binding="labelErgebnis">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Ergebnis"/>
</properties>
</component>
</children>
</grid>
</form>

36
GUI.java Normal file
View File

@@ -0,0 +1,36 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame {
private JPanel panelMain;
private JTextField textFieldRechenterm;
private JLabel labelGibRechentermEin;
private JButton buttonBerechnen;
private JLabel labelErgebnis;
public GUI(String title){
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(panelMain);
this.pack();
buttonBerechnen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//TODO: Text vom Text field zu Eingabe übergeben
Rechenmaschine rechner = new Rechenmaschine(textFieldRechenterm.getText());
labelErgebnis.setText("Das Ergebnis ist: "+rechner.getResult());
}
});
}
public static void main(String[] args) {
JFrame frame = new GUI("Rechenterme");
frame.setVisible(true);
}
private void createUIComponents() {
// TODO: place custom component creation code here
}
}

View File

@@ -5,26 +5,17 @@ import java.text.DecimalFormat;
*/
public class Rechenmaschine {
public static void main(String[] args) {
new Rechenmaschine();
}
private List<Token> tokenlist;
private String fehler;
private double result;
private String result;
private int klammern;
public Rechenmaschine() {
public Rechenmaschine(String eingabe) {
tokenlist = new List<>();
Scanner input = new Scanner(System.in);
System.out.println("Gib einen Term ein und bestätige mit ENTER: ");
String eingabe = input.nextLine();
input.close();
if (!scanne(eingabe)) {
System.out.println("Fehler bei der lexikalischen Analyse:");
System.out.println(fehler);
@@ -47,6 +38,7 @@ public class Rechenmaschine {
}
public boolean scanne(String pEingabe) {
char[] eingabe = pEingabe.toCharArray();
klammern = 0;
@@ -336,7 +328,6 @@ public class Rechenmaschine {
}
case 1:
if (currentToken.getType().equals("OPERATOR")) {
state = 0;
@@ -475,6 +466,7 @@ public class Rechenmaschine {
pList.next();
}
}
private double mehrereKlammern(List<Token> pList) {
List<Token> newTokenlist = new List<Token>();
double berechnet = 0;
@@ -486,7 +478,6 @@ public class Rechenmaschine {
while (!pList.getContent().getToken().equals(")")) {
if (pList.hasAccess()) {
//wenn current ( ist dann wird diese methode nochmal ab current ausgeführt und anschließend
// das ergebnis in newTokenlist eingefügt
@@ -537,7 +528,10 @@ public class Rechenmaschine {
double result = berechne(tokenlist);
DecimalFormat format = new DecimalFormat("#,##0.####");
String resultString = format.format(result);
System.out.println("Ergebnis der Ausführung: " + resultString);
this.result= resultString;
}
public String getResult() {
return result;
}
}