每当我觉得自己对Java有很多了解时,都会突然被一堵砖墙打中,这让我感觉自己像个白痴。今天最大的问题是CardLayout。至少我终于通过实例化字段列表中的NullPointerException摆脱了困扰我的可怕的buttonsCardPane。现在,我收到一条IllegalArgumentException: (wrong parent for Card Layout)消息。这是我当前删除不相关部分的程序:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;

public class UserManagement extends JDialog {

    /**
     *
     */
    private static final long serialVersionUID = 1814914240620323834L;
    private JTextField tfAddUserName;
    private JPasswordField pfAddPassword;
    private JPasswordField pfAddVerifyPassword;
    private JCheckBox cbAddWriteAccess;
    private JButton addUserButton;
    private JTextField tfDelUserName;
    private JPanel buttonsCardPane = new JPanel();
    private JPanel addUserButtonPanel;
    private JPanel delUserButtonPanel;
    private JPanel changePWPanel;
    private CardLayout cardLayout = new CardLayout(0, 0);

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UserManagement dialog = new UserManagement();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public UserManagement() {
        setResizable(false);
        setTitle("Add User to Database");
        setBounds(100, 100, 450, 310);
        getContentPane().setLayout(new MigLayout("", "[450px][trailing]", "[][]"));
        {
            final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
            tabbedPane.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    switch (tabbedPane.getSelectedIndex())
                    {
                    case 0: cardLayout.show(buttonsCardPane, "addUserButtonPanel"); break;
                    case 1: cardLayout.show(buttonsCardPane, "delUserButtonPanel"); break;
                    case 2: cardLayout.show(buttonsCardPane, "changePWPanel");
                    }

                }
            });
            getContentPane().add(tabbedPane, "cell 0 0 2 1,growx");

            {
                JPanel addUserPanel = new JPanel();
                addUserPanel.setOpaque(false);
                addUserPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
                tabbedPane.addTab("Add User", null, addUserPanel, null);
                addUserPanel.setLayout(new MigLayout("", "[][grow]", "[][][][]"));
                {
                    JLabel lblAddUserName = new JLabel("User Name:");
                    lblAddUserName.setHorizontalAlignment(SwingConstants.TRAILING);
                    addUserPanel.add(lblAddUserName, "cell 0 0,alignx trailing");
                }
                {
                    tfAddUserName = new JTextField();
                    tfAddUserName.addFocusListener(new FocusAdapter() {
                        @Override
                        public void focusLost(FocusEvent e) {
                            tfAddUserName.setText(tfAddUserName.getText().toLowerCase());
                        }
                    });
                    tfAddUserName.setColumns(10);
                    addUserPanel.add(tfAddUserName, "cell 1 0,growx");
                }
                {
                    JLabel lblAddPassword = new JLabel("Password:");
                    lblAddPassword.setHorizontalAlignment(SwingConstants.TRAILING);
                    addUserPanel.add(lblAddPassword, "cell 0 1,alignx trailing");
                }
                {
                    pfAddPassword = new JPasswordField();
                        // Removed for brevity.
                    addUserPanel.add(pfAddPassword, "cell 1 1,growx");
                }
                {
                    JLabel lblAddVerifyPassword = new JLabel("Verify password:");
                    lblAddVerifyPassword.setHorizontalAlignment(SwingConstants.TRAILING);
                    addUserPanel.add(lblAddVerifyPassword, "cell 0 2,alignx trailing");
                }
                {
                    pfAddVerifyPassword = new JPasswordField();
                    pfAddVerifyPassword.setBackground(Color.PINK);
                        // Removed for brevity.
                    addUserPanel.add(pfAddVerifyPassword, "cell 1 2,growx");
                }
                {
                    JLabel lblAddWriteAccess = new JLabel("Write Access:");
                    lblAddWriteAccess.setHorizontalAlignment(SwingConstants.TRAILING);
                    addUserPanel.add(lblAddWriteAccess, "cell 0 3,growx");
                }
                {
                    cbAddWriteAccess = new JCheckBox("");
                    addUserPanel.add(cbAddWriteAccess, "cell 1 3");
                }
            }
            {
                JPanel deleteUserPanel = new JPanel();
                deleteUserPanel.setOpaque(false);
                deleteUserPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
                tabbedPane.addTab("Delete User", null, deleteUserPanel, null);
                deleteUserPanel.setLayout(new MigLayout("", "[][grow]", "[]"));
                {
                    JLabel lblDelUserName = new JLabel("User Name:");
                    lblDelUserName.setHorizontalAlignment(SwingConstants.TRAILING);
                    deleteUserPanel.add(lblDelUserName, "cell 0 0,alignx trailing");
                }
                {
                    tfDelUserName = new JTextField();
                    tfDelUserName.setColumns(10);
                    deleteUserPanel.add(tfDelUserName, "cell 1 0,growx");
                }
            }
        }
        {
//          buttonsCardPane = new JPanel();
            buttonsCardPane.setLayout(cardLayout);
            getContentPane().add(buttonsCardPane, "cell 0 1,alignx trailing,aligny bottom");
            {
                addUserButtonPanel = new JPanel();
                addUserButtonPanel.setName("addUserButtonPanel");
                addUserButtonPanel.setLayout(new MigLayout("insets 0", "[grow,trailing]", "[29px]"));
                buttonsCardPane.add(addUserButtonPanel, "addUserButtonPanel");
                {
                    addUserButton = new JButton("Add User");
                    addUserButton.setEnabled(false);
                    addUserButton.setActionCommand("OK");
                    addUserButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            // Removed for brevity
                        }
                    });
                }
                addUserButtonPanel.add(addUserButton, "cell 0 0,alignx trailing,aligny top");
                getRootPane().setDefaultButton(addUserButton);
            }
            {
                delUserButtonPanel = new JPanel();
                delUserButtonPanel.setName("delUserButtonPanel");
                buttonsCardPane.add(delUserButtonPanel, "delUserButtonPanel");
                delUserButtonPanel.setLayout(new MigLayout("insets 0", "[grow,trailing]", "[]"));
                {
                    JButton delUserButton = new JButton("Delete User");
                    delUserButton.setEnabled(false);
                    delUserButton.setActionCommand("OK");
                    delUserButtonPanel.add(delUserButton, "cell 0 0,alignx trailing");
                }
            }
            {
                changePWPanel = new JPanel();
                changePWPanel.setName("changePWPanel");
                buttonsCardPane.add(changePWPanel, "changePWPanel");
                changePWPanel.setLayout(new MigLayout("insets 0", "[grow,trailing]", "[]"));
                {
                    JButton updatePWbutton = new JButton("Update Password");
                    updatePWbutton.setEnabled(false);
                    updatePWbutton.setActionCommand("OK");
                    changePWPanel.add(updatePWbutton, "cell 0 0,alignx trailing");
                }
            }
        }
        {
            JButton closeButton = new JButton("Close");
            getContentPane().add(closeButton, "cell 1 1,growx");
            closeButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // Removed for brevity
                }
            });
            closeButton.setActionCommand("Cancel");
        }
    }

}


