简体   繁体   English

Java屏幕分辨率大小

[英]Java Screen Resolution Size

I need some direction on this project that I am creating. 我需要有关正在创建的项目的一些指导。 I am currently trying to create a JFrame that automatically opens to the resolution of the screen. 我目前正在尝试创建一个JFrame,它会自动打开到屏幕分辨率。 I have looked online for help with this aspect of the project and I have found multiple ways to do it. 我在网上寻求有关该项目这一方面的帮助,并且发现了多种实现方法。 After doing even more research on the methods through the API and reference code of how to use the methods I have put together some code. 在通过API对方法进行了更多研究之后,以及如何使用这些方法的参考代码,我整理了一些代码。 When I run my program I get a window that pops up like this... (not full screen...) 运行程序时,我会看到一个这样弹出的窗口...(不是全屏显示...)

计划结果

I would appreciate some pointers on where I messed up, I would prefer help in the way of tips or tricks but I will accept code as well (sorry just trying to learn and not copy code). 我希望能得到一些有关我搞砸的地方的指针,我希望以技巧或窍门的方式提供帮助,但我也会接受代码(对不起,我只是尝试学习而不是复制代码)。 Thank you for your time. 感谢您的时间。

import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Frame;

public class Window {

private static int ScreenWidth;
private static int ScreenHeight;

public int getWidth(){
    return  ScreenHeight;
}

public int getLength(){
    return ScreenHeight;
}

public void WindowSetup(){
    Dimension UserScreen = Toolkit.getDefaultToolkit().getScreenSize();
    int ScreenWidth = (int) UserScreen.getWidth();
    int ScreenHeight = (int) UserScreen.getHeight();
}

static void CreateJframe(){
    JFrame gui  = new JFrame("Changeable Resolution");
    gui.setDefaultLookAndFeelDecorated(true);
    gui.setDefaultCloseOperation(gui.EXIT_ON_CLOSE);
    gui.setPreferredSize(new Dimension(ScreenWidth, ScreenHeight));
    gui.pack();
    gui.setVisible(true);

}


public static void main(String[] args) {
    Window window = new Window();
    Window.CreateJframe();      
}

} }

  1. You never call WindowSetup 您永远不会调用WindowSetup
  2. You assign the results of getScreenSize to local variables 您将getScreenSize的结果分配给局部变量
  3. Then you call pack ... pack uses the preferred size of the content, not the window, to make decisions about who best to size the window. 然后调用pack ... pack使用内容的首选大小(而不是窗口大小)来决定谁最适合调整窗口大小。

So that ain't going to work... 这样就行不通了...

The simplest way would be to use JFrame#setExtendedState and pass it JFrame.MAXIMIZED_BOTH 最简单的方法是使用JFrame#setExtendedState并将其传递给JFrame.MAXIMIZED_BOTH

JFrame gui = new JFrame("Changeable Resolution");
gui.setExtendedState(JFrame.MAXIMIZED_BOTH);
gui.setDefaultCloseOperation(gui.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);

Now, you might be saying, but I want to cover the taskbar... 现在,您可能会说,但是我想介绍一下任务栏...

In which case you could try something more like... 在这种情况下,您可以尝试更像...

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();

GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle bounds = gc.getBounds();

JFrame gui = new JFrame("Changeable Resolution");
gui.setSize(bounds.width, bounds.height);
gui.setLocation(bounds.x, bounds.y);
gui.setDefaultCloseOperation(gui.EXIT_ON_CLOSE);
gui.setVisible(true);

But you'll probably find that the task bar doesn't like been covered, in that case, you would need to resort to full screen exclusive mode... 但是您可能会发现任务栏不喜欢被遮盖,在这种情况下,您将需要采用全屏独占模式...

See Full-Screen Exclusive Mode API for more details... 有关更多详细信息,请参见全屏独占模式API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM