Hi, habe hier folgenden code geschrieben:
von Zeile 199-211 muss irgendwas nicht passen, weil wenn ich das gui starte und im Preisfeld den Preis eingebe dann kommt immer eine NumberFormatException und die Fehlermeldung: bitte als int eingeben obwohl ich alles richtig eingebe kann mir jmd helfen?
von Zeile 199-211 muss irgendwas nicht passen, weil wenn ich das gui starte und im Preisfeld den Preis eingebe dann kommt immer eine NumberFormatException und die Fehlermeldung: bitte als int eingeben obwohl ich alles richtig eingebe kann mir jmd helfen?
Java:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class BibliothekGui extends JFrame {
private JLabel titelLabel = new JLabel("Titel: ");
private JTextField titelFeld = new JTextField();
private JLabel isbnLabel = new JLabel("ISBN: ");
private JTextField isbnFeld = new JTextField();
private JLabel preisLabel = new JLabel("Preis: ");
private JTextField preisFeld = new JTextField();
private JButton hinzufuegenButton = new JButton("Hinzufuegen");
private JButton entfernenButton = new JButton("Entfernen");
private JTextArea ausgabe = new JTextArea();
final JScrollPane scroll = new JScrollPane(ausgabe);
private JPanel eingabeBereich = new JPanel();
private BibliothekSet bibliothek;
public BibliothekGui() throws Exception {
bibliothek = new BibliothekSet("Bibliothek");
setLayout(new GridLayout(2, 1));
setTitle("Bibliothek");
setSize(350, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
eingabeBereich.setLayout(new GridLayout(4, 2));
eingabeBereich.add(titelLabel);
eingabeBereich.add(titelFeld);
eingabeBereich.add(isbnLabel);
eingabeBereich.add(isbnFeld);
eingabeBereich.add(preisLabel);
eingabeBereich.add(preisFeld);
eingabeBereich.add(hinzufuegenButton);
eingabeBereich.add(entfernenButton);
add(eingabeBereich);
hinzufuegenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputTitel = titelFeld.getText();
titelFeld.setText("");
if (inputTitel == null) {
try {
throw new Exception("Titel darf nicht null sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
if (inputTitel.isEmpty()) {
try {
throw new Exception("Titel darf nicht leer sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
String inputIsbn = isbnFeld.getText();
isbnFeld.setText("");
if (inputIsbn == null) {
try {
throw new Exception("ISBN darf nicht null sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
if (inputIsbn.isEmpty()) {
try {
throw new Exception("ISBN darf nicht leer sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
String inputPreis = preisFeld.getText();
preisFeld.setText("");
if (inputPreis == null) {
try {
throw new Exception("Preis darf nicht null sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
if (inputPreis.isEmpty()) {
try {
throw new Exception("Preis darf nicht leer sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
int i = Integer.parseInt(inputPreis);
if (i <= 0) {
try {
throw new Exception(
"Preis darf nicht kleiner oder gleich 0 sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
;
try {
Buch b = new Buch(titelFeld.getText(), isbnFeld.getText(),
Integer.parseInt(preisFeld.getText()));
bibliothek.hinzufuegen(b);
ausgabe.setText("" + b);
} catch (NumberFormatException e1) {
JOptionPane
.showMessageDialog(BibliothekGui.this,
"Fehler beim Umwandeln des Preises. Bitte als int eingeben.");
} catch (Exception e1) {
JOptionPane.showMessageDialog(BibliothekGui.this, e1
.getMessage());
}
}
});
entfernenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputTitel = titelFeld.getText();
titelFeld.setText("");
if (inputTitel == null) {
try {
throw new Exception("Titel darf nicht null sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
if (inputTitel.isEmpty()) {
try {
throw new Exception("Titel darf nicht leer sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
String inputIsbn = isbnFeld.getText();
isbnFeld.setText("");
if (inputIsbn == null) {
try {
throw new Exception("ISBN darf nicht null sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
if (inputIsbn.isEmpty()) {
try {
throw new Exception("ISBN darf nicht leer sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
String inputPreis = preisFeld.getText();
preisFeld.setText("");
if (inputPreis == null) {
try {
throw new Exception("Preis darf nicht null sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
if (inputPreis.isEmpty()) {
try {
throw new Exception("Preis darf nicht leer sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
int i = Integer.parseInt(inputPreis);
if (i <= 0) {
try {
throw new Exception(
"Preis darf nicht kleiner oder gleich 0 sein");
} catch (Exception ec) {
ausgabe.setText(ec.getMessage());
}
}
try {
Buch b = new Buch(titelFeld.getText(), isbnFeld.getText(),
Integer.parseInt(preisFeld.getText()));
bibliothek.entfernen(b);
ausgabe.setText("" + bibliothek);
} catch (NumberFormatException e1) {
JOptionPane
.showMessageDialog(BibliothekGui.this,
"Fehler beim Umwandeln des Preises. Bitte als double eingeben.");
} catch (Exception e1) {
JOptionPane.showMessageDialog(BibliothekGui.this, e1
.getMessage());
}
// Buch b = new Buch(titelFeld.getText(), isbnFeld.getText(),
// Double.parseDouble(preisFeld.getText()));
// bibliothek.entfernen(b);
//
// ausgabe.setText("" + bibliothek);
}
});
add(scroll);
}
public static void main(String[] args) {
try {
BibliothekGui frame = new BibliothekGui();
frame.setVisible(true);
} catch (Exception e) {
System.out
.println("Fehler beim Starten des Programmes, breche ab.");
}
}
}