[英]JDialog login with JFrame
我正在尝试创建一个'模态'JDialog(我希望这是它应该是什么样子),我想在我的对话框中输入我的用户和密码,如果它是正确的,那么打开我的主JFrame,我解析一些信息。 当我执行登录按钮时出现问题,我不能用它的文本框等引用当前对象(this); 这是代码,这是错误:
另外我有点困惑,因为当我成功登录时,我应该打开一个JFrame .....不确定如何继续,因为我应该只使用一个JFrame并切换我的面板。 提前致谢
package test_area;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class jpanel1 extends JFrame{
static boolean loginBool = false; //if this goes true, then I go to my frame
// JPANEL---------------------------------------------
public static class panel1 extends JPanel implements ActionListener{
//simple constructor
public panel1(){
JPanel a = this;
a.setLayout(new GridLayout(5,1,5,5));
JLabel username = new JLabel("Username");
JTextField usernameTxt = new JTextField(8);
JLabel password = new JLabel("Password");
JPasswordField passwordTxt = new JPasswordField(55);
JButton doIt = new JButton("Log In");
doIt.addActionListener(this);
a.add(username);
a.add(usernameTxt);
a.add(password);
a.add(passwordTxt);
a.add(doIt);
a.setSize(200,200);
a.setVisible(true);
}
//constructor with param
// public panel1(JLabel a, JTextField b, JLabel c, JPasswordField d, JButton e){
//}
//@Overwritten method
@Override
public void actionPerformed(ActionEvent ae) {
String user;
String pw;
user = a.usernameTxt.getText();
pw = a.passwordTxt.getText();
// package does not exist
}
}
// JPANEL---------------------------------------------
//JFrame
public jpanel1(){
JFrame frame = new JFrame("Login Pane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new panel1());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
jpanel1 gg = new jpanel1(); //frame;
}
}
声明一个静态布尔值,一旦登录正确就应该启动,然后以某种方式打开新的JFrame ......
你的代码甚至没有对JDialog的引用......
假设frame
是您的应用程序的JFrame
类型的主窗口,并dialog
您的JDialog
类型的登录对话框。 在开始显示模态登录对话框。
frame.setVisible(true);
dialog.setModal(true);
dialog.setVisible(true);
登录成功时隐藏对话框
dialog.setVisible(false);
你不需要改变模态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.