1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| package test;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Main implements java.awt.event.ActionListener {
static int m = 0;
static int n = 0;
public static void main(String[] args) {
JFrame window = new JFrame();
JPanel pan = new JPanel();
JTextField a = new JTextField();
JTextField b = new JTextField();
JTextArea c = new JTextArea();
Font police = new Font("Serif", Font.PLAIN, 20);
a.setFont(police);
b.setFont(police);
c.setFont(police);
a.setBounds(100, 100, 100, 25);
b.setBounds(300, 100, 100, 25);
c.setBounds(200, 200, 100, 25);
a.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent arg0) {
}
public void insertUpdate(DocumentEvent arg0) {
try {
c.setText(Integer.toString(Integer.parseInt(a.getText())));
} catch(NumberFormatException e) {
c.setText("ERROR");
}
}
public void removeUpdate(DocumentEvent arg0) {
if(!a.getText().isEmpty()) {
try {
c.setText(Integer.toString(Integer.parseInt(a.getText())));
} catch(NumberFormatException e) {
c.setText("ERROR");
}
}
else {
c.setText("");
}
}
});
b.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent arg0) {
}
public void insertUpdate(DocumentEvent arg0) {
try {
c.setText(Integer.toString(Integer.parseInt(b.getText())));
} catch(NumberFormatException e) {
c.setText("ERROR");
}
}
public void removeUpdate(DocumentEvent arg0) {
if(!b.getText().isEmpty()) {
try {
c.setText(Integer.toString(Integer.parseInt(b.getText())));
} catch(NumberFormatException e) {
c.setText("ERROR");
}
}
else {
c.setText("");
}
}
});
pan.setLayout(null);
pan.add(a);
pan.add(b);
pan.add(c);
window.setContentPane(pan);
window.setSize(600, 400);
window.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
} |
Partager