每次我觉得自己已经学到了很多关于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)
答案 0 :(得分:2)
在尝试在代码中执行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;