2015-12-09 51 views
0

我想将JFrame置于全屏并将显示模式更改为1280 * 720,但JFrame不是全屏。无法用全屏独占模式获得全屏

pic

这里是我的代码

JFrame f = new JFrame("Test"); 
f.setUndecorated(true); 
f.setResizable(false); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

GraphicsDevice device = GraphicsEnvironment 
     .getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
if (device.isFullScreenSupported()) { 
    device.setFullScreenWindow(f); 
    if (device.isDisplayChangeSupported()) { 
     try { 
      DisplayMode dm = new DisplayMode(1280, 720, 32, 60); 
      device.setDisplayMode(dm); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     } else { 
     System.err.println("Change display mode not supported"); 
    } 
} else { 
    System.err.println("Full screen not supported"); 
} 
+0

工作对我来说,Windows 7中,Java的8看起来像你使用的是Windows 10? – MadProgrammer

+0

@MadProgrammer是的,我使用Windows 10,我试图在Windows 8上运行,但它不工作。 –

+0

@NoppawitThairungroj然后看看链接的“可能的重复”,这似乎是合理的选择 – MadProgrammer

回答

1

我怀疑,你的显卡和/或视频驱动程序和/或监视器不能支持DisplayMode你正在尝试使用

可能优选使用由 GraphicsDevice#getDisplayModes列出的DisplayMode之一,例如...

DisplayMode[] modes = device.getDisplayModes(); 
for (DisplayMode mode : modes) { 
    System.out.println(mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate()); 
} 

其中,在我的机器上输出

640x480 32 @ 60 
640x480 32 @ 75 
720x480 32 @ 60 
720x480 32 @ 75 
720x576 32 @ 60 
720x576 32 @ 75 
800x600 32 @ 60 
800x600 32 @ 75 
1024x768 32 @ 60 
1024x768 32 @ 75 
1152x864 32 @ 75 
1280x720 32 @ 60 
1280x720 32 @ 75 
1280x768 32 @ 60 
1280x768 32 @ 75 
1280x800 32 @ 60 
1280x800 32 @ 75 
1280x960 32 @ 60 
1280x960 32 @ 75 
1280x1024 32 @ 60 
1280x1024 32 @ 75 
1360x768 32 @ 60 
1366x768 32 @ 60 
1600x900 32 @ 60 
1600x1024 32 @ 60 
1600x1200 32 @ 60 
1680x1050 32 @ 59 
1680x1050 32 @ 60 
1920x1080 32 @ 59 
1920x1080 32 @ 60 
1920x1200 32 @ 59 
1920x1200 32 @ 60 

正如你所看到的,1280x720 32 @ 60被列为可用的模式之一,你的代码工作正常,我的机器上而无需修改。

我曾尝试使用DisplayMode dm = new DisplayMode(1280, 720, DisplayMode.BIT_DEPTH_MULTI, DisplayMode.REFRESH_RATE_UNKNOWN);,但它与java.lang.IllegalArgumentException: Invalid display mode

失败的话,那我想,妈的,我会挑选出“最”有可能比赛,只是尝试,直到一个棒...

try { 
    List<DisplayMode> matchingModes = new ArrayList<>(25); 

    DisplayMode[] modes = device.getDisplayModes(); 
    for (DisplayMode mode : modes) { 
     if (mode.getWidth() == 1280 && mode.getHeight() == 720) { 
      matchingModes.add(mode); 
     } 
    } 

    if (!matchingModes.isEmpty()) { 
    for (DisplayMode mode : matchingModes) { 
     try { 
      device.setDisplayMode(mode); 
      System.out.println(mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate()); 
      break; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    } else { 
     System.err.println("!! No matching modes available"); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

哪个结束使用1280x720, 32 @ 60。现在,我还以为你也许可以在列表中位深度的进行排序和刷新率,但我会留给你来决定,并制定出

这基本上是我的测试代码...

import java.awt.DisplayMode; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class JavaApplication155 { 

    public static void main(String[] args) { 
     JFrame f = new JFrame("Test"); 
     f.setUndecorated(true); 
     f.add(new TestPane()); 
     f.setResizable(false); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     GraphicsDevice device = GraphicsEnvironment 
         .getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
     if (device.isFullScreenSupported()) { 
      device.setFullScreenWindow(f); 
      if (device.isDisplayChangeSupported()) { 
       try { 
        List<DisplayMode> matchingModes = new ArrayList<>(25); 

        DisplayMode[] modes = device.getDisplayModes(); 
        for (DisplayMode mode : modes) { 
         if (mode.getWidth() == 1280 && mode.getHeight() == 720) { 
          matchingModes.add(mode); 
         } 
        } 

        if (!matchingModes.isEmpty()) { 
         for (DisplayMode mode : matchingModes) { 
          try { 
           device.setDisplayMode(mode); 
           System.out.println(mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate()); 
           break; 
          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
         } 
        } else { 
         System.err.println("!! No matching modes available"); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } else { 
       System.err.println("Change display mode not supported"); 
      } 
     } else { 
      System.err.println("Full screen not supported"); 
     } 
    } 

    public static class TestPane extends JPanel { 

     public TestPane() { 
      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 
        SwingUtilities.windowForComponent(TestPane.this).dispose(); 
       } 
      }); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      String text = getWidth() + "x" + getHeight(); 
      FontMetrics fm = g.getFontMetrics(); 
      int x = (getWidth() - fm.stringWidth(text))/2; 
      int y = (getHeight() - fm.getHeight())/2; 
      g.drawString(text, x, y + fm.getAscent()); 

      GraphicsDevice device = GraphicsEnvironment 
          .getLocalGraphicsEnvironment().getDefaultScreenDevice(); 

      DisplayMode mode = device.getDisplayMode(); 
      text = mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate(); 
      x = (getWidth() - fm.stringWidth(text))/2; 
      y += fm.getHeight(); 
      g.drawString(text, x, y + fm.getAscent()); 
     } 

    } 

} 

正如the Display Mode trail

提到当你的应用程序选择显示模式,您可能希望保留喜欢的显示模式的列表,然后选择从可用的显示模式列表中最好的之一。

测试在Windows 10,Java的8