如何获取显示尺寸而非屏幕尺寸?

huangapple go评论67阅读模式
英文:

How to get the display size and not the screen size?

问题

我在互联网和StackOverflow上搜索过,并找到了许多获取当前屏幕大小的方法,但该屏幕大小并不是显示尺寸,是否有办法获取显示尺寸而不是屏幕大小?

我目前正在使用以下代码来获取屏幕大小:

//屏幕大小
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
frame.add(panel);

//获取屏幕大小
int height = (int) screenSize.getSize().getHeight();
int width = (int) screenSize.getSize().getWidth();
System.out.println("当前系统宽度和高度:" + screenSize.width + ", " + screenSize.height);
frame.setSize(width, height);

有人知道如何获取显示尺寸吗?还是说这实际上不太可能?

英文:

I've searched the internet and StackOverflow and found lots of ways to get the current screen size but<br> that screen size isn't the display size, is there a way to get the display size and not the screen size?<br><br>
I'm currently using the following lines of code to get the screen size:<br>

//size of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
frame.add(panel);

//get screensize
int height = (int) screenSize.getSize().getHeight();
int width = (int) screenSize.getSize().getWidth();
System.out.println(&quot;Current system width &amp; height: &quot; + screenSize.width + &quot;, &quot; + screenSize.height);
frame.setSize(width, height);

Does anyone know a way to get the display size? Or is it not really possible?

答案1

得分: 1

> 我想把一个按钮放在右上角

这也正是为什么我要求您提供实际需求,而不是您尝试的解决方案。您尝试的解决方案是错误的。

  1. 不要使用空布局。Swing 被设计用于与布局管理器一起使用。JFrame 的默认布局管理器是 BorderLayout。因此,您可以创建一个 JPanel,并使用带有右对齐的 FlowLayout(请阅读 API 获取正确的构造函数)。然后将按钮添加到面板,再将面板添加到框架中。

因此,要想将组件添加到顶部/右上角,基本逻辑如下:

JButton button = new JButton(...);
JPanel top = new JPanel(new FlowLayout(...));
top.add(button);
frame.add(top, BorderLayout.PAGE_START);

请阅读 Swing 教程中有关布局管理器的内容,以获取更多信息和使用布局管理器的示例。

  1. 不要使用 setSize() 方法。通常,您会在将所有组件添加到框架后使用 pack() 方法,框架将根据添加到框架的所有组件的首选大小调整大小。

但是,在这种情况下,您想要最大化框架,您可以使用以下代码:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);

在将所有组件添加到框架之后执行上述操作。

英文:

> I want to place a button on the top right

And that is exactly why I asked for your actual requirement, not your attempted solution. Your attempted solution is wrong.

  1. Don't use a null layout. Swing was designed to be used with layout managers. The default layout manager for a JFrame is the BorderLayout. So you can create a JPanel and use a FlowLayout with right alignment (read the API for the proper constructor. Then you add your button to the panel and the panel to the frame.

So to add a component to the top/right the basic logic would be:

JButton button = new JButton(...);
JPanel top = new JPanel( new FlowLayout(...) );
top.add(button);
frame.add(top, BorderLayout.PAGE_START);

Read the Swing tutorial on Layout Managers for more information and examples of using a layout manager.

  1. Don't use the setSize() method. Generally you would use the pack() method AFTER adding all components to the frame and the frame would size itself based on the preferred size of all components added to the frame.

However in this case you want the frame maximized to you would use:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);

AFTER adding all the components to the frame.

huangapple
  • 本文由 发表于 2020年10月27日 00:07:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64540830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定