简体   繁体   English

Java框架图标化问题

[英]Java frame iconified issue

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. 不幸的是我不懂Java,所以我只需要搜索Google并根据对我来说有意义的代码进行猜测和检查。 I know this is asking a lot. 我知道这要问很多。 I am trying my best to slowly pickup Java and I really appreciate the help. 我正在尽我所能缓慢地使用Java,我非常感谢您的帮助。 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. 该代码已经提供了在托盘图标和全窗口之间来回切换的规定,并且通过阅读窗口侦听器(特别是windowIconified)已经取得了一些进展,但是我只是没有足够的经验来真正了解我的更改因为它们并不立即显而易见。 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);
    }
}

}

窗口

starts out minimized as a tray icon 最小化为托盘图标

You need to use: 您需要使用:

setExtendedState(JFrame.ICONIFIED);

when you set the other frame properties. 设置其他框架属性时。

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

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