Java Jframe ICONIFIED关闭失败

时间:2018-11-23 05:15:56

标签: java swing jframe minimize

感谢上一个问题,我现在可以将我的应用程序作为任务栏图标(Java frame iconified issue)进行启动,但是现在遇到了一些其他问题,无法解决该问题。当运行主程序时,控制台窗口确实不再按预期启动全屏,但是,该窗口不仅在右下角形成了系统任务栏图标,还在任务栏上形成了传统的最小化窗口。我已经注意到,以前在进行此更改之前,当按下“隐藏窗口”按钮时,主任务栏窗口消失了,只在右上角保留了较小的系统任务栏图标。似乎在上一个问题中建议的更改使窗口保持运行状态,但似乎从视图中隐藏了它。我该如何改变这种行为?我只希望在右下角看到小系统任务栏图标。同样,退出主应用程序后,任务栏图标不会关闭。它们保持打开状态,如果主应用程序多次启动,有时会堆积起来。如果我只能弄清楚“隐藏窗口”动作的来源,就好像可以弄清楚,但是,我看不到找到代码中引用的那个按钮的动作。请帮忙!

package com.skcraft.launcher.dialog;

import com.skcraft.launcher.Launcher;
import com.skcraft.launcher.swing.LinedBoxPanel;
import com.skcraft.launcher.swing.MessageLog;
import com.skcraft.launcher.swing.SwingHelper;
import com.skcraft.launcher.util.PastebinPoster;
import com.skcraft.launcher.util.SharedLocale;
import lombok.Getter;
import lombok.NonNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import static com.skcraft.launcher.util.SharedLocale.tr;

/**
 * A frame capable of showing messages.
 */
public class ConsoleFrame extends JFrame {

private static ConsoleFrame globalFrame;

@Getter private final Image trayRunningIcon;
@Getter private final Image trayClosedIcon;

@Getter private final MessageLog messageLog;
@Getter private LinedBoxPanel buttonsPanel;

private boolean registeredGlobalLog = false;

/**
 * Construct the frame.
 *
 * @param numLines number of lines to show at a time
 * @param colorEnabled true to enable a colored console
 */
public ConsoleFrame(int numLines, boolean colorEnabled) {
    this(SharedLocale.tr("console.title"), numLines, colorEnabled);
}

/**
 * Construct the frame.
 * 
 * @param title the title of the window
 * @param numLines number of lines to show at a time
 * @param colorEnabled true to enable a colored console
 */
public ConsoleFrame(@NonNull String title, int numLines, boolean colorEnabled) {
    messageLog = new MessageLog(numLines, colorEnabled);
    trayRunningIcon = SwingHelper.createImage(Launcher.class, "tray_ok.png");
    trayClosedIcon = SwingHelper.createImage(Launcher.class, "tray_closed.png");

    setTitle(title);
    setIconImage(trayRunningIcon);

    setSize(new Dimension(650, 400));
    setExtendedState(JFrame.ICONIFIED); //This is the new line of code added to minimize the window on load
    initComponents();

    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent event) {
            performClose();
        }
    });
}

/**
 * Add components to the frame.
 */
private void initComponents() {
    JButton pastebinButton = new JButton(SharedLocale.tr("console.uploadLog"));
    JButton clearLogButton = new JButton(SharedLocale.tr("console.clearLog"));
    buttonsPanel = new LinedBoxPanel(true);

    buttonsPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
    buttonsPanel.addElement(pastebinButton);
    buttonsPanel.addElement(clearLogButton);

    add(buttonsPanel, BorderLayout.NORTH);
    add(messageLog, BorderLayout.CENTER);
    clearLogButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            messageLog.clear();
        }
    });

    pastebinButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            pastebinLog();
        }
    });

    hideMessages();
}

/**
 * Register the global logger if it hasn't been registered.
 */
private void registerLoggerHandler() {
    if (!registeredGlobalLog) {
        getMessageLog().registerLoggerHandler();
        registeredGlobalLog = true;
    }
}

/**
 * Attempt to perform window close.
 */
protected void performClose() {
    messageLog.detachGlobalHandler();
    messageLog.clear();
    registeredGlobalLog = false;
    dispose();
}

/**
 * Send the contents of the message log to a pastebin.
 */
private void pastebinLog() {
    String text = messageLog.getPastableText();
    // Not really bytes!
    messageLog.log(tr("console.pasteUploading", text.length()), messageLog.asHighlighted());

    PastebinPoster.paste(text, new PastebinPoster.PasteCallback() {
        @Override
        public void handleSuccess(String url) {
            messageLog.log(tr("console.pasteUploaded", url), messageLog.asHighlighted());
            SwingHelper.openURL(url, messageLog);
        }

        @Override
        public void handleError(String err) {
            messageLog.log(tr("console.pasteFailed", err), messageLog.asError());
        }
    });
}

public static void showMessages() {
    ConsoleFrame frame = globalFrame;
    if (frame == null) {
        frame = new ConsoleFrame(10000, false);
        globalFrame = frame;
        frame.setTitle(SharedLocale.tr("console.launcherConsoleTitle"));
        frame.registerLoggerHandler();
        frame.setVisible(true);
    } else {
        frame.setVisible(true);
        frame.registerLoggerHandler();
        frame.requestFocus();
    }
}

public static void hideMessages() {
    ConsoleFrame frame = globalFrame;
    if (frame != null) {
        frame.setVisible(false);
    }
}

}

enter image description here

0 个答案:

没有答案