added Aufgabenblatt 13

This commit is contained in:
Sven Vogel 2023-06-07 19:42:19 +02:00
parent d70c95bea9
commit a030d09a07
15 changed files with 677 additions and 0 deletions

View File

@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="formdev.flatlaf" type="repository">
<properties maven-id="com.formdev:flatlaf:2.3" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/formdev/flatlaf/2.3/flatlaf-2.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -4,6 +4,7 @@
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt10/AufgabenBlatt10.iml" filepath="$PROJECT_DIR$/AufgabenBlatt10/AufgabenBlatt10.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt10/AufgabenBlatt10.iml" filepath="$PROJECT_DIR$/AufgabenBlatt10/AufgabenBlatt10.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt11/AufgabenBlatt11.iml" filepath="$PROJECT_DIR$/AufgabenBlatt11/AufgabenBlatt11.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt11/AufgabenBlatt11.iml" filepath="$PROJECT_DIR$/AufgabenBlatt11/AufgabenBlatt11.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt13/AufgabenBlatt13.iml" filepath="$PROJECT_DIR$/AufgabenBlatt13/AufgabenBlatt13.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" filepath="$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" filepath="$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" filepath="$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" filepath="$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" filepath="$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" filepath="$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" />

View File

@ -24,3 +24,5 @@ public class Taube implements Fliegen {
System.out.println("GuuurrrGuurrr"); System.out.println("GuuurrrGuurrr");
} }
} }

29
AufgabenBlatt13/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="openjdk-19" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="formdev.flatlaf" level="project" />
</component>
</module>

View File

@ -0,0 +1,12 @@
Titel,Autor,Verlag,Preis,Verfügbar,
Atlas - Die Geschichte von Pa Salt,Lucinda Riley | Harry Whittaker,Goldmann,24,ja,
Das Cafe ohne Namen,Robert Seethaler,Claassen,24,ja,
Melody,Suter, Martin,26,ja
Blue Skies,T.C. Boyle,Hanser,28,nein,
Dark Sigils - Wie die Dunkelheit befiehlt,Anna Benning,Fischer,20,ja,
Eine Frage der Chemie,Bonnie Garmus,Piper,24,ja,
Der letzte Sessellift,John Irving,Diogenes,26,ja,
Liebe oder Eierlikör - Fast eine Romanze,Heldt, Dora,15,ja
Die Unverbesserlichen - Die Revanche des Monsieur Lipaire,Volker Klöpfel | Michael Kobr,Ullstein,20,nein,
Pompeji oder Die fünf Reden des Jowna,Ruge, Eugen,25,nein
Die Liebe an miesen Tagen,Ewald Arenz,Dumont,28,nein,
1 Titel,Autor,Verlag,Preis,Verfügbar,
2 Atlas - Die Geschichte von Pa Salt,Lucinda Riley | Harry Whittaker,Goldmann,24,ja,
3 Das Cafe ohne Namen,Robert Seethaler,Claassen,24,ja,
4 Melody,Suter, Martin,26,ja
5 Blue Skies,T.C. Boyle,Hanser,28,nein,
6 Dark Sigils - Wie die Dunkelheit befiehlt,Anna Benning,Fischer,20,ja,
7 Eine Frage der Chemie,Bonnie Garmus,Piper,24,ja,
8 Der letzte Sessellift,John Irving,Diogenes,26,ja,
9 Liebe oder Eierlikör - Fast eine Romanze,Heldt, Dora,15,ja
10 Die Unverbesserlichen - Die Revanche des Monsieur Lipaire,Volker Klöpfel | Michael Kobr,Ullstein,20,nein,
11 Pompeji oder Die fünf Reden des Jowna,Ruge, Eugen,25,nein
12 Die Liebe an miesen Tagen,Ewald Arenz,Dumont,28,nein,

View File

@ -0,0 +1,15 @@
package aufgabe1;
public record Book(String title, String author, String verlag, double value, boolean available) {
public static Book parseFromCommaSeparatedString(String line) {
var values = line.split(",");
try {
return new Book(values[0], values[1], values[2], Double.parseDouble(values[3]), Boolean.parseBoolean(values[4]));
} catch (ArrayIndexOutOfBoundsException ignored) {
throw new IllegalArgumentException("not enough entries for book");
} catch (NumberFormatException ignored) {
throw new IllegalArgumentException("value of book not a number");
}
}
}

