简体   繁体   English

好像多个 window 应用 JFrame Java

[英]As if multiple window application JFrame Java

I have recently started learning Java and creating gui program using JFrame.我最近开始学习 Java 并使用 JFrame 创建 gui 程序。 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.我想做一些事情,比如按下一个按钮,以便当前在 window 中的所有内容都消失(JButton 和 JLabel),并显示其他按钮,以便所有内容仍然在一个 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.您应该对要隐藏的组件使用 setVisible(false),对要显示的组件使用 setVisible(true)。 Probably you need to add a boolean parameter on your changeLog method.可能您需要在 changeLog 方法中添加 boolean 参数。 And do hide or show things according to parameter并根据参数隐藏或显示事物

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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