⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 numbertest.java

📁 最简单的非整形数字分割程序。写的不是很好了
💻 JAVA
字号:
/**
 * @(#)NumberTest.java
 *
 *
 * @author 
 * @version 1.00 2008/7/4
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class NumberTest implements ActionListener {
	private TextField tf1 ;
	private TextField tf2 ;
	private TextField tf3 ;
	
	private Label lb1;
	private Label lb2;
	private Label lb3;
	private Label lb4;
	
	private JButton bt1;
	private JButton bt2;
	private Panel p1;
	private Panel p2;
	final Frame f= new Frame("数字分割");;
    public NumberTest() {
    }
    public void init(){
  	
    	f.setLocation(400,200);   
    	f.setBackground(Color.pink);
    	p1 = new Panel();
    	p2 = new Panel();
    	lb1 = new Label("请输入一个数字:");
    	lb2 = new Label("整数部分是:");
    	lb3 = new Label("小数部分是:");
    	lb4 = new Label();
    	tf1 = new TextField();
    	tf2 = new TextField();
    	tf3 = new TextField();
    	bt1 = new JButton("确定");
    	bt2 = new JButton("取消");
    	
    	p1.setLayout(new GridLayout(3,2,2,2));
    	p2.setLayout(new FlowLayout(15,35,10));
    	
    	p1.add(lb1);
    	p1.add(tf1);
    	p1.add(lb2);
    	p1.add(tf2);
    	p1.add(lb3);
    	p1.add(tf3);
    	
    	p2.add(bt1);
    	p2.add(bt2);
    	
    	bt1.addActionListener(this);
    	bt2.addActionListener(this);
    	tf1.addActionListener(this);
    	
    	f.add(p1,BorderLayout.NORTH);
    	f.add(p2,BorderLayout.CENTER);
    	f.add(lb4,BorderLayout.SOUTH);
    	
    	f.pack();
    	f.setResizable(false);
    	f.setVisible(true);
    	f.addWindowListener(new WindowAdapter(){
    		public void windowClosing(WindowEvent e){
    			f.dispose();
    		}
    	});	
    }
    
    public void actionPerformed(ActionEvent e){
    	if(lb4.getText().length() != 0){
    		lb4.setText("");
    	}	
    	if(e.getSource() == bt2){
    		f.dispose();
    	}
    	else{
   			if(tf1.getText().length()==0){
   				lb4.setText("请先输入一个数字!");
   				if((tf2.getText().length() != 0)||(tf3.getText().length() != 0)){
   					tf2.setText("");
   					tf3.setText("");
   				}
   			}else{
   				checkString();
   			}
    	}
    }
    
    public void checkString(){
    	String str = tf1.getText();
    	boolean b = true;
    	int n = 0;//字符串的长度统计.
    	int m = 0;//用来统计'.'的个数.
    	for(int i = 0 ; i < str.length() ; i++){
    		
    		if(str.charAt(i) >= '0' & str.charAt(i) <= '9'){
    			n++;
    			continue;
    		}else if(str.charAt(i) == '.'){
    			m++;
    			n++;
    			if(m > 1){
    				lb4.setText("输入数字格式有错误!");	
    				b= false;
    			}
    			continue;
    		}
    		else {
    			lb4.setText("你输入的不是数字,请重新输入!");
    			tf1.setText("");
    			tf2.setText("");
    			tf3.setText("");
    			b = false;
    			break;
    		}	
    	}
    	if(tf1.getText().length() != 0){
    		int i = str.indexOf('.');
    		if( b == true && m == 1){
    			tf2.setText(str.substring(0,i));
				tf3.setText(str.substring((i+1),str.length()));
    		}else if(b == true && m == 0){
    			tf2.setText(str.substring(0,str.length()));
    			tf3.setText("0");
    		}
    	}
    }

    public static void main(String[] args){
    	NumberTest nt = new NumberTest();
    	nt.init();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -