KeyListener
和ActionListener
接口处理来自用户的事件。 全屏模式下事件处理的正确方法是什么?请扩展此框架,以提供鼠标单击和按键事件的实现,请不要膨胀您的示例(该示例以
Timer
启动全屏独占模式,使用q4312079q更新窗口中的图形):import java.applet.Applet;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.Timer;
public class applet extends Applet
{
Timer timer;
JFrame frame;
DisplayMode[] displayModes = new DisplayMode[] {
new DisplayMode(1280, 800, 32, 60)
};
BufferStrategy bufferStrategy;
Rectangle bounds;
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public void init()
{
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); //displays, fonts, color shemes...
GraphicsDevice device = env.getDefaultScreenDevice(); //for one-display systems
setIgnoreRepaint(true);
GraphicsConfiguration gc = device.getDefaultConfiguration();
frame = new JFrame(gc);
device.setFullScreenWindow(frame);
if (device.isDisplayChangeSupported())
device.setDisplayMode(displayModes[0]);
frame.createBufferStrategy(2);
bufferStrategy = frame.getBufferStrategy();
timer = new Timer(1000 / 50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Graphics2D g = null;
try {
g = (Graphics2D) bufferStrategy.getDrawGraphics();
render(g);
} finally {
g.dispose();
}
bufferStrategy.show();
}
});
}
private void render(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, bounds.width, bounds.height);
}
public void start()
{
timer.start();
}
public void stop()
{
timer.stop();
}
}
#1 楼
它看起来像“如何使用键绑定”和“如何编写鼠标侦听器”中所示的常规方法在全屏独占模式下正常工作。import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
/** @see http://stackoverflow.com/questions/7456227 */
public class FullScreenTest extends JPanel {
private static final String EXIT = "Exit";
private JFrame f = new JFrame("FullScreenTest");
private Action exit = new AbstractAction(EXIT) {
@Override
public void actionPerformed(ActionEvent e) {
f.dispatchEvent(new WindowEvent(
f, WindowEvent.WINDOW_CLOSING));
}
};
private JButton b = new JButton(exit);
public FullScreenTest() {
this.add(b);
f.getRootPane().setDefaultButton(b);
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), EXIT);
this.getActionMap().put(EXIT, exit);
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
FullScreenTest.this.setToolTipText(
"("+ e.getX() + "," + e.getY() + ")");
}
});
}
private void display() {
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice dev = env.getDefaultScreenDevice();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBackground(Color.darkGray);
f.setResizable(false);
f.setUndecorated(true);
f.add(this);
f.pack();
dev.setFullScreenWindow(f);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new FullScreenTest().display();
}
});
}
}
#2 楼
如此处的建议,Mac OS X用户对于全屏应用程序可能会有不同的用户期望。此处显示的另一种方法依赖于com.apple.eawt
类,这些类“提供了一种简单的方法来实现本机功能,以在Mac OS X上微调Java应用程序。” FullScreenUtilities
方法setWindowCanFullScreen()
启用该功能,Application
方法requestToggleFullScreen()
动态更改设置。请注意,展开图标在各个版本之间有何不同。Mac OS 10.9,Mavericks:
Mac OS 10.10,优胜美地:
Mac OS X 10.11,El Capitan:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
/**
* @see https://stackoverflow.com/a/30308671/230513
* @see https://stackoverflow.com/questions/7456227
* @see https://stackoverflow.com/q/13064607/230513
* @see https://stackoverflow.com/q/30089804/230513
* @see https://stackoverflow.com/q/25270465/230513
* @see http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/macosx/classes/com/apple/
*/
public class FullScreenTest extends JPanel {
private static final String NAME = "Mac OS X Full Screen Test";
private static final String TOGGLE = "Toggle Full Screen";
private final JFrame f = new JFrame(NAME);
private final Action exit = new AbstractAction(TOGGLE) {
@Override
public void actionPerformed(ActionEvent e) {
toggleOSXFullscreen(f);
}
};
private final JButton b = new JButton(exit);
public FullScreenTest() {
this.add(b);
f.getRootPane().setDefaultButton(b);
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), TOGGLE);
this.getActionMap().put(TOGGLE, exit);
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
FullScreenTest.this.setToolTipText(
"(" + e.getX() + "," + e.getY() + ")");
}
});
}
private void display() {
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBackground(Color.darkGray);
f.add(this);
f.add(new JLabel(NAME, JLabel.CENTER), BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
if (System.getProperty("os.name").startsWith("Mac OS X")) {
enableOSXFullscreen(f);
toggleOSXFullscreen(f);
enableOSXQuitStrategy();
}
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println(e);
}
});
}
//FullScreenUtilities.setWindowCanFullScreen(window, true);
private void enableOSXFullscreen(Window window) {
try {
Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
Class params[] = new Class[]{Window.class, Boolean.TYPE};
Method method = util.getMethod("setWindowCanFullScreen", params);
method.invoke(util, window, true);
} catch (ClassNotFoundException | NoSuchMethodException |
SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException exp) {
exp.printStackTrace(System.err);
}
}
//Application.getApplication().requestToggleFullScreen(window);
private void toggleOSXFullscreen(Window window) {
try {
Class application = Class.forName("com.apple.eawt.Application");
Method getApplication = application.getMethod("getApplication");
Object instance = getApplication.invoke(application);
Method method = application.getMethod("requestToggleFullScreen", Window.class);
method.invoke(instance, window);
} catch (ClassNotFoundException | NoSuchMethodException |
SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException exp) {
exp.printStackTrace(System.err);
}
}
//Application.getApplication().setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
private void enableOSXQuitStrategy() {
try {
Class application = Class.forName("com.apple.eawt.Application");
Method getApplication = application.getMethod("getApplication");
Object instance = getApplication.invoke(application);
Class strategy = Class.forName("com.apple.eawt.QuitStrategy");
Enum closeAllWindows = Enum.valueOf(strategy, "CLOSE_ALL_WINDOWS");
Method method = application.getMethod("setQuitStrategy", strategy);
method.invoke(instance, closeAllWindows);
} catch (ClassNotFoundException | NoSuchMethodException |
SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException exp) {
exp.printStackTrace(System.err);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new FullScreenTest()::display);
}
}
评论
另请参见在OSX上摇摆:如何捕获command-Q?
–垃圾神
16-3-29在1:50
快速说明:对requestToggleFullScreen的调用似乎在静默地失败,如果它是在使帧可显示之前进行的。 (在El Capitan 10.11.6上进行测试。)
– Radioodef
17年11月30日,0:22
评论
谢谢您的回答!您能否也提供与Java applet相同的示例?我希望示例成为一个Java applet,但似乎我忘了在第一篇文章中包括它了……我认为您只需对所提供的实际代码做些小改动/改动,如果您这样做的话,将非常感谢,因为您的榜样非常好学习!
–Vadim
2011-09-17 20:22
我建议使用JApplet,但为了安全起见,可能必须对其进行签名。您可以使用此处显示的混合方法来尝试它; java-web-start将是一个更好的部署策略。
–垃圾神
2011-09-17 21:04
谢谢,会检查的!
–Vadim
2011年9月17日下午21:09
@Vadim请注意,使用此技术stackoverflow.com/questions/14317352,OSX中的Java 1.7实际上存在一个错误。
– sage88
2013年9月29日9:16
另请参见全屏Swing组件在Mac OS X Mountain Lion的Java 7上无法接收键盘输入。
–垃圾神
15年5月18日在16:10