gpt4 book ai didi

java - CardLayout 喜欢向我抛出异常

转载 作者:行者123 更新时间:2023-12-02 05:34:39 26 4
gpt4 key购买 nike

每当我感觉自己已经学到了很多关于 Java 的知识时,我就会突然遇到一堵砖墙,让我感觉自己像个十足的白痴。今天的大问题是 CardLayout。至少我终于通过在字段列表中实例化 buttonsCardPane 摆脱了困扰我的可怕的 NullPointerException 。现在我收到一条 IllegalArgumentException: (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,以至于看不到它。

编辑:堆栈跟踪,根据要求:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com