0

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);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
kees mees
  • 11
  • 1
  • 1
  • 7

2 Answers2

5

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);
            }
        });
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

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);
Kristoff
  • 167
  • 3
  • 13
  • 2
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. Take a look at [Why is it frowned upon to use a null layout in SWING?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more details – MadProgrammer Nov 08 '14 at 22:13
  • Just because something may be frowned upon, doesn't mean it's not worth knowing about. And you can write code to dynamically handle positioning and sizing without using pre-made layouts. If Oracle didn't want anyone using it, it would have been deprecated. – Kristoff Nov 08 '14 at 22:20
  • 1
    So instead of using a layout manager, you'd write a layout manager, makes sense. In 15 years of professional development, I've never found a situation where I had to use a null layout manager, even written one or two layout managers of my own... – MadProgrammer Nov 08 '14 at 22:46
  • 1
    You are right it is not deprecated but highly discouraged. From [official Swing tutorial](https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html): *"Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales. Layout managers also can be reused easily by other containers, as well as other programs."* – dic19 Nov 08 '14 at 22:47
  • 1
    Myself, I've never used a premade layout, because I feel that when you code it yourself to be handled properly, you'll have a much better outcome than if you used a premade layout. I can see where you're coming from, but in the 8 years of myself programming, every language I've worked in, I handled resizing myself, and even restricted sizes that were too small to be practical. – Kristoff Nov 08 '14 at 22:57
  • So I take you never use 3rd party libraries, because you can do that yourself to? All it says to me (IMHO) is lack the understand of what the API is and how it can be used. You don't even need to use the inbuilt layout managers, you could use something like MigLayout, which is highly favoured for its high level of flexibility... – MadProgrammer Nov 09 '14 at 01:36
  • In most cases, I write my own libraries for future use. If I'm working with someone else's product (hardware/software), I'll implement the required API's. Unless it doesn't benefit me to write it myself, I'll use a libraries for certain things. It's just personal preference to know every single thing going on, and control it myself. But that's just me. – Kristoff Nov 09 '14 at 01:47
  • *"If Oracle didn't want anyone using it, it would have been deprecated."* What ***precisely*** would they deprecate? They are not about to deprecate a primitive like `null`, and 3rd party layouts such as `AbsoluteLayout` are beyond their control to deprecate. – Andrew Thompson Nov 09 '14 at 02:34