View File

@ -0,0 +1,36 @@
package aufgabe1;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
public record BookDatabase(List<Book> books) {
public static BookDatabase readFromFile(File file) {
if (!file.exists() || !file.canRead() || file.isDirectory())
throw new IllegalArgumentException("Cannot read from file");
try {
var source = Files.readString(Paths.get(file.getPath()), StandardCharsets.UTF_8);
var books = source.lines().skip(1).map(Book::parseFromCommaSeparatedString).toList();
return new BookDatabase(books);
} catch (ArrayIndexOutOfBoundsException | IOException e) {
throw new IllegalStateException("Cannot read from file: " + e);
}
}
public Optional<Book> getBook(String title) {
return books.stream().filter(book -> book.title().equals(title)).findFirst();
}
public List<String> getTitles() {
return books.stream().map(Book::title).toList();
}
}

View File

@ -0,0 +1,160 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="aufgabe1.BookDatabaseViewer">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="436" height="301"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<grid row="0" 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="OK"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" 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="Cancel"/>
</properties>
</component>
</children>
</grid>
<component id="8a631" class="javax.swing.JButton" binding="verfuegbarkeitPruefenButton" default-binding="true">
<constraints>
<grid row="0" 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="Verfügbarkeit prüfen"/>
</properties>
</component>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="6" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="8" left="8" bottom="8" right="8"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="line" title="Selected Book"/>
<children>
<component id="90c7" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" 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="Buchtitel"/>
</properties>
</component>
<hspacer id="12140">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="89251">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="6cca" class="javax.swing.JComboBox" binding="comboBoxTitle">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="47df3" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" 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="Titel"/>
</properties>
</component>
<component id="84e92" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" 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="Autor"/>
</properties>
</component>
<component id="d489b" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" 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="Verlag"/>
</properties>
</component>
<component id="632a2" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="0" 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="Preis"/>
</properties>
</component>
<component id="48c66" class="javax.swing.JTextField" binding="textFieldTitle">
<constraints>
<grid row="1" column="1" 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="59868" class="javax.swing.JTextField" binding="textFieldAuthor">
<constraints>
<grid row="2" column="1" 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="d716" class="javax.swing.JTextField" binding="textFieldVerlag">
<constraints>
<grid row="3" column="1" 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="a7f35" class="javax.swing.JTextField" binding="textFieldValue">
<constraints>
<grid row="4" column="1" 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>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,93 @@
package aufgabe1;
import javax.swing.*;
import java.awt.event.*;
import java.util.Optional;
public class BookDatabaseViewer extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JComboBox<String> comboBoxTitle;
private JTextField textFieldTitle;
private JTextField textFieldAuthor;
private JTextField textFieldVerlag;
private JTextField textFieldValue;
private JButton verfuegbarkeitPruefenButton;
private final BookDatabase bookDatabase;
private Optional<Book> currentBook = Optional.empty();
public BookDatabaseViewer(BookDatabase bookDatabase) {
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
setTitle("Book Database");
this.bookDatabase = bookDatabase;
buttonOK.addActionListener(e -> onOK());
buttonCancel.addActionListener(e -> onCancel());
textFieldValue.setEditable(false);
textFieldVerlag.setEditable(false);
textFieldAuthor.setEditable(false);
textFieldTitle.setEditable(false);
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
comboBoxTitle.addActionListener(e -> {
SwingUtilities.invokeLater(this::selectTitle);
});
verfuegbarkeitPruefenButton.addActionListener(this::checkAvailability);
bookDatabase.getTitles().forEach(comboBoxTitle::addItem);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setLocationByPlatform(true);
setVisible(true);
}
private void checkAvailability(ActionEvent actionEvent) {
if (currentBook.isPresent()) {
var book = currentBook.get();
JOptionPane.showMessageDialog(this, "Is the book available? " + (book.available() ? "Yes" : "No"));
}
}
private void selectTitle() {
var title = (String) this.comboBoxTitle.getSelectedItem();
currentBook = bookDatabase.getBook(title);
if (currentBook.isPresent()) {
var book = currentBook.get();
this.textFieldTitle.setText(book.title());
this.textFieldAuthor.setText(book.author());
this.textFieldValue.setText(String.format("%.2f€", book.value()));
this.textFieldVerlag.setText(book.verlag());
}
}
private void onOK() {
// add your code here
dispose();
}
private void onCancel() {
// add your code here if necessary
dispose();
}
}

