как обновить или закрыть основной JFrame после закрытия jinternalframe - PullRequest
0 голосов

У меня есть домашнее задание по созданию программ, использующих язык Java в Eclipse Neon.Проблема в том, что отображается 2 jframe после того, как я войду в свой jinternalframe.первый 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);
                    }
                }
            }
        }


    }

}

Мне нужно закрыть первый jframe после того, как я закончу вход в мой jinternalframe и покажу новый jframe.или первое обновление jframe для моего нового jframe без его закрытия.

нужна помощь ... # beginner # Спасибо ...

...