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

📄 ch10ex7.java

📁 JAVA程序设计 丁岳伟 彭敦陆编 高等教育出版社 第7---11章程序
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Frame6 extends JFrame{
	JTextField t1=new JTextField(8);
	JTextField t2=new JTextField(8);
	Container cp;
	public Frame6(){
		super();
		setTitle("Text Field Verify");
		cp=getContentPane();
		cp.setLayout(new FlowLayout());
		cp.add(t1);
		cp.add(t2);
		t1.requestFocus();
		t1.addActionListener(new ActionListenerT1());
		t2.addActionListener(new ActionListenerT2());
		setSize(300,150);
		setLocation(50,50);
		addWindowListener(new WindowDestroyer());
	}
	class ActionListenerT1 implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(validInteger(t1))
				t1.transferFocus();
			else{
				t1.requestFocus();
				t1.selectAll();
			}
		}
	}
	class ActionListenerT2 implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(validInteger(t2))
				t2.transferFocus();
			else{
				t2.requestFocus();
				t2.selectAll();
			}
		}
	}
	private boolean validInteger(JTextField in){
		try{
			Integer.parseInt(in.getText());
			return true;
		}catch(NumberFormatException e){
			return false;
		}
	}
}
public class ch10ex7{
	public static void main(String args[]){
		Frame6 win=new Frame6();
		win.setVisible(true);
	}
}
class WindowDestroyer extends WindowAdapter{
	public void windowClosing(WindowEvent e){
		System.exit(0);
	}
}

⌨️ 快捷键说明

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