简体   繁体   中英

JFrame Positioning components using GridBagLayout

I'm trying to position several components using the GridBagLayout, but I'm a bit confused. When I try positioning them they don't move and just create several columns. I have tried using null layout but get punished every time.

Here's my code:

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

public class Main
{

    JFrame window = new JFrame("PE Fixture");   //JFrame variables
    JPanel container = new JPanel();
    JPanel guestFixturesPanel = new JPanel();
    JLabel guestFixturesTitle = new JLabel("FIXTURES");
    JButton loginButton = new JButton("Login");
    JSeparator southBar1 = new JSeparator(JSeparator.HORIZONTAL);
    JSeparator southBar2 = new JSeparator(JSeparator.HORIZONTAL);
    JTable listTable = new JTable();

    CardLayout cardLayout = new CardLayout();
    GridLayout layout = new GridLayout();

    public Main()
    {

        container.setLayout(cardLayout);

        guestFixturesPanel.setLayout(layout);        //GUEST FIXTURES PANEL COMPONENTS
        GridBagConstraints c = new GridBagConstraints();

            guestFixturesTitle.setBounds(120, 20, 500, 50);
            guestFixturesTitle.setFont(new Font("TimesRoman", Font.PLAIN, 50));
            guestFixturesTitle.setForeground(Color.WHITE);
            c.fill = GridBagConstraints.PAGE_START;
            c.weightx = 0.5;
            c.gridx = 1;
            c.gridy = 0;
            guestFixturesPanel.add(guestFixturesTitle, c);
            southBar1.setBounds(0, 760, 500, 10);
            northBar1.setBounds(0, 80, 500, 10);

            listTable = new JTable(data, columns)
            {
                public boolean isCellEditable(int data, int columns)        //Makes cells editable false by anyone
                {
                    return false;
                }

            };
            listTable.setPreferredScrollableViewportSize(new Dimension(450, 750));
            listTable.setFillsViewportHeight(false);
            listTable.setBounds(22, 100, 450, 640);
            JScrollPane scroll = new JScrollPane(listTable);
            listTable.setBackground(Color.LIGHT_GRAY);
            c.fill = GridBagConstraints.CENTER;
            guestFixturesPanel.add(scroll, c);
            loginButton.setBounds(330, 770, 150, 50);
            c.fill = GridBagConstraints.LAST_LINE_END;
            guestFixturesPanel.add(loginButton, c);

            container.add(guestFixturesPanel, "2");  
            cardLayout.show(container, "1");

        window.add(container);          //Creates the frame of the program.
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(500, 860);
        window.setResizable(false);
        window.setVisible(true);

    }

    public static void main(String[] args)
    {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main();
            }
        });

    }
}

The main problem is here:

GridLayout layout = new GridLayout();
...
guestFixturesPanel.setLayout(layout);

You're using GridLayout instead of GridBagLayout . Consequently when you add a component to guestFixturesPanel the layout manager is ignoring the constraint. For instance here:

guestFixturesPanel.add(guestFixturesTitle, c); // c is completely ignored

Additionaly, about this:

guestFixturesTitle.setBounds(120, 20, 500, 50);

Never-ever mix layout managers with setBounds() . These are two completely incompatible things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM