Mahesh
Mahesh

Reputation: 1

Unable to set button's location in java swing application

I am new to Java and was trying to develop a basic swing application. I wanted to set the location of the button on the JFrame. I tried to do this but was unable to do this this is my code. I am using eclipse for development

public class MyUI extends JFrame {

    JButton button1 = new JButton("Click");
    JTextField tb1 = new JTextField(5);
    JPanel panel1 = new JPanel();

    public MyUI() {
        super("Test");
        setVisible(true);
        this.setLayout(null);
        panel1.setLayout(null);
        panel1.setVisible(true);
        button1.setVisible(true);
        panel1.add(button1);
        add(panel1);
        panel1.setLocation(10, 10);
        button1.setLocation(10, 10);    
        setDefaultCloseOperation(EXIT_ON_CLOSE);    
        button1.addActionListener(this);
    }

    public static void main(String[] args) {
        MyUI gui = new MyUI();
        gui.setSize(400, 300);
    }
}

Upvotes: 0

Views: 3118

Answers (3)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Would like to suggest something, though its not the direct immediate answer to your question, but still its important from my point of view....

You can use Group Layout which was developed by NetBeans team back in 2005, its awesome to work with.... Try using the Windows Builder Pro which is provided by Google for free now... You can get your application up and running in no time......

Upvotes: -1

Goblin Alchemist
Goblin Alchemist

Reputation: 827

Your panel and button are not seen because they have zero size. Add something like:

panel1.setSize(100, 100);
button1.setSize(80, 30);

or use the setBounds method which is more convenient to set location and size simultaneously:

panel1.setBounds(10, 10, 100, 100);
button1.setBounds(10, 10, 80, 30);

Upvotes: 0

mKorbel
mKorbel

Reputation: 109813

1.why you put two JComponents to the same Bounds

panel1.setLocation(10, 10);
button1.setLocation(10, 10);  

2.have look at Initials Thread

3.public class MyUI extends JFrame {

should be

public class MyUI extends JFrame implements ActionListener{ 

4.don't extend JFrame, create a local variable

5.setVisible(true); should be (in this form) only last code line into MyUI() constructor

6.setVisible(true); is important issue, you visibled JFrame and then to add JComponent(s)

7.don't use NullLayout, use proper LayoutManager, in the case that you remove this.setLayout(null); and panel1.setLayout(null); added JComponents could be visible

8.use pack() before setVisible(true) as last two code lines in constructor

EDIT (by using built_in LayoutManagers, BorderLayout for JFrame and FlowLayout for JPanel)

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

public class MyUI extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JButton button1 = new JButton("Click");
    private JTextField tb1 = new JTextField(5);
    private JPanel panel1 = new JPanel();

    public MyUI() {
        super("Test");
        panel1.add(tb1);
        panel1.add(button1);
        add(panel1);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        button1.addActionListener(this);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyUI testing = new MyUI();
            }
        });
    }
}

Upvotes: 2

Related Questions