JFrame使用GridBagLayout定位组件

时间:2014-02-21 22:13:22

标签: java swing jframe layout-manager

我正在尝试使用GridBagLayout定位几个组件,但我有点困惑。当我尝试定位它们时,它们不会移动并只创建几个列。我尝试过使用null布局,但每次都会受到惩罚。

这是我的代码:

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

    }
}

1 个答案:

答案 0 :(得分:2)

主要问题在于:

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

您使用GridLayout代替GridBagLayout。因此,当您向guestFixturesPanel添加组件时,布局管理器会忽略该约束。例如:

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

另外,关于这个:

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

永远不要将布局管理器与setBounds()混合在一起。这是两个完全不相容的东西。