+ 1

Java Swing, как получить размер после manager layout-a?

Вставил панель с помощью border layout, он растянул панель на максимум, пытаюсь получить его размер, выдаëт ноль, не понимаю, в чëм причина.

16th Dec 2022, 8:57 AM
Дмитрий Нуйков
Дмитрий Нуйков - avatar
5 Antworten
+ 1
Are you trying to get the size of your frame? are you using the pack method instead of setSize? Post your code here or in private to me, would be easier to help.
17th Dec 2022, 3:03 AM
JavaBobbo
JavaBobbo - avatar
0
import static java.awt.Frame.MAXIMIZED_BOTH; import java.awt.*; import javax.swing.*; class Polygon { public static void main(String[] args){ JFrame window = new JFrame(); window.setSize(500,500); window.getContentPane().setBackground(Color.white); window.setExtendedState(MAXIMIZED_BOTH); window.setVisible(true); window.getContentPane().setLayout(new BorderLayout()); JPanel panel = new JPanel(); panel.setBackground(Color.gray); panel.setPreferredSize(new Dimension(20,20)); Dimension panelSize = panel.getSize(); System.out.println("panel width - " + panelSize.width); System.out.println("panel height - " + panelSize.height); window.add(panel, "North"); } }
17th Dec 2022, 8:00 AM
Дмитрий Нуйков
Дмитрий Нуйков - avatar
0
I am trying to get JPanel size (panel in this code) after using BorderLayout, is this possible?
17th Dec 2022, 8:05 AM
Дмитрий Нуйков
Дмитрий Нуйков - avatar
0
You can use panel.setPreferredSize(new Dimension(0,100)); to set the height of the panel, if it is that you are looking for :)
17th Dec 2022, 7:46 PM
JavaBobbo
JavaBobbo - avatar
0
Sorry i misunderstood you.. First change to this Dimension panelSize = panel.getPreferredSize(); then to this System.out.println("panel width - " + panelSize.getSize().width); System.out.println("panel height - " + panelSize.getSize().height);
17th Dec 2022, 7:58 PM
JavaBobbo
JavaBobbo - avatar