0

I have a program but I can't combine the textfield and the button in the same frame like in top textfield and below that the button:

Here is my source code:

import java.awt.*;
import javax.swing.*;
public class FirstGui extends JFrame
{
     JTextField texts;
 JButton button;   

    public FirstGui()
    {
        texts = new JTextField(15);
        add(texts);
        button = new JButton("Ok");
        add(button);

    }
    public static void main(String args [])
    {
        FirstGui gui = new FirstGui();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(200,125);
        gui.setVisible(true);

    }
}
2
  • 1
    see: docs.oracle.com/javase/tutorial/uiswing/layout/visual.html Commented Feb 8, 2014 at 10:58
  • 1
    It is always a good idea to state what you expect to happen and what actually happens. Far better than "I can't" or "doesn't work". Commented Feb 8, 2014 at 11:39

2 Answers 2

2

Add a layout like FlowLayout:

public FirstGui()
{
    setLayout(new FlowLayout());
    texts = new JTextField(15);
    add(texts);
    button = new JButton("Ok");
    add(button);

}

at the very beginning of your constructor, before anything else.

Sign up to request clarification or add additional context in comments.

3 Comments

i already tried it but the error is java.lang.error: do not use FirstGui.setLayout() use FirstGui.getContentPane().setLayout()instead
Don't do FirstGui.setLayout(), that can't work. FirstGui refers to the class, not an instance. Just write setLayout(new FlowLayout()), as it is.
anything that can i try ?
2

The default layout is BorderLayout for JFrame. When you add components to a BorderLayout, if you don't specify their BorderLayout position, like BorderLayout.SOUTH, the component will automatically be add to BorderLayout.CENTER. Bthe thing is though, each position can only have one component. So when you add texts it gets added to the CENTER. Then when you add button, it gets added to the CENTER but texts gets kicked out. So to fix, you can do

add(texts, BorderLayout.NORTH);
add(button, BorderLayout.CENTER);

See Laying out Components Within a Container to learn more about layout managers.


UPDATE

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

public class FirstGui extends JFrame {

    JTextField texts;
    JButton button;

    public FirstGui() {
        texts = new JTextField(15);
        add(texts, BorderLayout.CENTER);
        button = new JButton("Ok");
        add(button, BorderLayout.SOUTH);

    }

    public static void main(String args[]) {
        FirstGui gui = new FirstGui();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.pack();
        gui.setVisible(true);

    }
}

11 Comments

same java.lang.Error :ccccccccccc
I don't know what you're doing wrong then, it works fine for me.
i know setLayout(new FlowLayout()); will work but ill get java.lang.error :((
sir im using JCreator .
Use an industry standard IDE like Netbeans or Eclipse. It's free to download
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.