import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import
javax.swing.text.AttributeSet;
import
javax.swing.text.BadLocationException;
import
javax.swing.text.PlainDocument;
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);
pack();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setLocation((screenWidth - 800) / 2, (screenHeight - 110) / 2);
setVisible(true);
setResizable(false);
}
public static void main(String args[]){
NumAna num = new NumAna();
num.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class
customDoc
extends
PlainDocument{
public
void
insertString(int
offs,
String
str,
AttributeSet
a)
throws
BadLocationException
{
for(int i=0;i<str.length();i++){
if(str.charAt(i)<'0'||str.charAt(i)>'9'){
return;
}
}
super.insertString(offs,str,a);
}
}
[[it] 本帖最后由 freish 于 2008-4-3 09:03 编辑 [/it]]
收到的鲜花