📄 logindialogdemo.java
字号:
package LoginDialogDemo;
//LoginDialogDemo.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class LoginDialogDemo
extends JFrame {
JButton button = new JButton("Click Me");
JPanel panel = new JPanel(new FlowLayout());
public LoginDialogDemo() {
final JFrame frame = this;
this.getContentPane().add(panel, BorderLayout.SOUTH);
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showLoginDialog(frame);
}
});
this.setSize(300, 200);
this.setTitle("显示登陆对话框");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
void showLoginDialog(JFrame frame) {
JPanel p = new JPanel(new GridLayout(0, 1));
JTextField tfUserName = new JTextField();
JPasswordField tfPassword = new JPasswordField();
p.add(new JLabel("Username: "));
p.add(tfUserName);
p.add(new JLabel("Password: "));
p.add(tfPassword);
/*
JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or
informs them of something. For information about using JOptionPane, see How to Make Dialogs,
a section in The Java Tutorial.
*/
/*
public static int showConfirmDialog(Component\u00A0parentComponent,
Object\u00A0message,
String\u00A0title,
int\u00A0optionType,
int\u00A0messageType)
throws HeadlessException
Parameters:
parentComponent - determines the Frame in which the dialog is displayed;
if null, or if the parentComponent has no Frame, a default Frame is used.
message - the Object to display
title - the title string for the dialog
optionType - an integer designating the options available on the dialog: YES_NO_OPTION, or YES_NO_CANCEL_OPTIO
NmessageType - an integer designating the kind of message this is; primarily used to determine the icon from the pluggable Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
*/
if (JOptionPane.showConfirmDialog(frame // may want to pass your application frame here
, p
, "Login"
, JOptionPane.OK_CANCEL_OPTION
, JOptionPane.PLAIN_MESSAGE
) == JOptionPane.OK_OPTION) {
System.out.println("User Name:" + tfUserName.getText());
System.out.println("Password:" + new String(tfPassword.getPassword()));
}
}
public static void main(String[] args) {
LoginDialogDemo frame = new LoginDialogDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -