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

📄 textfieldtest.java

📁 GUI代码,用与实现相关GUI的功能,如有需要即可自由下载!
💻 JAVA
字号:
//JTextField和JPasswordField
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextFieldTest extends Frame
{
	private JTextField textField1,textField2,textField3;
	private JPasswordField passwordField1;
	
	//set up GUI
	public TextFieldTest
	{
			super("Testing JTextField and JPasswordField");
			
			Container cn = getContentPane();
			cn.setLayout(new FlowLayout());
			
			//construct textfield with default sizing 
			textField1 = new JTextField(10);
			cn.add(textField1);
			
			//construct textfield with default text
			textField2 = new JTextField("Enter text here");
			cn.add(textField2);
			
			//construct textfield with default text and
			//20 visible elements and no event handler
			textField3 = new JTextField("Uneditable text field",20);
			textField3.setEditable(false);
			cn.add(textField3);
			
			//construct password
			passwordField1 = new JPasswordField("Hidden text");
			cn.add(passwordField1);
			
			//register event handler
			TextFieldHandler handler = new TextFieldHandler();
			textField1.addActionListener(handler);	
			textField2.addActionListener(handler);		
			textField3.addActionListener(handler);
			passwordField1.addActionListener(handler);
			
			setSize(400,100);
			setVisible(true);
		}
		
		public static void main(String []args)
		{
				TextFieldTest app = new TextFieldTest();
				app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		private class TextFieldHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
					String s = "";
					
					//user pressed Enter in JTextField textField
					if(e.getSource() == textField1)
					  s = "textField1: "+e.getActionCommand();
					//user pressed Enter in JTextField textField2
					else if(e.getSource() == textField2)
					  s = "textField2: "+e.getActionCommand();
					  //user pressed Enter in JTextField textField3
					else if(e.getSource() == textField3)
					  s = "textField2: "+e.getActionCommand();
					  //user pressed Enter in JPasswordField passwordField1
					else if(e.getSource() == passwordField1)
					  {
					  	JPasswordField pwd =(JPasswordField)e.getSource();
					  	s = "passwordfiled: "+new String(passwordField1.getPassword());
					  	}
					  JOptionPane.showMessageDialog(null,s);
				}
			}
}

⌨️ 快捷键说明

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