user6009023
user6009023

Reputation:

How do I remove the empty space in a JFrame?

I'm relatively new to Java GUI programming, and although the concepts are coming along well in my head, there's this small issue I'm having with removing the empty space in a JFrame.

If you run the code I have below, you'll notice that there's a considerable gap between the JTextFields and the JButton. I'd like the remove this gap so that the button is touching the very bottom JTextField.

Also, another very minor question. How do I increase the height of the JTextFields so that they have three rows instead of just one?

Anyway here's the code.

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


public class HW4GUI extends JFrame
{

private JButton jbtAction;
private JTextField jtfFName;
private JTextField jtfLName;
private JTextField jtfLibNo;
private static int nextLibNo;
private JPanel textPanel;

public HW4GUI()
{
    super("HW4GUI");
    makeFrame();
    showFrame();
}

public void makeFrame()
{
    setLayout(new BorderLayout());
    setResizable(false);

    textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(3,2));

    jbtAction = new JButton("Add Borrower");    

    JLabel FirstNameLabel = new JLabel("FirstName:");
    jtfFName = new JTextField(3);

    JLabel LastNameLabel = new JLabel("LastName:");
    jtfLName = new JTextField(3);

    JLabel LibNoLabel = new JLabel("Library Number:");
    jtfLibNo = new JTextField(3);

    FirstNameLabel.setHorizontalAlignment(JTextField.RIGHT);
    LastNameLabel.setHorizontalAlignment(JTextField.RIGHT);
    LibNoLabel.setHorizontalAlignment(JTextField.RIGHT);

    jtfLibNo.setEditable(false);

    textPanel.add(FirstNameLabel);
    textPanel.add(jtfFName);
    textPanel.add(LastNameLabel);
    textPanel.add(jtfLName);
    textPanel.add(LibNoLabel);
    textPanel.add(jtfLibNo);

    add(textPanel, BorderLayout.NORTH);
    add(jbtAction, BorderLayout.SOUTH);
}

public void showFrame()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setSize(400,200);
    setVisible(true);
}

public void actionPerformed(ActionEvent e)
{

}
}

Upvotes: 0

Views: 1001

Answers (1)

Spork
Spork

Reputation: 48

Both of these are very easy fixes. For the first question, just set the second to "CENTER" instead of "SOUTH", and for the second question you can make it a JTextarea, which is multiple lines, or a JScrollPane which can scroll so it has a very large size. P.S. you should always include a main method to run from like this one below.

public static void main(String[] args){
new HW4GUI();
}

Upvotes: 2

Related Questions