我正在制作一些简单的JFrame东西。我使用cardlayout。我得到了一些错误。我还在寻找问题所在。需要6个多小时。我从eclipse得到了这个错误信息。我使用netbeans来制作GUI。
"Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout
at java.awt.CardLayout.checkLayout(CardLayout.java:404)
at java.awt.CardLayout.show(CardLayout.java:526)
at RestaurantDemo.actionPerformed(RestaurantDemo.java:379)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
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.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
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)"
Java代码 public class RestaurantDemo extends JFrame implements ActionListener {
//private CardLayout card;
private javax.swing.JFrame tool;
private java.awt.List allFoodList;
private javax.swing.JList buyBtn_Others;
private javax.swing.JButton buyByCardBtn;
private javax.swing.JButton buyByFoodPointBtn;
private javax.swing.JPanel card3;
private javax.swing.JTextField cardNumberField_Others;
private javax.swing.JTextField cardNumberField_Student;
private javax.swing.JButton checkBtn;
private javax.swing.JPanel content;
private javax.swing.JButton deleteBtn;
private javax.swing.JButton insertBtn;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JButton managerBtn;
private javax.swing.JPanel managerCard;
private javax.swing.JButton othersBtn;
private javax.swing.JPanel othersCard;
private javax.swing.JPasswordField passwordField1_Student;
private javax.swing.JPasswordField passwordField2_Student;
private javax.swing.JPasswordField passwordField_Others;
private java.awt.List providedFoodList;
private javax.swing.JList providedFoodList_Student;
private javax.swing.JList soldFoodList;
private javax.swing.JButton studentBtn;
private javax.swing.JTextField studentNumberField;
private javax.swing.JPanel toolBar;
public RestaurantDemo() {
initComponents();
managerBtn.addActionListener(this);
tool.setSize(500,500);
tool.setVisible(true);
tool.setResizable(false);
}
public static void main(String[] arg) {
RestaurantDemo a = new RestaurantDemo();
}
private void initComponents() {
tool = new javax.swing.JFrame();
content = new javax.swing.JPanel();
othersCard = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
cardNumberField_Others = new javax.swing.JTextField();
passwordField_Others = new javax.swing.JPasswordField();
jScrollPane2 = new javax.swing.JScrollPane();
buyBtn_Others = new javax.swing.JList();
jButton1 = new javax.swing.JButton();
card3 = new javax.swing.JPanel();
jScrollPane3 = new javax.swing.JScrollPane();
providedFoodList_Student = new javax.swing.JList();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
cardNumberField_Student = new javax.swing.JTextField();
studentNumberField = new javax.swing.JTextField();
passwordField1_Student = new javax.swing.JPasswordField();
passwordField2_Student = new javax.swing.JPasswordField();
buyByCardBtn = new javax.swing.JButton();
buyByFoodPointBtn = new javax.swing.JButton();
managerCard = new javax.swing.JPanel();
allFoodList = new java.awt.List();
providedFoodList = new java.awt.List();
jScrollPane1 = new javax.swing.JScrollPane();
soldFoodList = new javax.swing.JList();
jLabel1 = new javax.swing.JLabel();
checkBtn = new javax.swing.JButton();
insertBtn = new javax.swing.JButton();
deleteBtn = new javax.swing.JButton();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
toolBar = new javax.swing.JPanel();
managerBtn = new javax.swing.JButton();
othersBtn = new javax.swing.JButton();
studentBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
//CardLayout card = ;
content.setLayout(new CardLayout());
jLabel2.setText("card number : ");
jLabel3.setText("pw:");
jScrollPane2.setViewportView(buyBtn_Others);
jButton1.setText("buy");
javax.swing.GroupLayout othersCardLayout = new javax.swing.GroupLayout(othersCard);
othersCard.setLayout(othersCardLayout);
othersCardLayout.setHorizontalGroup(
othersCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(othersCardLayout.createSequentialGroup()
.addGroup(othersCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(othersCardLayout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(othersCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(othersCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(passwordField_Others, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
.addComponent(cardNumberField_Others))
.addGap(27, 27, 27))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, othersCardLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(63, 63, 63)))
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
.addContainerGap())
);
othersCardLayout.setVerticalGroup(
othersCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(othersCardLayout.createSequentialGroup()
.addGroup(othersCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(othersCardLayout.createSequentialGroup()
.addGap(100, 100, 100)
.addGroup(othersCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(cardNumberField_Others, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(15, 15, 15)
.addGroup(othersCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(passwordField_Others, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(33, 33, 33)
.addComponent(jButton1))
.addGroup(othersCardLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(19, Short.MAX_VALUE))
);
content.add(othersCard, "card3");
jScrollPane3.setViewportView(providedFoodList_Student);
jLabel4.setText("card : ");
jLabel5.setText("pw : ");
jLabel6.setText("number : ");
jLabel7.setText("pw2 : ");
studentNumberField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
studentNumberFieldActionPerformed(evt);
}
});
buyByCardBtn.setText("카드 결제");
buyByFoodPointBtn.setText("푸드포인트 결제");
javax.swing.GroupLayout card3Layout = new javax.swing.GroupLayout(card3);
card3.setLayout(card3Layout);
card3Layout.setHorizontalGroup(
card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(card3Layout.createSequentialGroup()
.addGroup(card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(card3Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel6)
.addComponent(jLabel7)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cardNumberField_Student)
.addComponent(studentNumberField, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
.addComponent(passwordField1_Student)
.addComponent(passwordField2_Student)
.addGroup(card3Layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(buyByCardBtn)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, card3Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buyByFoodPointBtn)
.addGap(50, 50, 50)))
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
card3Layout.setVerticalGroup(
card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(card3Layout.createSequentialGroup()
.addGroup(card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(card3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(card3Layout.createSequentialGroup()
.addGap(66, 66, 66)
.addGroup(card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(cardNumberField_Student, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(passwordField1_Student, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(buyByCardBtn)
.addGap(32, 32, 32)
.addGroup(card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(studentNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(card3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(passwordField2_Student, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(buyByFoodPointBtn)))
.addContainerGap(19, Short.MAX_VALUE))
);
content.add(card3, "card4");
jScrollPane1.setViewportView(soldFoodList);
jLabel1.setText("list");
checkBtn.setText("sure");
insertBtn.setText("add");
deleteBtn.setText("delete");
jLabel8.setText("menue");
jLabel9.setText("mnue2");
javax.swing.GroupLayout managerCardLayout = new javax.swing.GroupLayout(managerCard);
managerCard.setLayout(managerCardLayout);
managerCardLayout.setHorizontalGroup(
managerCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(managerCardLayout.createSequentialGroup()
.addGroup(managerCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(managerCardLayout.createSequentialGroup()
.addContainerGap()
.addComponent(allFoodList, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(managerCardLayout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(insertBtn)))
.addGroup(managerCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(managerCardLayout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(providedFoodList, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(managerCardLayout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(deleteBtn)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
.addGroup(managerCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, managerCardLayout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, managerCardLayout.createSequentialGroup()
.addComponent(checkBtn)
.addGap(52, 52, 52))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, managerCardLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jLabel8)
.addGap(55, 55, 55)
.addComponent(jLabel9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(58, 58, 58))
);
managerCardLayout.setVerticalGroup(
managerCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(managerCardLayout.createSequentialGroup()
.addGap(16, 16, 16)
.addGroup(managerCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel8)
.addComponent(jLabel9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(managerCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
.addComponent(allFoodList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(providedFoodList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(13, 13, 13)
.addGroup(managerCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(checkBtn)
.addGroup(managerCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(insertBtn)
.addComponent(deleteBtn)))
.addContainerGap(42, Short.MAX_VALUE))
);
content.add(managerCard, "card2");
tool.add(content, java.awt.BorderLayout.CENTER);
toolBar.setBackground(new java.awt.Color(255, 255, 153));
managerBtn.setText("manager");
managerBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
managerBtnActionPerformed(evt);
}
});
othersBtn.setText("other");
studentBtn.setText("student");
javax.swing.GroupLayout toolBarLayout = new javax.swing.GroupLayout(toolBar);
toolBar.setLayout(toolBarLayout);
toolBarLayout.setHorizontalGroup(
toolBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(toolBarLayout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(managerBtn)
.addGap(18, 18, 18)
.addComponent(othersBtn)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(studentBtn)
.addContainerGap(186, Short.MAX_VALUE))
);
toolBarLayout.setVerticalGroup(
toolBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, toolBarLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(toolBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(managerBtn)
.addComponent(othersBtn)
.addComponent(studentBtn))
.addContainerGap())
);
tool.add(toolBar, java.awt.BorderLayout.PAGE_START);
pack();
}// </editor-fold>
private void managerBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void studentNumberFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
CardLayout card = (CardLayout)(content.getLayout());
Object eventSource = e.getSource();
//Object eventSource = null;
if(eventSource == managerBtn) {
//System.out.println("aa");
card.show(tool ,"card1");
}
}
}
CardLayout的父级错误
这是告诉你,你没有指定使用该卡片布局的"面板"。在您的代码中,您有一个setLayout( new CardLayout() )
语句来设置指定变量的布局。
RestaurantDemo.actionPerformed (RestaurantDemo.java: 379)
告诉您引起问题的语句。
该语句中的show(...)
方法必须引用与上面设置卡片布局相同的变量。
最后,一些一般性的建议。使用有意义的名称:
content.add(managerCard, "card2");
在你的代码中你使用"card1", "card2", "card3"…这些名字没有任何意义,而且会导致错误。使用更有意义的名称,例如"ManagerCard",这样您就不必猜测"card2"的含义。更好的做法是,在程序中声明静态变量,这样您就可以在整个代码中使用变量而不是文字值。