Reputation: 294
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.
Upvotes: 0
Views: 47
Reputation: 11
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
Upvotes: 1