I want to make a JFrame to display a GUI like this: https://i.sstatic.net/2POFs.png (im not allowed to embed pictures)
the blank spcaces are the same X and Y dimentions, the buttons are the same X and Y dimentions, JTextArea is half of the frame.
public static JFrame frame = new JFrame ("Launcher " + version);
public static GridBagConstraints c = new GridBagConstraints();
public static JTextArea news = new JTextArea();
public static JButton launchGame = new JButton("Launch Game");
public static JButton settings = new JButton("Settings");
public static JButton exitGame = new JButton("Exit");
public static void main(String[] args)
{
news.setEditable(false);
news.setBounds(0, 0, (screenSize.width - 500) / 2, screenSize.height - 400);
news.append(" NEWS:\n");
news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));
frame.setLayout(new GridBagLayout());
frame.setBounds(300, 200, screenSize.width - 500, screenSize.height - 400);
c.fill = GridBagConstraints.VERTICAL;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
frame.add(news, c);
frame.setVisible(true);
c.fill = GridBagConstraints.EAST;
c.weightx = 0.5;
c.gridx = 1;
c.gridheight = 1;
frame.add(launchGame, c);
c.gridy = 1;
frame.add(settings, c);
c.gridy = 2;
frame.add(exitGame, c);
}
Is this what you want? I added/modified the following in your button constraints:
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 10, 0, 10); // 10 is the marginal
And here is the full code:
public static void main(String[] args)
{
news.setEditable(false);
news.append(" NEWS:\n");
news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));
frame.setLayout(new GridBagLayout());
c.fill = GridBagConstraints.VERTICAL;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
frame.add(news, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(10, 10, 10, 10); // 10 is the marginal
c.weightx = 0.5;
c.gridx = 1;
c.gridheight = 1;
frame.add(launchGame, c);
c.gridy = 1;
frame.add(settings, c);
c.gridy = 2;
frame.add(exitGame, c);
frame.pack();
frame.setVisible(true);
}
Here is a similar layout using BoxLayout
instead (easier to work with I think):
private static JComponent buildButtons() {
for (JButton button : Arrays.asList(launchGame, settings, exitGame)) {
button.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
}
Box b = Box.createVerticalBox();
b.add(Box.createVerticalStrut(10));
b.add(launchGame);
b.add(Box.createVerticalStrut(10));
b.add(settings);
b.add(Box.createVerticalStrut(10));
b.add(exitGame);
b.add(Box.createVerticalStrut(10));
return b;
}
static JComponent buildUI() {
Box b = Box.createHorizontalBox();
b.add(news);
b.add(Box.createHorizontalStrut(10));
b.add(buildButtons());
b.add(Box.createHorizontalStrut(10));
return b;
}
public static void main(String[] args)
{
news.setEditable(false);
news.append(" NEWS:\n");
news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));
frame.add(buildUI());
frame.pack();
frame.setVisible(true);
}