gpt4 book ai didi

java - CardLayout IllegalArgumentException 与 next()

转载 作者:行者123 更新时间:2023-12-01 20:56:28 25 4
gpt4 key购买 nike

我正在尝试创建一个从一个面板移动到另一个面板的程序,并且用户在每个面板中回答一个问题。我目前正在尝试使用 next() 来切换卡,但它给了我一个 IllegalArgumentException 问题。

我已将代码简化为问题的基础,以便消除问题中不必要的代码。我的主要类是箭头,第二类是 Flash,因为我希望每个面板都有自己的物理文件。我尝试在 Flash 中调用相同的 next() 函数以获得相同的效果,即使技术上没有第三个面板。

这是箭头(主要)

package layout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Arrow{
public JPanel panelHouse;
public JFrame frame = new JFrame();
public int total = 3000;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Arrow window = new Arrow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
} } }); } //putting all the closing brackets on one line saves space. This is test code so it doesn't matter to me
public Arrow() {
initialize();
}
public Arrow(int x){
//this is here so I don't run initialize when I need to create an Arrow variable in other classes
//it doesn't actually do anything
}

private void initialize() {
CardLayout cl = new CardLayout(0, 0);
frame.setBounds(400, 400, 909, 572);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(cl);

JPanel panelHouse = new JPanel();
panelHouse.setBounds(0, 0, 10, 10);
frame.getContentPane().add(panelHouse, "Housing");
panelHouse.setLayout(null);

Flash Flash1 = new Flash();
frame.getContentPane().add(Flash1, "Flash");
Flash1.setLayout(null);

JButton btnHouseNext = new JButton("NEXT STATION");
btnHouseNext.setFont(new Font("Times New Roman", Font.PLAIN, 20));
btnHouseNext.setBounds(344, 371, 184, 48);
btnHouseNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cl.next(frame);
}
});//Close housingNext button actionlistener
panelHouse.add(btnHouseNext);
}//close initialize

public JPanel getPanelHouse() {
return panelHouse;
}

public JFrame getFrame(){
return frame;
}
}//close class body

这是 Flash(第二个面板)

package layout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Flash extends JPanel{
private CardLayout cardLayout = new CardLayout();
private static final long serialVersionUID = 1L;
public Flash() {
Arrow a = new Arrow(4);
JPanel Flash1 = new JPanel();
setBounds(0, 0, 10, 10);
setLayout(null);
setBackground(Color.orange);

JButton buttonFlash = new JButton("NEXT STATION");
buttonFlash.setFont(new Font("Times New Roman", Font.PLAIN, 20));
buttonFlash.setBounds(344, 371, 184, 48);
buttonFlash.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Flash1.setVisible(false);
cardLayout.next(a.getFrame());
}
});
add(buttonFlash);
setVisible(true);
}
}

这是我的错误堆栈跟踪

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout
at java.awt.CardLayout.checkLayout(Unknown Source)
at java.awt.CardLayout.next(Unknown Source)
at layout.Arrow$2.actionPerformed(Arrow.java:48)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

现在我还看到了容器是 JPanel 而不是像我一样使用 JFrame 的代码。我这样做是否不正确,应该改变我的做法吗?

编辑:我更改了代码以反射(reflect)建议

最佳答案

您需要替换此行:

frame.getContentPane().setLayout(new CardLayout(0, 0));

这样:

frame.getContentPane().setLayout(cl);

按照当前的编写方式,您将框架的布局设置为全新的 CardLayout,而不是使用您创建并分配给 cl 变量的布局。

关于java - CardLayout IllegalArgumentException 与 next(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42288600/

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