关闭jinternalframe后如何更新或关闭主JFrame

时间:2019-05-08 22:35:11

标签: java jframe jinternalframe

我有一些功课要在Eclipse Neon中使用Java语言制作程序。问题是,在我登录jinternalframe后,显示2个jframe。第一个jframe是登录之前我的主框架,第二个是登录后我需要的jframe。

这是我的jframe代码

package mainProgram;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import connection.Connect;
import mainFormContent.*;

public class MainForm extends JFrame implements ActionListener {
    String Id;
    String ID= "";
    String role = "";
    Connect con;
    JMenuBar menuBar;
    JMenu userMenu,viewProducts,transactions,manageProducts,viewTransactions;
    JMenuItem signIn,signUp,signOut,updateProfile,exit,myCart,purchasedProducts;
    JLabel lblImage = new JLabel();
    JDesktopPane desktop = new JDesktopPane(){
        ImageIcon icon = new ImageIcon("images/roses.jpg");
        Image image = icon.getImage();

        Image newimage = image.getScaledInstance(1400, 700, Image.SCALE_SMOOTH);

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(newimage, 0, 0, this);
        }

    };

    private void initGuest(){
        menuBar = new JMenuBar();
        userMenu = new JMenu("User");
        signIn = new JMenuItem("Sign In");
        signUp = new JMenuItem("Sign Up");
        signOut = new JMenuItem("Sign Out");
        updateProfile = new JMenuItem("Update Profile");
        exit = new JMenuItem("Exit");

        menuBar.add(userMenu);
        userMenu.add(signIn);
        userMenu.add(signUp);
        userMenu.add(signOut);
        userMenu.add(updateProfile);
        userMenu.add(exit);

        signOut.setEnabled(false);
        updateProfile.setEnabled(false);

        setJMenuBar(menuBar);

    }

    private void initUser(){
        menuBar = new JMenuBar();
        userMenu = new JMenu("User");
        viewProducts = new JMenu("View Products");
        transactions = new JMenu("Transactions");

        signIn = new JMenuItem("Sign In");
        signUp = new JMenuItem("Sign Up");
        signOut = new JMenuItem("Sign Out");
        updateProfile = new JMenuItem("Update Profile");
        exit = new JMenuItem("Exit");
        myCart = new JMenuItem("My Cart");
        purchasedProducts = new JMenuItem("Purchased Products");


        menuBar.add(userMenu);
        menuBar.add(viewProducts);
        menuBar.add(transactions);

        userMenu.add(signIn);
        userMenu.add(signUp);
        userMenu.add(signOut);
        userMenu.add(updateProfile);
        userMenu.add(exit);

        transactions.add(myCart);
        transactions.add(purchasedProducts);

        signIn.setEnabled(false);
        signUp.setEnabled(false);

        setJMenuBar(menuBar);

    }

    private void initAdmin(){
        menuBar = new JMenuBar();
        userMenu = new JMenu("User");
        manageProducts = new JMenu("Manage Products");
        viewTransactions = new JMenu("View Transactions History");

        signIn = new JMenuItem("Sign In");
        signUp = new JMenuItem("Sign Up");
        signOut = new JMenuItem("Sign Out");
        updateProfile = new JMenuItem("Update Profile");
        exit = new JMenuItem("Exit");


        menuBar.add(userMenu);
        menuBar.add(manageProducts);
        menuBar.add(viewTransactions);

        userMenu.add(signIn);
        userMenu.add(signUp);
        userMenu.add(signOut);
        userMenu.add(updateProfile);
        userMenu.add(exit);

        signIn.setEnabled(false);
        signUp.setEnabled(false);
        updateProfile.setEnabled(false);

        setJMenuBar(menuBar);
    }


    public MainForm(String ID) {    
        Id = ID;
        con = new Connect();
        ResultSet rs;
        try {
            rs = con.executeQuery("SELECT UserRole FROM users WHERE UserID = '"+ID+"'");
            while(rs.next()){
                role = (String) rs.getObject("UserRole");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        setSize(700,1000);
        setTitle("Main Form");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setExtendedState(MAXIMIZED_BOTH);
        setLayout(new BorderLayout());
        setContentPane(desktop);
        if(role.equals("User")){
            initUser();
            signOut.addActionListener(this);
            updateProfile.addActionListener(this);
            exit.addActionListener(this);
            viewProducts.addActionListener(this);
            myCart.addActionListener(this);
            purchasedProducts.addActionListener(this);
        }
        else if(role.equals("Admin")){
            initAdmin();
            signOut.addActionListener(this);
            exit.addActionListener(this);
            manageProducts.addActionListener(this);
            viewTransactions.addActionListener(this);
        }
        setVisible(true);   

    }

    public MainForm(){
        setSize(700,1000);
        setTitle("Main Form");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setExtendedState(MAXIMIZED_BOTH);
        setLayout(new BorderLayout());
        setContentPane(desktop);
        initGuest();
        signIn.addActionListener(this);
        signUp.addActionListener(this);
        exit.addActionListener(this);
        setVisible(true);

    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new MainForm();
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        setContentPane(desktop);
        if(role.equals("User")){
            if(e.getSource()==signOut){
                dispose();
                new MainForm();
            }
            else if(e.getSource()==updateProfile){
                desktop.removeAll();
                desktop.add(new UpdateProfile(Id));
            }
            else if(e.getSource()==exit){
                dispose();
            }
            else if(e.getSource()==viewProducts){
                desktop.removeAll();
                desktop.add(new ViewProducts(Id));
            }
            else if(e.getSource()==myCart){
                desktop.removeAll();
                desktop.add(new MyCart(Id));
            }
            else if(e.getSource()==purchasedProducts){
                desktop.removeAll();
                desktop.add(new PurchasedProducts(Id));
            }
        }
        else if(role.equals("Admin")){
            if(e.getSource()==signOut){
                dispose();
                new MainForm();
            }
            else if(e.getSource()==exit){
                dispose();
            }
            else if(e.getSource()==manageProducts){
                desktop.removeAll();
                desktop.add(new ManageProducts(Id));
            }
            else if(e.getSource()==viewTransactions){
                desktop.removeAll();
                desktop.add(new ViewTransactions(Id));
            }
        }
        else{
            if(e.getSource()==signIn){
                desktop.removeAll();
                desktop.add(new SignIn());
            }
            else if(e.getSource()==signUp){
                desktop.removeAll();
                desktop.add(new SignUp());
            }
            else if(e.getSource()==exit){
                dispose();
            }
        }

    }

}

这是我的jinternalframe代码


import java.awt.event.*; 
import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import connection.Connect;

public class SignIn extends JInternalFrame implements ActionListener {

    String ID = "";
    Connect con;
    JPanel mainPanel,topPanel,midPanel,cbPanel,botPanel;
    JLabel lblTop,lblEmail,lblPassword;
    JCheckBox rememberCB;
    JTextField txtEmail;
    JPasswordField txtPassword;
    JButton signinBtn;



    private void init(){
        mainPanel = new JPanel(new GridLayout(3,1));
        topPanel = new JPanel(new GridLayout(1,1));
        midPanel = new JPanel(new GridLayout(3,2,10,15));
        botPanel = new JPanel(new FlowLayout());
        lblTop = new JLabel("Sign in to Lave's Vuitton",JLabel.CENTER);
        lblTop.setFont(lblTop.getFont().deriveFont(16.0f));
        lblEmail = new JLabel("Email");
        lblPassword = new JLabel("Password");
        txtEmail = new JTextField();
        txtPassword = new JPasswordField();
        rememberCB =new JCheckBox("Remember Me");
        signinBtn = new JButton("Sign In");

        lblTop.setForeground(Color.WHITE);
        lblEmail.setForeground(Color.WHITE);
        lblPassword.setForeground(Color.WHITE);
        rememberCB.setForeground(Color.WHITE);
        rememberCB.setBackground(Color.PINK);
        signinBtn.setForeground(Color.PINK);

        topPanel.setBorder(BorderFactory.createEmptyBorder(0,0,25,0));
        topPanel.add(lblTop);
        midPanel.add(lblEmail);
        midPanel.add(txtEmail);
        midPanel.add(lblPassword);
        midPanel.add(txtPassword);
        midPanel.add(rememberCB);
        botPanel.setBorder(BorderFactory.createEmptyBorder(38,0,0,0));
        botPanel.add(signinBtn);

        mainPanel.add(topPanel);
        mainPanel.add(midPanel);
        mainPanel.add(botPanel);

        topPanel.setBackground(Color.PINK);
        midPanel.setBackground(Color.PINK);
        botPanel.setBackground(Color.PINK);
        mainPanel.setBackground(Color.PINK);

        add(mainPanel);

    }

    public SignIn(){
        con = new Connect();
        setSize(450,300);

        setTitle("SignIn");
        setClosable(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());
        init();
        signinBtn.addActionListener(this);
        setVisible(true);

    }

    boolean check(String email,String password){
        String pass = "";
        String emai = "";
        try {
            ResultSet rs = con.executeQuery("SELECT UserID AS IDentity FROM users WHERE UserEmail = '"+email+"'");
            while(rs.next()){
                ID = (String)rs.getObject("IDentity");
            }
            rs = con.executeQuery("SELECT UserEmail AS Emai FROM users WHERE UserID = '"+ID+"'");
            while(rs.next()){
                emai = (String)rs.getObject("Emai");
            }
            rs = con.executeQuery("SELECT UserPassword AS Pass FROM users WHERE UserID = '"+ID+"'");
            while(rs.next()){
                pass = (String)rs.getObject("Pass");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (!email.equals(emai)){
            JOptionPane.showMessageDialog(this,"Email Not Registered!");
            return false;
        }
        else if(!password.equals(pass)){
            JOptionPane.showMessageDialog(this,"Invalid Password!");
            return false;
        }
        else if(password.equals(pass)){
            return true;
        }
        return false;
    }



    @Override
    public void actionPerformed(ActionEvent a) {
        // TODO Auto-generated method stub
        if(a.getSource()==signinBtn){
            String email = txtEmail.getText();
            char[]password = txtPassword.getPassword();
            String pass = new String(password);
            if(email.isEmpty()||pass.isEmpty())JOptionPane.showMessageDialog(this,"Please fill all space");
            else if(!email.isEmpty()||!pass.isEmpty()){
                if(check(email,pass)==true){
                    if(rememberCB.isSelected()){

                        JOptionPane.showMessageDialog(this,"Hello, "+email.substring(0 ,email.indexOf("@"))+" ! You don't have to fill the password again next time.");
                        dispose();
                        new MainForm(ID);
                    }
                    else {
                        JOptionPane.showMessageDialog(this,"Hello, "+email.substring(0 ,email.indexOf("@"))+" !");                  
                        dispose();
                        new MainForm(ID);
                    }
                }
            }
        }


    }

}

完成jinternalframe的登录后,我需要关闭第一个jframe并显示新的jframe。 或第一个jframe更新到我的新jframe而不关闭它。

需要帮助...#初学者# 谢谢...

0 个答案:

没有答案