GridBagLayout constraints not working?

Here is my code that uses GridbagLayout

public IntroPanel(){
    JPanel intro = new JPanel(new GridBagLayout());
    JLabel label1 = new JLabel("Test test test");
    label1.setFont(new Font("Helvetica", Font.PLAIN, 40));
    label1.setHorizontalAlignment(SwingConstants.LEFT);
    JLabel label2 = new JLabel("test2 test2 test2");

    setPreferredSize(new Dimension(640,480));
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = 10;
    gbc.gridy = 10;
    gbc.weightx = 0;
    add(label1, gbc);

    gbc.gridx = 20;
    gbc.gridy = 10;
    add(label2, gbc);
}

I changed all the gridx, gridy, GridBagConstraints to align them but nothing is working. NORTHEAST for grid constraints is not working either. Can anyone help? Learning how to create my own GUI..

Main coding is here

public static void main(String[] args) {
    JFrame frame = new JFrame("Title111111111");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tp = new JTabbedPane();

    tp.addTab("Intro", new IntroPanel());
    tp.addTab("Catalogue", new CataloguePanel());

    JPanel panel = new JPanel();
    panel.setBackground(Color.WHITE);

    frame.add(panel);
    frame.add(tp);
    frame.pack();

    frame.setVisible(true);
}

Answers


The line

add(label1, gbc);

is adding to this (the IntroPanel).

What you probably mean is this:

intro.add(label1, gbc);

EDIT:

Try adding setLayout(new GridBagLayout()); before the adds


Need Your Help

Preserve POST variables during login redirect in GAE?

python google-app-engine http-post

in a form, I submit data to a python webapp handler (all Google App Engine based) using a HTTP POST request. In this script, I first check if the user is logged in and if not, I use

Custom dimens.xml for mdpi devices - how it can be done?

android layout screen

My application have layout, which looks perfect at all devices, except devices with mdpi screen destiny. Activity just doesn't fit to screen on mdpi devices.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.