Hello Lovely People,

Please have a look at my GUI first, before reading the rest (I've attached a picture, see at the end of my post).

I'm having two problem with this GUI.

1) I don't want my Start button to be as long as it is. I want it to be about the half of if its current size and at the centre (as it is). how do I fix this?

2)As you can see, my text fields are quiet big as well. I would like to shorter both, in height and length (it should have enough room for 4 digits).

Here's My Code, Hope someone can help.
Thanks In Advance:)

package prjectilegui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


class ControlPanel extends JPanel
{
    //Fields
    private JLabel initialHeight, initialVelocity, initialAngle,empty1,empty2,empty3,empty4;
    public JRadioButton position, velocity, acceleration, energy;
    private ButtonGroup bg;
    public JTextField heightTextField, velocityTextField, angleTextField;
    private JButton start;
    private JPanel higherPanel, centerPanel, lowerPanel;

    //Default Constructor
    public ControlPanel()
    {
        setLayout(new GridLayout(3,1));

        // initializing Start Button
        start = new JButton("Start");
        start.addActionListener(new startButtonListener());

        // creating empty space to correctly place the start button
        empty3 = new JLabel("");
        empty4 = new JLabel("");
        
        // creating higher panel
        higherPanel = new JPanel(new GridLayout(3,1));
        higherPanel.add(empty3);
        higherPanel.add(start);
        higherPanel.add(empty4);

        // creating center and lower panels
        createCenterPanel();
        createLowerPanel();

        // adding panels to ControlPanel
        add(higherPanel);
        add(centerPanel);
        add(lowerPanel);
    }

    private void createCenterPanel()
    {
        // center panel
        centerPanel = new JPanel(new GridLayout(3,2));

        // initializing labels
        initialHeight = new JLabel("Initial Height     ");
        initialVelocity = new JLabel("Initial Velocity     ");
        initialAngle = new JLabel("Initial Angle     ");

        // initializing textfields
        heightTextField = new JTextField(2);
        velocityTextField = new JTextField(2);
        angleTextField = new JTextField(2);

        // adding elements to centerPanel
        centerPanel.add(initialHeight);
        centerPanel.add(heightTextField);
        centerPanel.add(initialVelocity);
        centerPanel.add(velocityTextField);
        centerPanel.add(initialAngle);
        centerPanel.add(angleTextField);
    }

    private void createLowerPanel()
    {
        lowerPanel = new JPanel(new GridLayout(6,1));

        // initializing radiobuttons
        position = new JRadioButton("Position");
        velocity = new JRadioButton("Velocity");
        acceleration = new JRadioButton("Acceleration");
        energy = new JRadioButton("Energy");

        // creating empty space
        empty1 = new JLabel("");
        empty2 = new JLabel("");
        // initializing buttongroup and add radiobutton to it
        bg = new ButtonGroup();
        bg.add(position);
        bg.add(velocity);
        bg.add(acceleration);
        bg.add(energy);

        // add radioButtons and empty space to lowerPanel
        lowerPanel.add(empty1);
        lowerPanel.add(position);
        lowerPanel.add(velocity);
        lowerPanel.add(acceleration);
        lowerPanel.add(energy);
    }

    //Start button listener
    private class startButtonListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }
}

Recommended Answers

All 4 Replies

If you really want fine control of your layout, while retaining the platform-independence and re-sizing ability, the GridBagLayout layout manager has just about anything you can imagine. There's a bit of a learning curve, but it's well worth the effort. Tutorials and reference materials on the web as usual.

Thank you James, I will look into GridBagLayout manager :-)

Thanks ztini, appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.