+ 1
Java Swing, как получить размер после manager layout-a?
Вставил панель с помощью border layout, он растянул панель на максимум, пытаюсь получить его размер, выдаëт ноль, не понимаю, в чëм причина.
5 Answers
+ 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.
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");
}
}
0
I am trying to get JPanel size (panel in this code) after using BorderLayout, is this possible?
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 :)
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);