kees mees
kees mees

Reputation: 11

java change size of Textfield

I want to make a frame where you have to enter your name and stuff. I try to make it like, name: textfield, next line, age: textfield and so on... But the textfield is really high. If I add 2 textfields in one panel in the frame, one textfield contains half of my screen. I try to fix this with a layout manager but I have no idea how.

Could someone please help me?

Here's my code:

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

class GUI{
    public static void main(String [] args){
        GUI g = new GUI();
        g.Start();
    }

    public void Start(){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(640, 640);            
        JPanel panel = new JPanel();
        JPanel west = new JPanel();         
        JLabel name = new JLabel("Name: ");
        JLabel age = new JLabel("Age: ");           
        JTextField field1 = new JTextField();
        JTextField field2 = new JTextField();           
        west.setLayout(new BoxLayout(west, BoxLayout.Y_AXIS));
        west.add(name);
        west.add(age);      
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(field1);
        panel.add(field2);          
        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.getContentPane().add(BorderLayout.WEST, west);
        frame.setVisible(true);
    }
}

Upvotes: 0

Views: 21545

Answers (2)

Kristoff
Kristoff

Reputation: 187

Not sure which IDE you're working in, but I do know that with Eclipse, WindowBuilder Pro helps a lot with things like this.

What you're looking for is setBounds(x, y, width, height). Use this on your objects of the GUI.

x, y are the coordinates for the origin of the object (0,0) relative to the window we're working in. Origin is also in the top left of the window, and the further you go away from it in the x/y directions, the number increases. A little different from a typical graph in school.

Here's a piece of code from one of my GUI's to act as an example.

final TextField textField_1 = new TextField();
textField_1.setBounds(10, 231, 370, 22);
panel.add(textField_1);

Upvotes: -1

MadProgrammer
MadProgrammer

Reputation: 347334

The combination of BoxLayout and BorderLayout are working against you, consider using something like GridBagLayout instead.

Have a look at How to Use GridBagLayout for more details

GridBagLayout

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LayoutTest {

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

    public LayoutTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JPanel form = new JPanel(new GridBagLayout());

                JLabel name = new JLabel("Name: ");
        JLabel age = new JLabel("Age: ");

        JTextField field1 = new JTextField(10);
        JTextField field2 = new JTextField(3);

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.anchor = GridBagConstraints.WEST;

                form.add(name, gbc);
                gbc.gridy++;
                form.add(age, gbc);

                gbc.gridx++;
                gbc.gridy = 0;
                form.add(field1, gbc);
                gbc.gridy++;
                form.add(field2, gbc);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(form);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Upvotes: 5

Related Questions