输入结束后,回车就出结果了
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test {
private JFrame f = new JFrame("三位数拆分");
private JLabel numLabel = new JLabel("三位数");
private JLabel hundredLabel = new JLabel("百位");
private JLabel tenthLabel = new JLabel("十位");
private JLabel indLabel = new JLabel("个位");
private JTextField hund = new JTextField();
private JTextField ten = new JTextField();
private JTextField indiv = new JTextField();
private JTextField number = new JTextField();
public Test(){
f.setLayout(new GridLayout(4, 2));
f.add(numLabel);
f.add(number);
f.add(hundredLabel);
f.add(hund);
f.add(tenthLabel);
f.add(ten);
f.add(indLabel);
f.add(indiv);
hund.setEditable(false);
ten.setEditable(false);
indiv.setEditable(false);
number.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(!number.getText().matches("\\d{3}")){
JOptionPane.showMessageDialog(null, "Error! It must be 3 digits only!");
number.setText("");
}else{
hund.setText(number.getText().substring(0, 1));
ten.setText(number.getText().substring(1, 2));
indiv.setText(number.getText().substring(2, 3));
}
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
}
public static void main(String args[]) {
new Test();
}
}
温馨提示:内容为网友见解,仅供参考