Java框架图标化问题 [英] Java frame iconified issue

查看:91
本文介绍了Java框架图标化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何修改一些现有代码,以使启动的控制台窗口最小化为一个托盘图标,而不是默认行为(即仅打开窗口).不幸的是我不懂Java,所以我只需要搜索Google并根据对我来说有意义的代码进行猜测和检查.我知道这要问很多.我正在尽我所能缓慢地使用Java,我非常感谢您的帮助.有人可以阅读此文件,并告诉我是否需要进行明显的布尔翻转来更改此行为,或者换出一些事件处理程序.该代码已经提供了在托盘图标和全窗口之间来回切换的规定,并且我通过阅读窗口侦听器(特别是windowIconified)取得了一些进展,但是我只是没有足够的经验来真正了解我所进行的更改因为它们并不立即显而易见.下面的文件是该项目中的许多文件之一,因此,如果在阅读后感到不正确,并且缺少适用的代码,我可以提供.但是,如果我正确地理解了代码,则这是在控制台窗口中建立的文件,因此我认为需要在此处进行更改.谢谢您的帮助!

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

}

推荐答案

以最小化为托盘图标开始

starts out minimized as a tray icon

您需要使用:

setExtendedState(JFrame.ICONIFIED);

设置其他框架属性时.

这篇关于Java框架图标化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