Swing对话框的问题,求教各位! 从JDialog继承一个类出来,添加用户输入的控件,添加事件响应就好了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我做的登陆窗口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); }} 弱弱问下 HashMap中存放元素时 有没可能hashCode相同 而equals值不同? JAVA 重构疑问 急求 公园租车 思路或 代码 如何将一个字符串转化为时间,从而得到其分钟数? 在线等解答!!! UltraEdit用于调试java,Ctrl+Shift+0快捷键被输入法占用了,如何改变javac等命令的快捷键? 再问个线程的问题?100分送 求教:流套接字与数据报套接字有什么区别吗? java中调用有界面的DLL(VC)的问题? Eclipse中开发SWT的东西用那个插件最好用? 今天当上了非技术区的斑竹,高兴散分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);
}
}