用JAVA写一个点确定弹出密码输入的窗口怎么写啊。谢谢帮忙
发布网友
发布时间:2023-07-19 19:08
我来回答
共1个回答
热心网友
时间:2024-12-02 15:55
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("JFrame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
void showLoginDialog(JFrame frame) {
JPanel p = new JPanel(new GridLayout(0,1));
JTextField tfUserName = new JTextField();
JTextField tfPassword = new JPasswordField();
p.add(new JLabel("Username"));
p.add(tfUserName);
p.add(new JLabel("Password:"));
p.add(tfPassword);
if (JOptionPane.showConfirmDialog(frame
,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();
}
}
=================
看看这个怎么样!