这是程序运行之后的效果图:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MyJFrame extends JFrame{
private JFrame jf = new JFrame("信息录入窗口");
private JPanel jp =new JPanel();
private JLabel l_name = new JLabel("姓名:");
private JLabel l_password = new JLabel("口令:");
static JTextField t_name = new JTextField(10);
static JPasswordField t_password = new JPasswordField(10);
private JButton ok = new JButton("ok");
private JButton cancel = new JButton("cancel");
public MyJFrame()
{
ok.addActionListener(new okListener());
cancel.addActionListener(new cancelListener());
jp.add(l_name);
jp.add(t_name);
jp.add(l_password);
jp.add(t_password);
jp.add(ok);
jp.add(cancel);
jf.add(jp);
jf.setVisible(true);
jf.setSize(190,150);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setLocation(500,270);
}
class okListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
check();
}
}
class cancelListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
}
public static void check()
{
if(t_name.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入用户名!","error",JOptionPane.ERROR_MESSAGE);
}
else if(t_password.getPassword().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入密码","error",JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "登陆成功","error",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args)
{
new MyJFrame();
}
}