Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class FormattedTextField {
public static void main(String[] args) {
new FormattedTextField();
}
public FormattedTextField() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
JTextField field = new JTextField(20);
field.setDocument(new PlainDocument() {
private static final long serialVersionUID = -324142616871349734L;
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
for (char c : str.toCharArray()) {
if (Character.isDigit(c)) {
return;
}
}
super.insertString(offs, str, a);
}
});
frame.add(field);
frame.pack();
frame.setVisible(true);
}
}
if(ae.getSource() == this.button4) {
double d1 = Double.parseDouble(a);
System.out.println(d1);
field.setDocument(new PlainDocument() {
private static final long serialVersionUID = -324142616871349734L;
field.setText("");
}
public void actionPerformed (ActionEvent ae){
String a = field.getText();
String b = field.getText();
if(ae.getSource() == this.button1){
field.setText(field.getText() + "1");
}
if(ae.getSource() == this.button2) {
field.setText(field.getText() + "2");
}
if(ae.getSource() == this.button3) {
field.setText(field.getText() + "3");
}
if(ae.getSource() == this.button4) {
double d1 = Double.parseDouble(a);
System.out.println(d1);
field.setText("");
double d2 = Double.parseDouble(b);
System.out.println(d2);
}