简体   繁体   中英

As if multiple window application JFrame Java

I have recently started learning Java and creating gui program using JFrame. I wanted to do something like pressing a button so that everything that is currently in the window disappears (JButton and JLabel) and that it shows the other buttons so that everything is still in one window. My code looks like this:

package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import app.Exit;
import app.Logger;

public class MainMenu extends JFrame implements ActionListener{

    // Create objects
    public static Logger log = new Logger();

    // Buttons declaration
    JButton bExit, bStartBot, bOptions, bAboutLikeplusBot, bChangelog;

    // Label declaration
    JLabel lLogo;

    public MainMenu() throws IOException {

        // Set window size
        log.AddToLog("Set window size", "JFrame", "INFO");
        setSize(800, 500);

        // Disable window resizable
        log.AddToLog("Disable window resizable", "JFrame", "INFO");
        setResizable(false);

        // Set window title
        log.AddToLog("Set window title", "JFrame", "INFO");
        setTitle("LikeplusBot");

        // Set layout
        log.AddToLog("Set layout", "JFrame", "INFO");
        setLayout(null);

        // Add Exit button
        log.AddToLog("Add Exit button", "JFrame/JButton", "INFO");
        bExit = new JButton("Exit");
        bExit.setBounds(610, 365, 160, 70);
        add(bExit);
        bExit.addActionListener(this);

        // Add Start bot button
        log.AddToLog("Add Start bot button", "JFrame/JButton", "INFO");
        bStartBot = new JButton("Start bot");
        bStartBot.setBounds(610, 25, 160, 70);
        add(bStartBot);
        bStartBot.addActionListener(this);

        // Add Options button
        log.AddToLog("Add Options button", "JFrame/JButton", "INFO");
        bOptions = new JButton("Options");
        bOptions.setBounds(610, 110, 160, 70);
        add(bOptions);
        bOptions.addActionListener(this);

        // Add About LikeplusBot button
        log.AddToLog("Add About LikeplusBot button", "JFrame/JButton", "INFO");
        bAboutLikeplusBot = new JButton("About LikeplusBot");
        bAboutLikeplusBot.setBounds(610, 195, 160, 70);
        add(bAboutLikeplusBot);
        bAboutLikeplusBot.addActionListener(this);

        // THIS IS A BUTTON WHICH A PRESS IS TO BE DISPLAYED BY OTHER BUTTONS ---------------------------
        // Add Changelog button
        log.AddToLog("Add Changelog button", "JFrame/JButton", "INFO");
        bChangelog = new JButton("Changelog");
        bChangelog.setBounds(610, 280, 160, 70);
        add(bChangelog);
        bChangelog.addActionListener(this);

        // Add logo photo
        log.AddToLog("Add logo photo", "JFrame/JLabel", "INFO");
        ImageIcon icon = new ImageIcon(MainMenu.class.getResource("/resources/LPB_300x300.png"));
        lLogo = new JLabel();
        lLogo.setBounds(120, 20, 400, 400);
        lLogo.setIcon(icon);
        add(lLogo);

    }

    /*
     * Show window
     */
    public static void main(String[] args) throws IOException {

        // Create object
        MainMenu window = new MainMenu();

        // Chose default close operation
        log.AddToLog("Set default close operation", "JFrame", "INFO");
        window.setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Set window visible
        log.AddToLog("Set window visible", "JFrame", "INFO");
        window.setVisible(true);

        // Set window icon
        log.AddToLog("Set window icon", "JFrame", "INFO");
        ImageIcon img = new ImageIcon(MainMenu.class.getResource("/resources/WindowIcon_32x32.png"));
        window.setIconImage(img.getImage());

    }

    /*
     * What is to be done after pressing the button
     */
    @Override
    public void actionPerformed(ActionEvent e) {

        Object source = e.getSource();

        // Exit button
        if (source==bExit) {

            try {
                log.AddToLog("Exit button clicked", "ActionListener", "INFO");
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            Exit exit = new Exit();
            try {
                exit.Exit();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }

        // Start bot button
        if (source==bStartBot) {

            try {
                log.AddToLog("Start bot button clicked", "ActionListener", "INFO");
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }

        // Options button
        if (source==bOptions) {

            try {
                log.AddToLog("Options button clicked", "ActionListener", "INFO");
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }

        // About LikeplusBot button
        if (source==bAboutLikeplusBot) {

            try {
                log.AddToLog("About LikeplusBot button clicked", "ActionListener", "INFO");
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }

        // THIS IS PERFORMED AFTER THIS BUTTON IS PRESSED ----------------------------------------------
        // Changelog button
        if (source==bChangelog) {

            try {
                log.AddToLog("Changelog button clicked", "ActionListener", "INFO");
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            Changelog changelog = new Changelog();

            // AND I WANT THIS METHOD THAT IS CALLED HERE SHOWING NEW BUTTONS IN THE WINDOW -------------
            try {
                changelog.Changelog();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }

}

I hope you can understand something:)

You should use setVisible(false) for the components you want to hide, and setVisible(true) for the components you want to show. Probably you need to add a boolean parameter on your changeLog method. And do hide or show things according to parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM