从JDialog继承一个类出来,添加用户输入的控件,添加事件响应就好了。
解决方案 »
- 运行Java程序时类名后带参数是如何实现的?
- 求助Java 中调用C 函数遇到的问题。
- netbeans中的问题java.awt.EventQueue.invokeLater(new Runnable())求解
- 俄而
- 小弟,遇到了java.lang.NullPointerException,帮忙看看问题出在哪里,送分100,急,在线等.....
- 不同的包里面有名字一样的class可以吗?
- JBUILDER下如何引入第三方的包
- 请问在swing布局中,如何使CardLayout 布局的lpanel切换卡片.
- 为何这个程序输入的三个数据只能是n1<n1<n3才可以比较,否则就不可以比较大小;急
- java中如何连接数据库?
- 今天当上了非技术区的斑竹,高兴散分200,呵呵
- 关于管道的问题!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;public class LoginUI extends JDialog
{ JTextField userFiled;
JPasswordField passwordFiled;
Container c; public LoginUI()
{
userFiled = null;
passwordFiled = null;
c = null;
String title = "System Login";
JFrame f = new JFrame();
new LoginUI(f, title);
} public LoginUI(JFrame f, String title)
{
super(f, title, true);
userFiled = null;
passwordFiled = null;
c = null;
setSize(400, 160);
setResizable(false);
Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(ds.width / 3, ds.height / 4);
c = getContentPane();
c.setLayout(new FlowLayout(0));
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ew)
{
System.exit(0);
} });
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 1));
JLabel label1 = new JLabel(new ImageIcon("Key.gif"));
p.add(label1);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(5, 1));
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(4, 1));
JButton okButton = new JButton("OK");
ActionListener okAction = new ActionListener() { public void actionPerformed(ActionEvent ea)
{
System.exit(0);
} };
JButton cancelButton = new JButton("Cancel");
ActionListener cancelAction = new ActionListener() { public void actionPerformed(ActionEvent ea)
{
System.exit(0);
} };
cancelButton.addActionListener(cancelAction);
p2.add(okButton);
p2.add(cancelButton);
JLabel label = new JLabel("\u8BF7\u60A8\u8F93\u5165\u6709\u6548\u7684\u7CFB\u7EDF\u767B\u9646\u53E3\u4EE4\u3002");
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout(0));
JLabel passwordName = new JLabel("\u53E3\u4EE4:");
JPanel p4 = new JPanel();
p4.setLayout(new FlowLayout(0));
JLabel userName = new JLabel("\u7528\u6237:");
userFiled = new JTextField(16);
passwordFiled = new JPasswordField(16);
ActionListener ok_Action = new ActionListener() { public void actionPerformed(ActionEvent ea)
{
String user_str = userFiled.getText();
String password_str = passwordFiled.getText();
if(user_str.trim() == null || user_str.equals(""))
{
JOptionPane.showMessageDialog(c, "\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A!", "System Message!", 0);
return;
}
if(password_str.trim() == null || password_str.equals(""))
{
JOptionPane.showMessageDialog(c, "\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A!", "System Message!", 0);
return;
}
if(!user_str.trim().equals("yanglongfei"))
{
JOptionPane.showMessageDialog(c, "\u7528\u6237\u540D\u9519\u8BEF!", "System Message!", 0);
return;
}
if(!password_str.trim().equals("yanglongfei"))
{
JOptionPane.showMessageDialog(c, "\u5BC6\u7801\u9519\u8BEF!", "System Message!", 0);
return;
} else
{
setVisible(false);
dispose();
return;
}
} };
okButton.addActionListener(ok_Action);
p3.add(userName);
p3.add(userFiled);
p4.add(passwordName);
p4.add(passwordFiled);
p1.add(label);
p1.add(p3);
p1.add(p4);
c.add(p);
c.add(p1);
c.add(p2);
setVisible(true);
}
}