Java frame iconified issue

为君一笑 提交于 2019-12-13 03:26:13

问题


I am trying to figure out how to modify some existing code so that the console window that launches, starts out minimized as a tray icon instead of the default behavior which is to just open the window. Unfortunately I do not know Java, so I am having to just search Google and guess and check based on what code does make sense to me. I know this is asking a lot. I am trying my best to slowly pickup Java and I really appreciate the help. Could someone please read over this file and tell me if there is an obvious Boolean flip I need to make to change this behavior, or swap out some event handler. The code already has the provision to switch back and forth between tray icon and full window and I have made some progress by reading up on window listeners, specifically windowIconified, but I just don't have enough experience yet to really understand the changes I am making as they are not immediately obvious. The file below is one of many in this project, so if after reading you feel I am mistaken and the applicable code is missing, I can provide it. If I am properly understanding the code though, this is the file that builds out the console window, so I would assume the changes need to be made here. Thank you for any help!

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));
    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);
    }
}

}


回答1:


starts out minimized as a tray icon

You need to use:

setExtendedState(JFrame.ICONIFIED);

when you set the other frame properties.



来源:https://stackoverflow.com/questions/53440690/java-frame-iconified-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!