Guten Tag,
Bitte ich brauch Hilfe wegen "String Eingabe erzwingen", ich hab überall gesucht und viel gefunden aber ich komm trotzdem damit nicht klar .. ich komme einfach auf den Trick nicht .. ich glaube dass es so einfach ist, aber ich komme einfach nicht drauf.
ich hab zwei JTextField Double Variable und JTextField ein String, ich probiere die ganze zeit dass wenn man auf den Calculate Button drückt und noch nicht die ganze JTextField eingaben geben hat dass dann einer Fehlermeldung raus komm , wie gesagt mit der Double Variable geht es super aber aber mit String geht es nicht.
Vielen Danke für eure Hilfe !!
Bitte ich brauch Hilfe wegen "String Eingabe erzwingen", ich hab überall gesucht und viel gefunden aber ich komm trotzdem damit nicht klar .. ich komme einfach auf den Trick nicht .. ich glaube dass es so einfach ist, aber ich komme einfach nicht drauf.
ich hab zwei JTextField Double Variable und JTextField ein String, ich probiere die ganze zeit dass wenn man auf den Calculate Button drückt und noch nicht die ganze JTextField eingaben geben hat dass dann einer Fehlermeldung raus komm , wie gesagt mit der Double Variable geht es super aber aber mit String geht es nicht.
Vielen Danke für eure Hilfe !!
Java:
package de.sbsapp.examples.geometriccalculator.view;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import de.sbsapp.examples.geometriccalculator.Document.DoubleCheckDocument;
import de.sbsapp.examples.geometriccalculator.model.Geometric;
import de.sbsapp.examples.geometriccalculator.model.Square;
public class InputSquare extends JDialog {
private static final long serialVersionUID = 1L;
private final JPanel contentPanel = new JPanel();
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JPanel pnlButton;;
private Geometric a;
public InputSquare(final Geometric a) throws ParseException {
this.a = a;
{
setBounds(100, 100, 450, 300);
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
textField1 = new JTextField();
textField1.setEditable(true);
textField1.setText("");
textField1.setBounds(153, 52, 86, 20);
contentPanel.add(textField1);
textField1.setColumns(13);
JLabel lblInputN = new JLabel("Name:");
setBounds(150, 150, 450, 300);
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
textField2 = new JTextField();
textField2.setDocument(new DoubleCheckDocument(textField2));
textField2.setText("12.10");
textField2.setEditable(true);
textField2.setText("");
textField2.setBounds(153, 82, 86, 20);
contentPanel.add(textField2);
textField2.setColumns(13);
JLabel lblInputA = new JLabel("Side A:");
setBounds(100, 200, 450, 300);
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
textField3 = new JTextField();
textField3.setDocument(new DoubleCheckDocument(textField3));
textField3.setText("12.10");
textField3.setEditable(true);
textField3.setText("");
textField3.setBounds(153, 112, 86, 20);
contentPanel.add(textField3);
textField3.setColumns(13);
JLabel lblInputB = new JLabel("Side B:");
if (a instanceof Square) {
lblInputN.setText("Name:");
lblInputA.setText("Side A:");
lblInputB.setText("Side B:");
}
lblInputN.setBounds(97, 52, 52, 14);
contentPanel.add(lblInputN);
lblInputA.setBounds(97, 82, 52, 14);
contentPanel.add(lblInputA);
lblInputB.setBounds(97, 112, 52, 14);
contentPanel.add(lblInputB);
}
{
pnlButton = new JPanel(new GridLayout(1, 2));
pnlButton.setLayout(new FlowLayout(FlowLayout.CENTER));
getContentPane().add(pnlButton, BorderLayout.SOUTH);
JButton btnButtonCalculate = new JButton("Calculate");
JButton btnButtonBack = new JButton("Back");
pnlButton.add(btnButtonCalculate);
pnlButton.add(btnButtonBack);
btnButtonCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
double nosideA, nosideB;
String textField1 = null;
nosideA = Double.parseDouble(textField2.getText());
nosideB = Double.parseDouble(textField3.getText());
if ((nosideA < 0) && (nosideA < 1000) && (nosideB < 0)
&& (nosideB < 1000)) {
computeAndClose();
}
else if (equals(textField1) == false) {
computeAndClose();
}
} catch (Exception g) {
JOptionPane.showMessageDialog(contentPanel,
"Eggs are not supposed to be green.",
"Inane warning", JOptionPane.WARNING_MESSAGE);
}
}
});
btnButtonBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
});
}
}
private void computeAndClose() {
((Square) a).setName(String.valueOf(textField1.getText()));
this.setVisible(false);
this.dispose();
((Square) a).setX(Double.parseDouble(textField2.getText()));
this.setVisible(false);
this.dispose();
((Square) a).setY(Double.parseDouble(textField3.getText()));
this.setVisible(false);
this.dispose();
}
}