2

I was wondering if anyone could give me any pointers on where to start with my assignment. I'm supposed to create a program that allows the user to input a value which would set the HGap or VGap between the buttons in BorderLayout. I've tried mapping it out on paper but I don't know if I should use a panel or a label, or how to implement the method of setting the gaps with a text area, or even how to extend the frame beyond the buttons (I have a code for the buttons but they fill out the entire space). This is what it's supposed to look like:

enter image description here

Similarly for VGap. Any help is greatly appreciated, but have in mind that I'm only a beginner. I only need suggestions and I'll try to update you with my coding, but for now I don't have anything.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
imaginedrragon
  • 303
  • 3
  • 18
  • Are you sure you are even using the assigned toolkit for the project and have appropriately tagged your question. The image in your question is a screenshot of a Swing UI, not a JavaFX UI. If you are writing a Swing application and not a JavaFX application, please update the tags in your question to reference the appropriate technology (or you are going to end up with answers that are not applicable and confuse you). – jewelsea Oct 23 '15 at 23:12
  • you're right it's Swing, i got confused on that part, thanks for pointing it out! – imaginedrragon Oct 23 '15 at 23:35
  • [*Java Swing: How to change GUI dynamically*](http://stackoverflow.com/q/5750068/230513) may give you some ideas. – trashgod Oct 24 '15 at 04:14

1 Answers1

2

The following code may give you an idea...

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class BorderDemo extends JFrame {
    private JPanel buttonPanel = new JPanel();
    private JPanel propertiesPanel = new JPanel();
    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 BorderDemo() {
        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);

        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) {
        BorderDemo borderDemo = new BorderDemo();
        borderDemo.setVisible(true);
        borderDemo.setSize(400, 400);
        borderDemo.setLocationRelativeTo(null);
        borderDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
ZpCikTi
  • 115
  • 1
  • 11
  • thanks man, this is exactly what i was looking for! i'd already given up because i had an idea but didn't know how to work with it, and this really saved me! – imaginedrragon Oct 25 '15 at 15:36