java 更改屏幕分辨率,在Java Swing中手动设置分辨率

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

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.

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

My approach was something like:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

screenHeight = screenSize.height;

screenWidth = screenSize.width;

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

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

How to set resolution manually in Java Swing?

解决方案

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.

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.

Consider the following example code:

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);

}

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).

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).

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值