我怀疑这个问题非常简单,但是我对与CardLayout的处理实在太累了,以至于看不到它。

编辑:Stacktrace,根据要求:

java.lang.IllegalArgumentException: wrong parent for CardLayout
    at java.awt.CardLayout.checkLayout(CardLayout.java:404)
    at java.awt.CardLayout.show(CardLayout.java:526)
    at info.chrismcgee.sky.UserManagement$1.stateChanged(UserManagement.java:84)
    at javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:416)
    at javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:270)
    at javax.swing.DefaultSingleSelectionModel.fireStateChanged(DefaultSingleSelectionModel.java:132)
    at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(DefaultSingleSelectionModel.java:67)
    at javax.swing.JTabbedPane.setSelectedIndexImpl(JTabbedPane.java:616)
    at javax.swing.JTabbedPane.setSelectedIndex(JTabbedPane.java:591)
    at javax.swing.JTabbedPane.insertTab(JTabbedPane.java:731)
    at javax.swing.JTabbedPane.addTab(JTabbedPane.java:767)
    at info.chrismcgee.sky.UserManagement.<init>(UserManagement.java:97)
    at info.chrismcgee.sky.UserManagement.main(UserManagement.java:62)

最佳答案

在尝试在代码中执行CardLayout功能之前,需要将布局管理器设置为CardLayout。即

 ....
 case 0: cardLayout.show(buttonsCardPane, "addUserButtonPanel"); break;
 ....
 buttonsCardPane.setLayout(cardLayout);


改成

 ....
 setBounds(100, 100, 450, 310);
 buttonsCardPane.setLayout(cardLayout);
 getContentPane().setLayout(new MigLayout("", "[450px][trailing]", "[][]"));
 ....
 case 0: cardLayout.show(buttonsCardPane, "addUserButtonPanel"); break;

关于java - CardLayout喜欢向我抛出异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25125691/

10-10 22:10