public class NumAna extends JFrame{
private Container container;
private JTextField input;
private JTextArea output;
private JLabel inputLabel;
private JButton button,closeButton;
public NumAna(){
super("数字判断");
container = getContentPane();
input = new JTextField("",15);
inputLabel = new JLabel("请输入数字");
input.setDocument(new customDoc());
output = new JTextArea(20,30);
output.setEditable(false);
button = new JButton("分析");
button.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
String num = input.getText();
if(num.length()==0)
output.setText("您没有输入数字。");
else{
output.append("您输入的是:"+input.getText()+",是"+num.length()+"位数.\n");
for(int i=0;i<num.length();i++)
output.append("第"+(i+1)+"位数是:"+num.charAt(i)+"\n");
output.append("\n");
StringBuffer buf = new StringBuffer(num);
buf.reverse();
output.append("逆序后为"+buf.toString()+"\n");
input.setText("");
for(int i = 0;i<buf.toString().length();i++)
output.append("第"+(i+1)+"位数是:"+buf.toString().charAt(i)+"\n");
}
input.grabFocus();
}
}
);
closeButton = new JButton("关闭");
closeButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
}
);
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(inputLabel);
panel1.add(input);
container.add(new JScrollPane(output));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(closeButton);
panel2.add(button);
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.SOUTH);