您好,登錄后才能下訂單哦!
本文實例講述了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。