102 lines
2.6 KiB
Java
102 lines
2.6 KiB
Java
|
package velamentum;
|
||
|
|
||
|
import javax.swing.BoxLayout;
|
||
|
import javax.swing.JFrame;
|
||
|
import javax.swing.JLabel;
|
||
|
import javax.swing.JOptionPane;
|
||
|
import javax.swing.JPanel;
|
||
|
import javax.swing.JTextField;
|
||
|
import javax.swing.text.AbstractDocument;
|
||
|
import javax.swing.text.AttributeSet;
|
||
|
import javax.swing.text.BadLocationException;
|
||
|
import javax.swing.text.DocumentFilter;
|
||
|
|
||
|
|
||
|
public class EingabeDialog extends Dialog{
|
||
|
|
||
|
private boolean nurZahlen = false;
|
||
|
private boolean wertAnzeigen = false;
|
||
|
private String ergebnis;
|
||
|
|
||
|
public EingabeDialog(String pTitel, String pNachricht) {
|
||
|
super(pTitel, pNachricht);
|
||
|
// TODO Auto-generated constructor stub
|
||
|
}
|
||
|
|
||
|
public EingabeDialog(String pTitel, String pNachricht, boolean pNurZahlen) {
|
||
|
super(pTitel, pNachricht);
|
||
|
this.nurZahlen = pNurZahlen;
|
||
|
}
|
||
|
|
||
|
public String nenneErgebnis() {
|
||
|
return this.ergebnis;
|
||
|
}
|
||
|
|
||
|
public int nenneErgebnisAlsZahl() {
|
||
|
if(this.nenneErgebnis()==null) {
|
||
|
return 0;
|
||
|
}
|
||
|
try {
|
||
|
return Integer.parseInt(this.ergebnis);
|
||
|
} catch (Exception e) {
|
||
|
System.err.println("Error: Ergebnis ist keine ganze Zahl");
|
||
|
//return -35505;
|
||
|
throw e;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public boolean nenneNurZahlen() {
|
||
|
return this.nurZahlen;
|
||
|
}
|
||
|
|
||
|
public boolean nenneWertAnzeigen() {
|
||
|
return this.wertAnzeigen;
|
||
|
}
|
||
|
|
||
|
public void setzeErgebnis(String pErgebnis) {
|
||
|
this.ergebnis = pErgebnis;
|
||
|
}
|
||
|
|
||
|
public void setzeNurZahlen(boolean pNurZahlen) {
|
||
|
this.nurZahlen = pNurZahlen;
|
||
|
}
|
||
|
|
||
|
public void setzeWertAnzeigen(boolean pWertAnzeigen) {
|
||
|
this.nurZahlen = pWertAnzeigen;
|
||
|
}
|
||
|
|
||
|
public void zeige() {
|
||
|
JFrame desk = new JFrame();
|
||
|
JPanel panel = new JPanel();
|
||
|
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
|
||
|
JLabel label = new JLabel(this.nenneNachricht());
|
||
|
JTextField tf = new JTextField();
|
||
|
if(this.nurZahlen) {
|
||
|
((AbstractDocument) tf.getDocument()).setDocumentFilter(new DocumentFilter() {
|
||
|
@Override
|
||
|
public void replace(FilterBypass fb, int offset, int length, String str, AttributeSet attr)
|
||
|
throws BadLocationException {
|
||
|
if (!str.matches("[0-9]")) {
|
||
|
super.replace(fb, offset, fb.getDocument().getLength(), "", attr);
|
||
|
return;
|
||
|
} else {
|
||
|
super.replace(fb, offset, length, str, attr);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
panel.add(label);
|
||
|
panel.add(tf);
|
||
|
|
||
|
int n = JOptionPane.showConfirmDialog(desk, panel, this.nenneTitel(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
|
||
|
if(n == JOptionPane.OK_OPTION) {
|
||
|
this.ergebnis = tf.getText();
|
||
|
}
|
||
|
else {
|
||
|
System.exit(0);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|