无法从内容窗格调用 Jframe



我试图建立这个管理系统。 如果用户忘记了密码,则登录页面是主页,则可以单击忘记密码来更改密码。 但 每当我从登录框架调用忘记密码时,都会打开一个小窗口,而不是忘记页面的编码窗口。 注意:忘记窗口单独执行时运行良好。

登录页面

`package hM_prac;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.sql.*;
public class login extends JFrame implements ActionListener {
private JPanel contentPane;
private JTextField txtUsername;
private JPasswordField pwdwhatever;
JLabel lblNewLabel,lblNewLabel_1,lblNewLabel_2;
JButton btnForgotPassword ,btnSignUp,btnLogin;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
login frame = new login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection con;
PreparedStatement ps;
ResultSet rs;
/**
* Create the frame.
*/
public login() {
super("LOGIN");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 465, 300);
contentPane = new JPanel();
contentPane.setBorder(new LineBorder(new Color(0, 0, 0)));
setContentPane(contentPane);
contentPane.setLayout(null);
lblNewLabel = new JLabel("Welcome to MYHOTEL");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Verdana", Font.PLAIN, 18));
lblNewLabel.setBounds(10, 11, 414, 23);
contentPane.add(lblNewLabel);
txtUsername = new JTextField();
txtUsername.setText("");
txtUsername.setBounds(222, 96, 94, 20);
contentPane.add(txtUsername);
txtUsername.setColumns(20);
lblNewLabel_1 = new JLabel("Username:");
lblNewLabel_1.setLabelFor(txtUsername);
lblNewLabel_1.setBounds(121, 99, 69, 14);
contentPane.add(lblNewLabel_1);
lblNewLabel_2 = new JLabel("Password:");
lblNewLabel_2.setBounds(121, 138, 69, 14);
contentPane.add(lblNewLabel_2);
btnLogin = new JButton("Login");
btnLogin.setBounds(169, 166, 89, 23);
contentPane.add(btnLogin);
btnSignUp = new JButton("Sign Up");
btnSignUp.setBounds(305, 215, 89, 23);
contentPane.add(btnSignUp);
btnForgotPassword = new JButton("Forgot password");
btnForgotPassword.setBounds(37, 215, 135, 23);
contentPane.add(btnForgotPassword);
pwdwhatever = new JPasswordField();
pwdwhatever.setText("");
pwdwhatever.setBounds(222, 136, 94, 17);
contentPane.add(pwdwhatever);
btnLogin.addActionListener(this);
btnSignUp.addActionListener(this);
btnForgotPassword.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
Object o=ae.getSource();
if(o==btnLogin)
{               
String uname,pass;
uname=txtUsername.getText();
pass=pwdwhatever.getText(); 
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
ps=con.prepareStatement("select * from account where username=? and password=?");
ps.setString(1,uname);
ps.setString(2,pass);
rs=ps.executeQuery();
if(rs.next())
{
System.out.println("Valid User.");
JOptionPane.showMessageDialog(this,"Valid User","Done",JOptionPane.INFORMATION_MESSAGE);
this.dispose();
rs.close();
ps.close();
//new homepage();
}
else
{
JOptionPane.showMessageDialog(this,"Incorrect","Error",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
if(o==btnSignUp)
{
this.dispose();
new sign_up().setVisible(true);
}   
if(o==btnForgotPassword)
{
this.dispose();
new forget().setVisible(true);
}
}
}
`

忘记页面

