我正在尝试在基于this code的Java APP中实现全屏模式
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("Mouse Click Demo");
//Make it non-Resizable
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new MouseEventDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
//frame.setVisible(true);
if (!frame.isDisplayable())
{
// Can only do this when the frame is not visible
frame.setUndecorated(true);
}
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
try
{
if(gd.isFullScreenSupported())
{
gd.setFullScreenWindow(frame);
System.out.print("DOES SUPPORT");
}
else
{
//Can't run in full screen mode
System.out.print("DOES NOT SUPPORT");
}
frame.setVisible(true);
}
finally
{
gd.setFullScreenWindow(null);
}
}
问题是,当我在Macbook上运行时,它仍显示顶部Mac栏和底部Deck,该控制台确实处于输入状态并打印DOES SUPPORT(支持),但仍未在全屏模式下显示。
我怎么了
编辑:This is my Screen Shot
最佳答案
回应您的Escape评论...
您最好的选择是使用Key Bindings
原因很多,但主要的原因是它没有受到KeyListener
所引起的排他性焦点问题的困扰
public class TickleMe {
public static void main(String[] args) {
new TickleMe();
}
public TickleMe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
JPanel content = new JPanel(new GridBagLayout());
content.add(new JLabel("Tickle Me"));
InputMap im = content.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = content.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "getMeOutOfHere");
am.put("getMeOutOfHere", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
try {
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gd.setFullScreenWindow(null);
} finally {
frame.dispose();
}
}
});
frame.setContentPane(content);
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (gd.isFullScreenSupported()) {
gd.setFullScreenWindow(frame);
frame.requestFocusInWindow();
}
}
});
}
}