I am making an IRC client, and I'm using a GridBagLayout so that when the window is resized, the JTextArea that displays messages will resize too. But whenever I try and alter the numbers, the components are always off one side of the screen (picture: http://imgur.com/BoJ84pS). Here's the code:

setSize(750, 550);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBackground(Color.LIGHT_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{15, 680, 40, 15};
gbl_contentPane.rowHeights = new int[]{15, 480, 25, 30};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);

The picture is a bit unclear, but the JTextArea, JButton and JTextField are all partly outside of the window. BTW I am using the windowbuilder plugin to make the GUI.

有帮助吗?

解决方案

Don't set the size of the window. You can't possibly (or at least, easily) predicate the size of a window on all possible platforms.

Instead, add all you components to the window and then call pack which will size the window to the preferred size of its content

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top