View File

@ -0,0 +1,30 @@
package aufgabe1;
import com.formdev.flatlaf.FlatDarculaLaf;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Main {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new FlatDarculaLaf());
} catch (UnsupportedLookAndFeelException e) {
throw new RuntimeException(e);
}
SwingUtilities.invokeLater(Main::init);
}
private static void init() {
var chooser = new JFileChooser();
chooser.addChoosableFileFilter(new FileNameExtensionFilter("csv files", "csv"));
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
var file = chooser.getSelectedFile();
new BookDatabaseViewer(BookDatabase.readFromFile(file));
}
}
}

View File

@ -0,0 +1,6 @@
package aufgabe2;
public interface Calculate {
Double calculate(Double x, Double y);
}

View File

@ -0,0 +1,177 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="aufgabe2.Calculator">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="669" height="413"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" 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>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<grid row="0" 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="OK"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" 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="Cancel"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="7" 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>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="d15d">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="5853b" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" 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="Operand 1"/>
</properties>
</component>
<component id="450f2" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" 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="Operand 2"/>
</properties>
</component>
<component id="dd24f" class="javax.swing.JTextField" binding="operandOneTextField">
<constraints>
<grid row="1" column="1" 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="45edc" class="javax.swing.JTextField" binding="operandTwoTextField">
<constraints>
<grid row="2" column="1" 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>
<grid id="f63c2" layout-manager="GridLayoutManager" row-count="1" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5308" class="javax.swing.JButton" binding="addButton">
<constraints>
<grid row="0" 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="+"/>
</properties>
</component>
<hspacer id="72ca6">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="bbf49" class="javax.swing.JButton" binding="subButton">
<constraints>
<grid row="0" column="1" 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="-"/>
</properties>
</component>
<component id="5e669" class="javax.swing.JButton" binding="MulButton">
<constraints>
<grid row="0" column="2" 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="*"/>
</properties>
</component>
<component id="e0654" class="javax.swing.JButton" binding="divButton">
<constraints>
<grid row="0" column="3" 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="/"/>
</properties>
</component>
</children>
</grid>
<component id="d4169" class="javax.swing.JLabel" binding="Result">
<constraints>
<grid row="5" column="0" 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="Result"/>
</properties>
</component>
<component id="722f6" class="javax.swing.JTextField" binding="resultTextField">
<constraints>
<grid row="5" column="1" 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>
<vspacer id="1935d">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="8c125" class="javax.swing.JLabel">
<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="Calculator"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,84 @@
package aufgabe2;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JTextField operandOneTextField;
private JTextField operandTwoTextField;
private JButton addButton;
private JButton subButton;
private JButton MulButton;
private JButton divButton;
private JLabel Result;
private JTextField resultTextField;
public Calculator() {
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
}
});
buttonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
});
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
addButton.addActionListener(e -> calculate(Double::sum));
subButton.addActionListener(e -> calculate((a,b) -> a - b));
MulButton.addActionListener(e -> calculate((a,b) -> a * b));
divButton.addActionListener(e -> calculate((a,b) -> a / b));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setLocationByPlatform(true);
setVisible(true);
}
private void calculate(Calculate operation) {
try {
var op0 = Double.parseDouble(operandOneTextField.getText());
var op1 = Double.parseDouble(operandTwoTextField.getText());
var res = operation.calculate(op0, op1);
resultTextField.setText(res.toString());
} catch (NumberFormatException e) {
JOptionPane.showInternalMessageDialog(null, "Operand numbers must be valid", "Calculation", JOptionPane.ERROR_MESSAGE);
}
}
private void onOK() {
// add your code here
dispose();
}
private void onCancel() {
// add your code here if necessary
dispose();
}
}

View File

@ -0,0 +1,10 @@
package aufgabe2;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(Calculator::new);
}
}