📄 jtextfielddemo.java
字号:
//JTextFieldDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextFieldDemo extends JFrame implements ActionListener{
private Container content;
private JLabel juserlabel;
private JLabel jpasswordlabel;
private JTextField jusertext;
private JPasswordField jpasswordtext;
public JTextFieldDemo() {
super("文本字段与点击事件");
content=getContentPane();
content.setLayout(null);
juserlabel=new JLabel("name:");
juserlabel.setBounds(5,5,80,20);
jusertext=new JTextField(25);
jusertext.setBounds(90,5,100,20);
jpasswordlabel=new JLabel("password:");
jpasswordlabel.setBounds(5,50,80,20);
jpasswordtext=new JPasswordField("");
jpasswordtext.setBounds(90,50,100,20);
content.add(juserlabel);
content.add(jusertext);
content.add(jpasswordlabel);
content.add(jpasswordtext);
jusertext.addActionListener(this);
jpasswordtext.addActionListener(this);
setSize(300,120);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String password=new String(jpasswordtext.getPassword());
if(jusertext.getText().equals("user")&&password.equals("123"))
JOptionPane.showMessageDialog(null,"welcome!");
else JOptionPane.showMessageDialog(null,"user name or password error!");
}
public static void main(String[] args){
JTextFieldDemo application=new JTextFieldDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -