有没有试试 BigInteger 或是 BigDecimal 呢?!
很好的一个类啊
要充分利用!
若不写界面的话10行左右的代码就可以了吧
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.math.BigInteger;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class Bigint extends JFrame{
private Container container;
private JTextField num1,num2,sum;
private JPanel panel,panel1;
private JButton add;
private BigInteger big1,big2,big3;
public Bigint(){
container = getContentPane();
num1 = new JTextField(100);
num1.setDocument(new NumOnly());
num2 = new JTextField(100);
num2.setDocument(new NumOnly());
sum = new JTextField(101);
sum.setEditable(false);
panel = new JPanel();
panel.setLayout(new GridLayout(3,1,5,5));
panel.add(num1);
panel.add(num2);
panel.add(sum);
container.add(new JScrollPane(panel));
panel1 = new JPanel(new FlowLayout());
add = new JButton("计算");
add.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
if(num1.getText().equals("")||num2.getText().equals("")){
JOptionPane.showMessageDialog(container,"您没有输入完要计算的内容","提示",JOptionPane.INFORMATION_MESSAGE);
}
else{
big1 = new BigInteger(num1.getText());
big2 = new BigInteger(num2.getText());
big3 = big1.add(big2);
sum.setText(big3.toString());
}
}
}
);
panel1.add(add);
container.add(panel1,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
Dimension frameSize = getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
setVisible(true);
}
public static void main(String args[]){
Bigint big = new Bigint();
}
public class NumOnly 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-27 00:48 编辑 [/it]]