您好,登录后才能下订单哦!
本文实例讲述了Java Swing中JDialog实现用户登陆UI。分享给大家供大家参考,具体如下:
JDialog是一种对话框组件,它常常与JOptionPane配合使用。JOptionPane提供对话框内部的消息、按钮等内容,JDialog提供对话框窗体,提供模态/非模态等属性。JDialog与JFrame在外观上的区别在于,它没有最大化/最小化按钮。如下图所示:
JDialog窗体
点击File-》connect之后弹出登陆窗口:
JFrame窗体
在下面的demo中,演示了JDialog构造登录窗体,以及从窗体中获得数据的方法。
LoginDemoStart.java
package awtDemo; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /* * @功能:使用JDialog构造登陆对话框,并获取登陆用户信息 * @版本:20150804 * @结构:LoginDemoStart[主类,提供初始UI],PasswordChooser[提供登陆UI并获取用户信息],User[保存用户信息] */ @SuppressWarnings("serial") class LoginDemoStart extends JFrame{ /* * @功能:提供初始UI,并调用PasswordChooser登陆界面 * @版本:20150804 */ private JTextArea textArea; private PasswordChooser passwordChooser ; public LoginDemoStart(){ //构造菜单栏 JMenuBar mbar = new JMenuBar(); setJMenuBar(mbar); JMenu fileMenu = new JMenu("File"); JMenuItem connectItem = new JMenuItem("connect"); connectItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //if 1st time,construct a dialog passwordChooser = new PasswordChooser(LoginDemoStart.this); passwordChooser.setVisible(true); if(passwordChooser.isLogedIn()) { User u = passwordChooser.getUser(); textArea.append("Username = " + u.getName() + ", Password = " + new String(u.getPassword()) + "\n"); } } }); JMenuItem exitItem = new JMenuItem("exit"); exitItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }); mbar.add(fileMenu); fileMenu.add(connectItem); fileMenu.add(exitItem); //添加文本区域 textArea = new JTextArea(); add(new JScrollPane(textArea), BorderLayout.CENTER); //设置窗体属性 setTitle("LoginDemoStart - www.jb51.net"); setSize(300,200); setLocationRelativeTo(null); } public static void main(String[] args) { // TODO Auto-generated method stub LoginDemoStart loginDemoStart = new LoginDemoStart(); loginDemoStart.setVisible(true); } }
PasswordChooser.java
package awtDemo; import java.awt.*; import java.awt.event.*; import javax.swing.*; /* * @功能:提供登陆UI并获取用户信息 * @版本:20150804 */ @SuppressWarnings("serial") class PasswordChooser extends JDialog{ private JTextField username ; private JPasswordField password ; private JButton okButton ; private JButton cancelButton ; private boolean isLogedIn = false; public PasswordChooser(Frame parent){ super(parent, true); //本UI包含2个panel JPanel inputPanel = new JPanel(); JPanel buttonPanel = new JPanel(); //构造inputPanel inputPanel.setLayout(new GridLayout(2,2)); inputPanel.add(new JLabel("Useername:")); username = new JTextField(); username.setColumns(10); password = new JPasswordField(); password.setColumns(10); inputPanel.add(username); inputPanel.add(new JLabel("Password:")); inputPanel.add(password); inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); //构造buttonPanel okButton = new JButton("OK"); okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub isLogedIn = true; setVisible(false); } }); cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub setVisible(false); } }); buttonPanel.add(okButton); buttonPanel.add(cancelButton); buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); //构造主框架 setLayout(new BorderLayout()); getContentPane().add(inputPanel, BorderLayout.NORTH); getContentPane().add(buttonPanel, BorderLayout.SOUTH); //设置窗体属性 setTitle("PasswordChooser - www.jb51.net"); setLocationRelativeTo(inputPanel); //setPreferredSize(new Dimension(300, 200)); pack(); //System.out.println(getPreferredSize()); } public void setUser(User u){ username.setText(u.getName()); } public User getUser(){ return new User(username.getText(), password.getPassword()); } public boolean isLogedIn(){ return isLogedIn; } public static void main(String[] args) { // TODO Auto-generated method stub } }
User.java
package awtDemo; /* * @功能:保存用户信息 * @版本:20150804 */ class User{ private String name; private char[] password; public User(String aName, char[] aPassword){ name = aName; password = aPassword; } public String getName(){ return name; } public char[] getPassword(){ return password; } public void setName(String aName){ name = aName; } public void setPassword(char[] aPassword){ password = aPassword; } }
更多关于java相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。