Java框架图标化问题

时间:2018-11-23 04:33:23

标签: java swing windowlistener

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

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

}

Window

1 个答案:

答案 0 :(得分:1)

  

以最小化为托盘图标开始

您需要使用:

setExtendedState(JFrame.ICONIFIED);

设置其他框架属性时。