gpt4 book ai didi

下一个显示的 Java 错误 JInternalFrame

转载 作者:行者123 更新时间:2023-12-01 13:17:55 27 4
gpt4 key购买 nike

我在将 jInternalFrame 从另一个 jInternalFrame 显示到 JDesktopane 时遇到问题,我使用的是 Netbeans IDE。第一次显示没有出现问题,但是当 jInternalFrame 用 dispose() 关闭并想再次显示时出现错误。

这是错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position
at java.awt.Container.addImpl(Container.java:1093)
at javax.swing.JLayeredPane.addImpl(JLayeredPane.java:230)
at javax.swing.JDesktopPane.addImpl(JDesktopPane.java:474)
at java.awt.Container.add(Container.java:410)
at InternalTes.FrameMaster.menuTambahActionPerformed(FrameMaster.java:83)
at InternalTes.FrameMaster.access$000(FrameMaster.java:13)
at InternalTes.FrameMaster$1.actionPerformed(FrameMaster.java:49)
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:3321)
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)

这是FrameMaster(JInternalFrame)代码:

package InternalTes;
/**
*
* @author Vikar
*/
public class FrameMaster extends javax.swing.JInternalFrame {

/**
* Creates new form FrameMaster
*/
public FrameMaster() {
initComponents();
}

private static FrameMaster instance;
public static FrameMaster getInstance() {
if(instance == null){
instance = new FrameMaster();
}
return instance;
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

btnShowChild = new javax.swing.JButton();

btnShowChild.setText("Show");
btnShowChild.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnShowChildActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnShowChild)
.addContainerGap(336, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(238, 238, 238)
.addComponent(btnShowChild)
.addContainerGap(17, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void btnShowChildActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
FrameChild wd = FrameChild.getInstance();
if(wd.isVisible()){
wd.toFront();
}else {
Window.jDesktopPane1.add(wd);
wd.setVisible(true);
}
}

// Variables declaration - do not modify
private javax.swing.JButton btnShowChild;
// End of variables declaration
}

这是 FrameChild(JInternalFrame) 代码:

package InternalTes;
/**
*
* @author Vikar
*/
public class FrameChild extends javax.swing.JInternalFrame {

/**
* Creates new form input
*/
public FrameChild() {
initComponents();
}

private static FrameChild myInstance;
public static FrameChild getInstance() {
if(myInstance == null){
myInstance = new FrameChild();
}
return myInstance;
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

btnClose = new javax.swing.JButton();

btnClose.setText("jButton1");
btnClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCloseActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(311, Short.MAX_VALUE)
.addComponent(btnClose)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(244, Short.MAX_VALUE)
.addComponent(btnClose)
.addContainerGap())
);

pack();
}// </editor-fold>

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
dispose();
}


// Variables declaration - do not modify
private javax.swing.JButton btnClose;
// End of variables declaration
}

对于 Window(JFrame),我没有将其包含在此处,因为代码只是普通的 JFrameJDesktoPane 内部。请java编程大师支持一下,这个问题让我疯狂了半年。我的系统信息项目只是因为这个问题而卡住了,我使用无分支的 JInternalFrame 没有问题。

最佳答案

您可以添加此行(子框架保留在内存中,但将在“dispose”时隐藏)

private void initComponents() {
setDefaultCloseOperation(HIDE_ON_CLOSE);
...

或者添加此行(子框架将一直被销毁并重新创建)

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {                                         
myInstance = null;
dispose();
}

并调整方法btnShowChildActionPerformed以获得正确的行为。

关于下一个显示的 Java 错误 JInternalFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22318319/

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