是否可以更改 JTabbedPane 的布局?

Is it possible to change the layout of a JTabbedPane?

我有一个 JTabbedPane 的问题,我的 JPanel 之一有边框,我假设 JTabbedPane 有自己的布局,所以边框基本上根据JTabbedPane 的布局,如下所示:

然后我尝试使用 jTabbedPane.setLayout(new GridBagLayout()); 更改 JTabbedPane 的边框,尽管这完全搞砸了,结果是这样的:

我一直试图让它看起来像这样的方式(澄清一下,我试图实现内边框,而不是外边框。):

是否可以实际更改布局以适应我想要实现的目标,或者是否有更好的方法?

任何帮助将不胜感激,谢谢。如果我的问题需要改进或需要任何其他代码或说明,请告诉我。

代码:

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;

public class Test extends JFrame
{
    public Test()
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch(Exception e)
                {
                    e.printStackTrace();
                }

                setLayout(new GridBagLayout());

                add(new HomePanel());

                pack();
                setVisible(true);
                setLocationRelativeTo(null);
                setExtendedState(JFrame.MAXIMIZED_BOTH);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });
    }

    public static void main(String[] args)
    {
        Test t = new Test();
    }

    public class HomePanel extends JPanel
    {
        protected JTabbedPane tabbedPane;

        public HomePanel()
        {
            tabbedPane = new JTabbedPane();
            tabbedPane.setPreferredSize(new Dimension(800, 500));

            tabbedPane.addTab("Your Bookings", new ShowBookingPanel());

            add(tabbedPane);

        }
    }

    public class ShowBookingPanel extends JPanel
    {
        protected JTable bookingTable;
        protected JScrollPane scrollPane;


        public ShowBookingPanel()
        {
            TitledBorder titledBorder = new TitledBorder("Your Bookings");
            titledBorder.setTitleJustification(TitledBorder.CENTER);
            Border border = new CompoundBorder(titledBorder, new EmptyBorder(40, 50, 40, 50));

            setBorder(border);

            String[] columnNames = {"Check-In Date", "Check-Out Date", "Room Type", "Price", "Action"};
            Object[][] data = {{"22/09/1997", "TBA", "Single", "Priceless"}, {"12/03/2017", "23/04/2017", "Double", "£50"},
                {"12/03/2017", "23/04/2017", "Double", "£50"}, {"12/03/2017", "23/04/2017", "Double", "£50"},
                {"12/03/2017", "23/04/2017", "Double", "£50"}, {"12/03/2017", "23/04/2017", "Double", "£50"},
                {"12/03/2017", "23/04/2017", "Double", "£50"}, {"12/03/2017", "23/04/2017", "Double", "£50"}};

            TableModel model = new DefaultTableModel(data, columnNames)
            {
                public boolean isCellEditable(int row, int column)
                {
                    return false;
                }
            };

            bookingTable = new JTable(model);
            bookingTable.setPreferredScrollableViewportSize(new Dimension(500, 300));
            bookingTable.setFillsViewportHeight(true);
            bookingTable.getTableHeader().setReorderingAllowed(false);
            bookingTable.getTableHeader().setResizingAllowed(false);
          bookingTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            scrollPane = new JScrollPane(bookingTable);

            setLayout(new GridBagLayout());

            add(scrollPane);
        }
    }
}

变化:

        tabbedPane.addTab("Your Bookings", new ShowBookingPanel());

收件人:

        JPanel centeredPanel = new JPanel(new GridBagLayout());
        centeredPanel.add(new ShowBookingPanel());

        tabbedPane.addTab("Your Bookings", centeredPanel);

只要组件作为唯一组件添加,GridBagLayout 就会使组件居中,不会拉伸它,使用默认约束。