该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
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("TextField & ActionEvent");
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 JPanel("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);
}
}