3

My code works fine, as in it does what it's supposed to, (thanks to the help of this website!) but I forgot one detail: I'm missing the borders around my panels. Thing is, I don't know how to do it, when I assign a label to a panel it doesn't surround all the components, but rather appears in a very weird form. I do understand why it happens, but I don't know how to fix it and how to get what I want. What I need:

enter image description here

but what I get:

enter image description here

import javax.swing.*;
import javax.swing.border.TitledBorder;

import java.awt.event.*;
import java.awt.*;

public class HW2BorderLayoutSettings extends JFrame {
private JPanel buttonPanel = new JPanel();
private JPanel propertiesPanel = new JPanel();
private JLabel label1 = new JLabel();
private JLabel label2 = new JLabel();
private JButton north = new JButton("North");
private JButton south = new JButton("South");
private JButton west = new JButton("West");
private JButton east = new JButton("East");
private JButton center = new JButton("Center");
private BorderLayout border = new BorderLayout();
private JLabel label = new JLabel("BorderLayout Properties:");
private JLabel hGapLabel = new JLabel("HGap:");
private JLabel vGapLabel = new JLabel("VGap:");
private JTextField hGapField = new JTextField();
private JTextField vGapField = new JTextField();
private GridLayout grid = new GridLayout(2, 2);
private Integer hGapInt;
private Integer vGapInt;

public HW2BorderLayoutSettings() {
    buttonPanel.setLayout(border);
    buttonPanel.add(north, BorderLayout.NORTH);
    buttonPanel.add(center, BorderLayout.CENTER);
    buttonPanel.add(south, BorderLayout.SOUTH);
    buttonPanel.add(west, BorderLayout.WEST);
    buttonPanel.add(east, BorderLayout.EAST);
    propertiesPanel.setLayout(grid);
    propertiesPanel.add(hGapLabel);
    propertiesPanel.add(hGapField);
    propertiesPanel.add(vGapLabel);
    propertiesPanel.add(vGapField);

    add(buttonPanel, BorderLayout.CENTER);
    add(propertiesPanel, BorderLayout.SOUTH);
    buttonPanel.add(label1);
    propertiesPanel.add(label2);

    label1.setHorizontalAlignment(JLabel.LEFT);
    label2.setHorizontalAlignment(JLabel.LEFT);
    TitledBorder titled1 = new TitledBorder("Container");
    label1.setBorder(titled1);
    TitledBorder titled2 = new TitledBorder("Properties");
    label2.setBorder(titled2);


    hGapField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            hGapInt = Integer.parseInt(hGapField.getText());
            border.setHgap(hGapInt);
            setSize((int) (getWidth() + hGapInt), getHeight());
            validate();
        }
    });

    vGapField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            vGapInt = Integer.parseInt(vGapField.getText());
            border.setVgap(vGapInt);
            setSize(getWidth(), (int) (getHeight() + vGapInt));
            validate();
        }
    });
}

public static void main(String[] args) {
    HW2BorderLayoutSettings borderDemo = new HW2BorderLayoutSettings();
    borderDemo.setTitle("Border Layout");
    borderDemo.setSize(400, 400);
    borderDemo.setLocationRelativeTo(null);
    borderDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    borderDemo.setVisible(true);

}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
imaginedrragon
  • 303
  • 3
  • 18
  • `private JLabel label1 = new JLabel()` should be `private JPanel panel1 = new JPanel()`. Same for the other one. Don't use labels as containers, since the label will only take its own text and icon into account when reporting a size. – Andrew Thompson Oct 26 '15 at 16:36
  • Overuse of TitledBorders is a common mistake made by programmers with little or no understanding of usability. You should use whitespace and indentation to group your fields, instead of drawing lines around them. The result will look much cleaner. – VGR Oct 26 '15 at 16:59

1 Answers1

2

Ah nevermind, I was complicating such a simple thing! Didn't need labels or new panels, just needed to add borders to existing ones!

TitledBorder titled1 = new TitledBorder("Container");
buttonPanel.setBorder(titled1);
TitledBorder titled2 = new TitledBorder("Properties");
propertiesPanel.setBorder(titled2);
imaginedrragon
  • 303
  • 3
  • 18