📄 textfieldtest.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextFieldTest extends JFrame {
private JLabel label1,label2;
private JTextField textField1,textField2;
private JPasswordField passwordField;
private JTextArea textArea;
public TextFieldTest(){
super( "Testing JTextField and JPasswordField" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
label1=new JLabel("Enter text here" );
container.add(label1);
textField1 = new JTextField( 10 );
container.add( textField1 );
label1=new JLabel("Enter password here" );
container.add(label1);
passwordField = new JPasswordField(10);
container.add( passwordField );
textField2 = new JTextField( "Uneditable text field", 20 );
textField2.setEditable( false );
container.add( textField2 );
textArea=new JTextArea("This is a textarea",5,20);
container.add(textArea);
TextFieldHandler handler = new TextFieldHandler();
textField1.addActionListener( handler );
textField2.addActionListener( handler );
passwordField.addActionListener( handler );
setSize( 325, 230 );
setVisible( true );
}
public static void main( String args[] ){
TextFieldTest application = new TextFieldTest();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
private class TextFieldHandler implements ActionListener {
public void actionPerformed( ActionEvent event ){
String string = "";
if ( event.getSource() == textField1 )
string = "textField1: " + event.getActionCommand();
else if ( event.getSource() == textField2 )
string = "textField2: " + event.getActionCommand();
else if ( event.getSource() == passwordField ) {
JPasswordField pwd =( JPasswordField ) event.getSource();
string = "passwordField: " +new String( passwordField.getPassword() );
}
JOptionPane.showMessageDialog( null, string );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -