将定义的JFrame大小转换为根据屏幕大小自动调整大小

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

Converting defined Jframe size to auto-sizing based on screensize

问题

我正在开发一个Java项目,一个二进制时钟,同时还有来自API的天气统计数据。最初编写代码时,我犯了一个错误,将框架大小定义为我的屏幕特定分辨率。现在我换了一台屏幕尺寸不同的新笔记本电脑,意识到了我的错误,但不确定该如何修复(因为我的旧笔记本电脑是4K屏幕,而新笔记本电脑不是)。

所有组件的大小也都是基于我定义的面板和框架大小的固定大小,它们都是
.setSize(2000,1500);
我尝试了插入:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds(0, 0, screenSize.width, screenSize.height);

在我的框架中:

JFrame frame = new JFrame("12-Hour Binary Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(2000, 1500);
frame.getContentPane().setBackground(Color.DARK_GRAY.darker().darker().darker());
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

以及修改了主面板Panel,在这个面板中包含了所有组件:

JPanel Panel = new JPanel();
Panel.setLayout(null);
Panel.setSize(2000, 1500);
Panel.setBackground(null);
Panel.setLocation(400, 100);
Panel.setVisible(true);

修改为:

JPanel Panel = new JPanel();
Panel.setLayout(null);
Panel.setSize(screenSize.width, screenSize.height);
Panel.setBackground(null);
Panel.setLocation(400, 100);
Panel.setVisible(true);

将所有内容添加到Panel,然后将Panel添加到frame

// 在程序末尾将组件添加到面板,然后将面板添加到框架
Panel.add(inner);
Panel.add(labelOne); // JLabel 组件在 Panel 内
Panel.add(labelTwo);
Panel.add(labelFour);
Panel.add(labelEight);
frame.add(weather);
frame.add(Panel, BorderLayout.CENTER);

但似乎没有任何变化。我认为我对如何做这些操作并没有很好的掌握。欢迎任何提示,因为我肯定做得不对。

我不确定是否只需要调整Panelframe的大小,因为所有内容都在其中,但由于没有产生任何效果,似乎我需要做更多的调整。我是否需要调整所有组件的大小?

另外,不确定为什么我将Panel的大小设置为与frame相同,而不是直接将所有内容添加到frame。我在很长时间后重新访问了这段代码。

英文:

I am working on a java project, a binary clock along with weather stats from an API. When originally writing it I made the mistake of defining the frame size as the specific resolution of my screen. Now that I have gotten a new laptop with a different screen size I have recognized my mistake and I am unsure of how to fix it. (its all cut off since my old laptop was 4k and my new laptop is not).

All of the components are also a defined size based on the defined size of my Panel and frame size which are both
.setSize(2000,1500); I have tried to insert

           Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	       frame.setBounds(0,0,screenSize.width, screenSize.height);

in my frame:

               JFrame frame = new JFrame("12-Hour Binary Clock");
		       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		       frame.setSize(2000,1500);
		       frame.getContentPane().setBackground(Color.DARK_GRAY.darker().darker().darker());
		       frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

as well as modifying my main panel, Panel where all the components are inside of:

       JPanel Panel = new JPanel();
	   Panel.setLayout(null);
       Panel.setSize(2000, 1500);
       Panel.setBackground(null);
       Panel.setLocation(400, 100);
       Panel.setVisible(true);

to

       JPanel Panel = new JPanel();
	   Panel.setLayout(null);
       Panel.setSize(screenSize.width, screenSize.height);
       Panel.setBackground(null);
       Panel.setLocation(400, 100);
       Panel.setVisible(true);

Adding everything to Panel and Panel to frame:

   //adding components to panel and panel to frame at end of program
   Panel.add(inner);
   Panel.add(labelOne); //JLabel components inside Panel
   Panel.add(labelTwo);
   Panel.add(labelFour);
   Panel.add(labelEight);
   frame.add(weather);
   frame.add(Panel, BorderLayout.CENTER);	

but it did not seem to do anything.
I don't think I have a good grasp of how to do this. Any tips are appreciated considering I definitely am not doing this right.

I'm not sure if I just need to adjust the size of Panel and frame since that is where everything is inside of but since it did not do anything it seems I will have to do more. Will I need to adjust the size of all components?

Also not sure why I made Panel the same size as frame instead of just adding everything to frame. I'm revisiting this code after a long time.

答案1

得分: 1

因为您的布局是 null,组件在窗口大小更改时很可能不会自动调整大小。您要么需要手动移动和调整组件的大小以适应窗口,要么添加一个布局。

我建议根据您的GUI的复杂程度,使用 BorderLayout、GridLayout 或两者的组合。学习布局的一个好方法是使用 Oracle 的教程:

BorderLayout:
https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

GridLayout:
https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

英文:

Since your layout is null the components will most likely not resize themselves when the window changes size. You will either have to manually move and resize the components to fit the window or add a layout.

I suggest using either a BorderLayout, a GridLayout or a combination of the two, depending on how advanced your GUI is. A good way to learn the layouts is to use Oracles tutorials:

BorderLayout:
https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

GridLayout:
https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

huangapple
  • 本文由 发表于 2020年9月9日 12:50:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63804942.html
匿名

发表评论

匿名网友

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

确定