JFrame 嵌套布局在线程错误中异常



所以我嵌套了多个布局,这些布局是BorderLayout内部的FlowLayout,它位于卡片布局内部。我有这么多布局的原因是因为我使用 FlowLayout 来对齐面板中的组件。然后使用边框布局将此面板添加到我的主框架中,我使用.NORTH等对齐它。然后,我将这个面板(包含边框布局内部的流布局的面板)添加到一个卡片中,这将允许我在面板之间切换。

当我将面板添加到卡时,我无法运行该程序并收到此错误:

"C:Program FilesJavajdk1.7.0_51binjava" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:Program Files (x86)JetBrainsIntelliJ IDEA Community Edition 13.0.2bin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.7.0_51jrelibcharsets.jar;C:Program FilesJavajdk1.7.0_51jrelibdeploy.jar;C:Program FilesJavajdk1.7.0_51jrelibjavaws.jar;C:Program FilesJavajdk1.7.0_51jrelibjce.jar;C:Program FilesJavajdk1.7.0_51jrelibjfr.jar;C:Program FilesJavajdk1.7.0_51jrelibjfxrt.jar;C:Program FilesJavajdk1.7.0_51jrelibjsse.jar;C:Program FilesJavajdk1.7.0_51jrelibmanagement-agent.jar;C:Program FilesJavajdk1.7.0_51jrelibplugin.jar;C:Program FilesJavajdk1.7.0_51jrelibresources.jar;C:Program FilesJavajdk1.7.0_51jrelibrt.jar;C:Program FilesJavajdk1.7.0_51jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.7.0_51jrelibextdnsns.jar;C:Program FilesJavajdk1.7.0_51jrelibextjaccess.jar;C:Program FilesJavajdk1.7.0_51jrelibextlocaledata.jar;C:Program FilesJavajdk1.7.0_51jrelibextsunec.jar;C:Program FilesJavajdk1.7.0_51jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.7.0_51jrelibextsunmscapi.jar;C:Program FilesJavajdk1.7.0_51jrelibextzipfs.jar;C:UsersHarryDesktopComputer Science ProjectsPE Fixtures v2.0outproductionPE Fixtures v2.0;C:Program Files (x86)JetBrainsIntelliJ IDEA Community Edition 13.0.2libidea_rt.jar" com.intellij.rt.execution.application.AppMain Main
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: unknown constraint: 2
    at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:463)
    at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:424)
    at java.awt.Container.addImpl(Container.java:1120)
    at java.awt.Container.add(Container.java:966)
    at Main.createAndShowGUI(Main.java:48)
    at Main.access$000(Main.java:8)
    at Main$2.run(Main.java:77)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

法典:

import javafx.geometry.HorizontalDirection;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
    public class Main
    {
        String[] columns = {"Sport", "Location", "Date", "Result"};
        String[][] data = {{"Football", "AQA Highschool", "12.11.13", "5 - 0"},
                {"Tennis", "Wembley", "26.11.14.", "TBC"}};
        private void createAndShowGUI()
        {
            CardLayout layout = new CardLayout();
            JPanel guestCard = new JPanel(layout);
            JPanel guestTitle = new JPanel(new FlowLayout(FlowLayout.CENTER));               //GUEST TOP PANEL
            JLabel fixturesLabel = new JLabel("FIXTURES");
            fixturesLabel.setFont(new Font("TimesRoman", Font.PLAIN, 50));
            fixturesLabel.setForeground(Color.WHITE);
            guestTitle.add(fixturesLabel);
            JTable fixturesTable = new JTable(data, columns)                                 //GUEST MID PANEL
            {
                public boolean isCellEditable(int data, int columns)
                {
                    return false;
                }
            };
            fixturesTable.setPreferredScrollableViewportSize(new Dimension(350, 450));
            fixturesTable.setFillsViewportHeight(true);
            JScrollPane scrollTable = new JScrollPane(fixturesTable);
            JPanel guestBot = new JPanel(new FlowLayout(FlowLayout.RIGHT));                  //GUEST BOT PANEL
            JButton loginButtonGuest = new JButton("Login");
            guestBot.add(loginButtonGuest);

            JPanel loginCard = new JPanel(layout);                                           //LOGIN TOP PANEL
            JPanel loginTitle = new JPanel(new FlowLayout(FlowLayout.CENTER));
            JLabel loginLabel = new JLabel("LOGIN");
            loginLabel.setFont(new Font("TimesRoman", Font.PLAIN, 50));
            loginLabel.setForeground(Color.WHITE);
            loginTitle.add(loginLabel);
            JPanel container = new JPanel(new BorderLayout(8,8));                           //ADDS CARDS TO CONTAINER
            container.add(guestCard, "2");
            container.add(loginCard, "3");
            guestCard.add(guestTitle, BorderLayout.NORTH);                                  //ADDS COMPONENTS TO CARDS
            guestCard.add(scrollTable, BorderLayout.CENTER);
            guestCard.add(guestBot, BorderLayout.SOUTH);
            loginCard.add(loginTitle, BorderLayout.NORTH);
            container.setBackground(Color.DARK_GRAY);                                       //COLOURS CARDS
            guestTitle.setBackground(Color.DARK_GRAY);
            scrollTable.setBackground(Color.DARK_GRAY);
            guestBot.setBackground(Color.DARK_GRAY);
            layout.show(container, "1");
            JFrame window = new JFrame("PE Fixtures");                                      //CREATES WINDOW
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.getContentPane().add(container);
            window.setSize(400, 700);
            window.setLocationRelativeTo(null);
            window.setResizable(false);
            window.setVisible(true);
        }
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Main().createAndShowGUI();
                }
            });
        }
    }

我现在已经修复了它。我只是让边框布局在卡片内,反之亦然。

相关内容

  • 没有找到相关文章

最新更新