` package hM_prac;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import  java.sql.*;
public class forget extends JFrame implements ActionListener {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JPasswordField passwordField;
JButton btnSubmit,btnSubmitAnswer;
JLabel label,lblNewLabel_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
System.out.println("hh");
forget window = new forget();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public forget() {
//super("login");
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 374, 358);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Username:");
lblNewLabel.setBackground(Color.BLACK);
lblNewLabel.setBounds(18, 77, 92, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Security Question:");
lblNewLabel_1.setBackground(Color.BLACK);
lblNewLabel_1.setBounds(18, 139, 132, 14);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Answer:");
lblNewLabel_2.setBackground(Color.BLACK);
lblNewLabel_2.setBounds(18, 164, 92, 14);
frame.getContentPane().add(lblNewLabel_2);

//frame.getContentPane().add(lblNewLabel_3);
JLabel lblNewLabel_5 = new JLabel("Forgot password");
lblNewLabel_5.setFont(new Font("Tahoma", Font.BOLD, 19));
lblNewLabel_5.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_5.setBounds(10, 11, 338, 23);
frame.getContentPane().add(lblNewLabel_5);
textField = new JTextField();
textField.setBackground(Color.WHITE);
textField.setBounds(156, 77, 182, 14);
frame.getContentPane().add(textField);
textField.setColumns(10);
btnSubmit = new JButton("Submit ");
btnSubmit.setBackground(Color.WHITE);
btnSubmit.setBounds(184, 102, 142, 23);
frame.getContentPane().add(btnSubmit);
label = new JLabel("..");
label.setBackground(Color.BLACK);
label.setBounds(160, 139, 178, 14);
frame.getContentPane().add(label);
textField_1 = new JTextField();
textField_1.setBounds(156, 164, 192, 14);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
btnSubmitAnswer = new JButton("Submit Answer");
btnSubmitAnswer.setBackground(Color.WHITE);
btnSubmitAnswer.setBounds(184, 189, 142, 23);
frame.getContentPane().add(btnSubmitAnswer);

//frame.getContentPane().add(btnSubmitPassword);

btnSubmit.addActionListener(this);
btnSubmitAnswer.addActionListener(this);
}

Connection con;
PreparedStatement ps;
ResultSet rs;
private JButton btnSubmitPassword;
public void actionPerformed(ActionEvent ae) {
Object o=ae.getSource();
String uname;
if(o==btnSubmit)
{       System.out.println("ques");     
uname=textField.getText();
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
ps=con.prepareStatement("select security_question from account where username=?");
ps.setString(1,uname);
rs=ps.executeQuery();
if(rs.next())
{
String secq=rs.getString("security_question");
System.out.println("question"+secq);
label.setText(secq);
frame.getContentPane().add(label);
}
else if(rs==null){
JOptionPane.showMessageDialog(this,"No such user","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this,"Invalid User","Error",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
else if(o==btnSubmitAnswer)
{
//System.out.println("jj");
String ans;
uname=textField.getText();
ans=textField_1.getText();
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
ps=con.prepareStatement("select * from account where username=? and answer=?");
ps.setString(1,uname);
ps.setString(2,ans);
rs=ps.executeQuery();
if(rs!=null)
{ 
System.out.println("jj");
lblNewLabel_3 = new JLabel("New Password:");
lblNewLabel_3.setBackground(Color.BLACK);
lblNewLabel_3.setBounds(18, 226, 117, 14);
frame.getContentPane().add(lblNewLabel_3);
passwordField = new JPasswordField();
passwordField.setBackground(Color.BLACK);
passwordField.setBounds(160, 226, 178, 17);
//frame.getContentPane().add(passwordField);    
frame.getContentPane().add(passwordField);
btnSubmitPassword = new JButton("Submit password");
btnSubmitPassword.setBounds(82, 285, 182, 23);
frame.getContentPane().add(btnSubmitPassword);
btnSubmitPassword.addActionListener(this);      
}
else{
JOptionPane.showMessageDialog(this,"Wrong answer","Error",JOptionPane.ERROR_MESSAGE);
}
}catch(Exception e){
e.printStackTrace();
}
}
else if(o==btnSubmitPassword){
String pass;
uname=textField.getText();
pass=passwordField.getText();
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
ps=con.prepareStatement("update account set  password=? where username=?");
ps.setString(1,pass);
ps.setString(1,uname);
rs=ps.executeQuery();
if(rs==null)
{
JOptionPane.showMessageDialog(this,"Error","Error",JOptionPane.ERROR_MESSAGE);
}
else{
JOptionPane.showMessageDialog(this,"Password reset","Error",JOptionPane.ERROR_MESSAGE);
this.dispose();
new login();
}}catch(Exception e){
e.printStackTrace();
}
}
}
}

`

这是从JFrame显示另一个窗口的更好方法(就像在这种情况下从"登录"窗口中显示"忘记密码"窗口一样(是将其定义为模态JDialog。以下是一些工作代码来演示可能的功能:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainWindow {
public static void main(String [] args) {
new MainWindow();
}
private JFrame frame;
public MainWindow() {   
frame = new JFrame("Main Window - login");  
JButton button = new JButton("Click me to open dialog");
button.addActionListener(new ButtonListener());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setLocation(200, 200);
frame.setVisible(true);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
new DialogWindow();  
}
}
}
class DialogWindow {
DialogWindow() {
JDialog dialog = new JDialog();
dialog.setTitle("Dialog - forgot");
dialog.setModal(true);  
dialog.add(new JLabel("Do something here..."));
dialog.setSize(250, 150);
dialog.setLocation(600, 200);
dialog.setVisible(true);
}
}

在上面的代码中,可以将MainWindow假定为登录窗口,DialogWindow假定为忘记密码对话框。当用户单击登录窗口中的按钮时,忘记对话框将作为模式窗口打开。然后用户在忘记的对话框中做一些工作;关闭后,它返回到主窗口。

这里有一个指向Oracle的Java Swing教程的链接,其中包含如何使用JFrameJDialog和其他GUI组件的示例。

最新更新