在Java Swing中手动设置分辨率 [英] Set resolution manually in Java Swing

查看:368
本文介绍了在Java Swing中手动设置分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来手动控制/拉伸整个GUI的大小.

I need a way to manually control/stretch the size of my whole GUI.

我有一个1400 x 1050桌面(本机分辨率),我想在代码中手动将分辨率缩放为1024 x 1050,因为我的应用程序是在1024 x 768桌面上编写的.因此,总体上,整个框架和所有按钮等都应该拉长/更大,并且关系应保持不变.

I have a 1400 x 1050 Desktop (native resolution) and I want to scale the resolution manually to 1024 x 1050 inside the code, because my application was written on/for a 1024 x 768 Desktop. So in general the whole frame and all buttons etc should be stretched / bigger as a whole and the relation should stay the same.

我无法通过Windows属性来执行此操作,因为由于使用其他应用程序,分辨率通常需要为1400 x 1050.

I can´t do it via Windows properties because the resolution in general needs to be 1400 x 1050 because of another application.

我的方法是这样的:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
screenHeight = screenSize.height;
screenWidth = screenSize.width; 

..然后通过setSize()更改屏幕尺寸?但是我不知道如何.

..and then change the screen size via setSize()? But I don´t understand how.

Java Tookit屏幕分辨率-如何使用它?

..很遗憾,此处答案中的链接不再起作用.

..unfortunately the link in the answer here does not work anymore.

如何在Java Swing中手动设置分辨率?

How to set resolution manually in Java Swing?

推荐答案

如注释中所述,最好的方法是修复基础代码以不强制显示1024 x 768的显示器.这通常是前端Java代码的非常独特代码的味道.正确使用布局管理器可以几乎总是使您的显示器以更灵活的方式工作.

As mentioned in the comments, the best approach here is to fix the underlying code to not mandate a 1024 x 768 display. This is usually a very distinct code smell of front-end Java code. Proper use of Layout Managers can almost always get your display to function in a more flexible manner.

但是,在工业中,有时进行重构以使旧组件正常运行 并不是可行的努力.在这种情况下,我建议您将无法重构的不灵活的GUI视为可以控制的较大GUI的组件.

However, in industry, sometimes refactoring to get legacy components functioning properly is not a feasible effort. In such a case, I would propose that you treat the inflexible GUI you can't refactor as a component of a larger GUI that you can control.

考虑以下示例代码:

public static void main(String[] args) {
  // Initialize the frame
  JFrame myApp = new JFrame("App");
  myApp.setSize(1400, 1050);

  // Create container for the GUI
  JPanel container = new JPanel(new BorderLayout());
  container.setPreferredSize(new Dimension(1024, 768));

  // Load the GUI into the container
  JComponent myGui = new JPanel(); // Replace this with actual GUI.
  myGui.setBackground(Color.RED); // Remove this once using actual GUI.
  container.add(myGui, BorderLayout.CENTER);

  // Create the frame's content pane
  JPanel content = new JPanel(new FlowLayout(FlowLayout.CENTER));
  content.setBackground(Color.BLUE); // Also remove this in production.

  // Add GUI to content pane
  content.add(container);

  // Add content pane to frame, show frame
  myApp.setContentPane(content);
  myApp.setVisible(true);
}

之所以可行,是因为我们已将GUI添加到BorderLayout面板的CENTER中(如果NORTH/SOUTH/EAST/WEST上没有任何组件,它将扩展GUI以占据面板的整个尺寸).我们将BorderLayout面板的首选大小设置为1024 x 768(即:指定GUI可以使用的大小),然后将该面板输入FlowLayout面板(这将保留首选大小)

This works because we've added the GUI to the CENTER of a BorderLayout panel (which will stretch the GUI to occupy the entire size of the panel in the absence of any components on the NORTH/SOUTH/EAST/WEST). We set the preferred size of that BorderLayout panel to be 1024 x 768 (IE: the size that the GUI is specified to work for), and then feed that panel into a FlowLayout panel (which will preserve the preferred size).

结果是我们的1400 x 1050应用程序包含我们的GUI组件的1024 x 768渲染(您可以通过修改包含BorderLayout的面板的首选大小来轻松地将其更改为1024 x 1050).

The result is that our 1400 x 1050 application contains a 1024 x 768 rendering of our GUI component (you can easily change this to 1024 x 1050 by modifying the preferred size of the panel containing the BorderLayout).

作为用户的练习,您会发现,如果运行此代码,则GUI代码不会垂直居中.这可以通过修改content面板的布局来解决.

As an exercise to the user, you'll notice that the GUI code isn't centered vertically if you run this. This can be tackled by modifying the layout of the content panel.

这篇关于在Java Swing中手动设置分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