ITEEDU

swing的版面结构

版面管理器

事件处理

鼠标事件

键盘事件处理

窗口与面板的使用

JInternalFrame的使用(窗口与面板)

JTabbedPane,JScroolPane,JScrolBa的使用

标签与按钮的使用与介绍

[复选框、选项按钮、列表方框、下拉式列表的使用与介绍]

表格(Table)的使用与介绍

JTable的事件处理

1文字输入组件的使用与介绍

树(Tree)的使用与介绍

对话框(Option Pane与Dialog)的使用与介绍

菜单与工具栏的使用与介绍

文件选择对话框,颜色选择对话框,分隔线的使用与介绍.

Slider,Timer,Progress的使用

9-3-2:利用Document构造JPasswordField:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JPasswordField3{

    public static void main(String args[])    {

    	JFrame f = new JFrame("JPasswordField3");
    	Container contentPane = f.getContentPane();
    	contentPane.setLayout(new BorderLayout());

    	JPanel p1 = new JPanel();        
    	//p1.setLayout(new GridLayout(4,2));
    	p1.setLayout(new GridBagLayout());
    	GridBagConstraints gbc = new GridBagConstraints();
    	gbc.anchor = GridBagConstraints.WEST; //设定Layout的位置
    	gbc.insets = new Insets(2,2,2,2); //设定与边界的距离(上,左,下,右)
    	
    	p1.setBorder(BorderFactory.createTitledBorder("您的基本数据"));         
    	JLabel l1 = new JLabel("姓名:");        
    	JLabel l2 = new JLabel("性别:");        
    	JLabel l3 = new JLabel("身高:");        
    	JLabel l4 = new JLabel("体重:");        
    	JPasswordField t1 = new JPasswordField(new JPasswordField3_OnlyNumberDocument(10),"",10);
    	JPasswordField t2 = new JPasswordField(new JPasswordField3_OnlyNumberDocument(1),"",2);
    	JPasswordField t3 = new JPasswordField(new JPasswordField3_OnlyNumberDocument(5),"",5);        
    	JPasswordField t4 = new JPasswordField(new JPasswordField3_OnlyNumberDocument(5),"",5);

    	gbc.gridy=1;
    	gbc.gridx=0;
    	p1.add(l1,gbc);
    	gbc.gridx=1;
    	p1.add(t1,gbc);
    	gbc.gridy=2;
    	gbc.gridx=0;
    	p1.add(l2,gbc);
    	gbc.gridx=1;
    	p1.add(t2,gbc);
    	gbc.gridy=3;
    	gbc.gridx=0;        
   	    p1.add(l3,gbc);
   	    gbc.gridx=1;
    	p1.add(t3,gbc);
    	gbc.gridy=4;
    	gbc.gridx=0;        
    	p1.add(l4,gbc);
    	gbc.gridx=1;
    	p1.add(t4,gbc);        
    	
    	contentPane.add(p1);        
    	f.pack();        
    	f.show();        
    	f.addWindowListener(new WindowAdapter() {            
    		public void windowClosing(WindowEvent e) {                    
    			System.exit(0);            
    		}        
    	});    
    }
}
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Toolkit;

public class JPasswordField3_OnlyNumberDocument extends PlainDocument{

	private int maxLength;
	int result;
	
	public JPasswordField3_OnlyNumberDocument(int maxLength){
		this.maxLength = maxLength;
	}
	
	public void insertString(int offset,String str,AttributeSet att) throws BadLocationException
	{
		for(int i=0;i<=9;i++){
			result = Integer.toString(i).compareTo(str);
			if (result == 0){ //是数字才处理

			  if ( getLength() + str.length() > maxLength ){
		   		Toolkit.getDefaultToolkit().beep();
			  }else{
				super.insertString(offset,str,att);
			  }	
			}
		}
	}
}