java - 如何使用 SWT AWT 处理 Windows 按键,最小化窗口并返回桌面

标签 java compiler-errors swt awt keypress

对于我使用 SWT(更普遍的是使用 Java)的第一步,我尝试使用一些基本组件制作一个简单的应用程序。这是一个简单的窗口,可以使用 NO_TRIM 最大化。

现在我想在Windows系统按键上编写一个EventHandler来隐藏该窗口并返回桌面。在意识到 SWT.COMMAND 仅适用于 MacOS 后,我替换了:

import org.eclipse.swt.events.KeyAdapter
import org.eclipse.swt.events.KeyAdapter

import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent

但我仍然收到此错误:

The method addKeyListener(KeyListener) in the type Control 
    is not applicable for the arguments (new KeyAdapter(){})

我不明白发生了什么。这是我的代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class MyApp {

    private Shell shell;
    private Display display;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            MyApp window = new MyApp();
            window.init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    private void init() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /**
     * Create contents of the window.
     */
    private void createContents() {
        shell = new Shell(display, SWT.NO_TRIM);
        shell.setText("MyApp");
        shell.setMaximized(true);

        final Image tmp_img_background = new Image(display, "img/background.png");
        shell.setBackgroundImage(tmp_img_background);

        shell.addKeyListener(new KeyAdapter() {
            //@Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_WINDOWS) {
                    shell.setMinimized(true);
                }
            }
        });

        shell.addListener(SWT.Resize, new Listener() {
            //@Override
            public void handleEvent(Event event) {
                Rectangle clientArea = shell.getClientArea();
                final Image img_background = new Image(display, tmp_img_background.getImageData().scaledTo(clientArea.width, clientArea.height));
                shell.setBackgroundImage(img_background);
                tmp_img_background.dispose();
            }
        });
    }
}

如有任何帮助,我们将不胜感激,谢谢。

编辑

我终于找到了一个解决方案来完成预期的任务。我现在使用 AWT/Swing,而不是 SWT。

这是我现在使用的代码,它执行相同的操作,只不过这次“Windows 键”将您带回桌面。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MyApp {

private JFrame frame;
private JPanel panel;
private JLabel label;
private ImageIcon imageIcon;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try
                {
                    MyApp window = new MyApp();
                    window.frame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MyApp() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame("MyApp");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.getContentPane().setBackground(Color.BLACK);
        frame.setVisible(true);

        Dimension frameArea = frame.getSize();
        int w = frameArea.width;
        int h = frameArea.height;

        try
        {
            panel = new JPanel();
            imageIcon = new ImageIcon(
                new ImageIcon(panel.getClass().getResource("/background.png"))
                .getImage()
                .getScaledInstance(w, h, Image.SCALE_DEFAULT)
            );
            label = new JLabel();
            label.setIcon(imageIcon);
            frame.setContentPane(label);
        }
        catch (Exception e)
        {
            System.out.println("File not found !");
        }

        frame.addKeyListener(new KeyAdapter() {
            //@Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_WINDOWS) {
                    frame.setState(JFrame.ICONIFIED);
                }
            }
        });
    }
}

最佳答案

您的编译器正在提示,因为您正在尝试将 AWT Listener/Adapter 添加到 SWT Shell。您必须改用 org.eclipse.swt.events.KeyAdapter。

但是,从表面上看,SWT 不支持 Windows 键。

Here是一个错误报告,要求添加对 Windows 键的支持。

关于java - 如何使用 SWT AWT 处理 Windows 按键,最小化窗口并返回桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16455716/

相关文章:

java - 针对仅使用一次的值优化变量的使用

java - 从图库中选择一张图片,图片后我想将其显示在不同的页面上

java - Java 中的文本文件读取和打印

无法使用 -std=c99 找到 math.h 常量?

java - 在SWT中测量CTabItems的字体大小?

Java SWT : Wrap main loop in exception handler?

java - 在子类构造函数中调用 getClass() 总是安全的吗?

casting - 为什么在将枚举元素分配给 C 中的相同枚举变量类型时必须强制转换枚举元素?

c++ - 将 Windows 代码移植到 Mac

Java Observer/Observable 模式不通知