我正在尝试从另一个框架打开JPanel。不能这样做

时间:2010-09-26 12:25:55

标签: java swing netbeans-6.9 swing-app-framework

我正在使用适当的GUI创建一个数据库操作程序。我也在使用秋千。无论如何,当我运行应用程序时,将打开以下窗口:

public class MusicShopManagementView extends FrameView {

public MusicShopManagementView(SingleFrameApplication app) {
    super(app);

    initComponents();

    // status bar initialization - message timeout, idle icon and busy animation, etc
    ResourceMap resourceMap = getResourceMap();
    int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
    messageTimer = new Timer(messageTimeout, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            statusMessageLabel.setText("");
        }
    });
    messageTimer.setRepeats(false);
    int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
    for (int i = 0; i < busyIcons.length; i++) {
        busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
    }
    busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
            statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
        }
    }); 
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);

    // connecting action tasks to status bar via TaskMonitor
    TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            String propertyName = evt.getPropertyName();
            if ("started".equals(propertyName)) {
                if (!busyIconTimer.isRunning()) {
                    statusAnimationLabel.setIcon(busyIcons[0]);
                    busyIconIndex = 0;
                    busyIconTimer.start();
                }
                progressBar.setVisible(true);
                progressBar.setIndeterminate(true);
            } else if ("done".equals(propertyName)) {
                busyIconTimer.stop();
                statusAnimationLabel.setIcon(idleIcon);
                progressBar.setVisible(false);
                progressBar.setValue(0);
            } else if ("message".equals(propertyName)) {
                String text = (String)(evt.getNewValue());
                statusMessageLabel.setText((text == null) ? "" : text);
                messageTimer.restart();
            } else if ("progress".equals(propertyName)) {
                int value = (Integer)(evt.getNewValue());
                progressBar.setVisible(true);
                progressBar.setIndeterminate(false);
                progressBar.setValue(value);
            }
        }
    });

    // tracking table selection
    masterTable.getSelectionModel().addListSelectionListener(
        new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                firePropertyChange("recordSelected", !isRecordSelected(),
                    isRecordSelected());
            }
        });

    // tracking changes to save
    bindingGroup.addBindingListener(new AbstractBindingListener() {
        @Override
        public void targetChanged(Binding binding, PropertyStateEvent event) {
            // save action observes saveNeeded property
            setSaveNeeded(true);
        }
    });

    // have a transaction started
    entityManager.getTransaction().begin();
}


public boolean isSaveNeeded() {
    return saveNeeded;
}

private void setSaveNeeded(boolean saveNeeded) {
    if (saveNeeded != this.saveNeeded) {
        this.saveNeeded = saveNeeded;
        firePropertyChange("saveNeeded", !saveNeeded, saveNeeded);
    }
}

public boolean isRecordSelected() {
    return masterTable.getSelectedRow() != -1;
}


@Action
public void newRecord() {
    musicshopmanagement.Intrument i = new musicshopmanagement.Intrument();
    entityManager.persist(i);
    list.add(i);
    int row = list.size()-1;
    masterTable.setRowSelectionInterval(row, row);
    masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
    setSaveNeeded(true);
}

@Action(enabledProperty = "recordSelected")
public void deleteRecord() {
    int[] selected = masterTable.getSelectedRows();
    List<musicshopmanagement.Intrument> toRemove = new
        ArrayList<musicshopmanagement.Intrument>(selected.length);
    for (int idx=0; idx<selected.length; idx++) {
        musicshopmanagement.Intrument i = 
            list.get(masterTable.convertRowIndexToModel(selected[idx]));
        toRemove.add(i);
        entityManager.remove(i);
    }
    list.removeAll(toRemove);
    setSaveNeeded(true);
}


@Action(enabledProperty = "saveNeeded")
public Task save() {
    return new SaveTask(getApplication());
}

private class SaveTask extends Task {
    SaveTask(org.jdesktop.application.Application app) {
        super(app);
    }
    @Override protected Void doInBackground() {
        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<musicshopmanagement.Intrument> merged = new 
                ArrayList<musicshopmanagement.Intrument>(list.size());
            for (musicshopmanagement.Intrument i : list) {
                merged.add(entityManager.merge(i));
            }
            list.clear();
            list.addAll(merged);
        }
        return null;
    }
    @Override protected void finished() {
        setSaveNeeded(false);
    }
}

/**
 * An example action method showing how to create asynchronous tasks
 * (running on background) and how to show their progress. Note the
 * artificial 'Thread.sleep' calls making the task long enough to see the
 * progress visualization - remove the sleeps for real application.
 */
@Action
public Task refresh() {
   return new RefreshTask(getApplication());
}

private class RefreshTask extends Task {
    RefreshTask(org.jdesktop.application.Application app) {
        super(app);
    }
    @SuppressWarnings("unchecked")
    @Override protected Void doInBackground() {
        try {
            setProgress(0, 0, 4);
            setMessage("Rolling back the current changes...");
            setProgress(1, 0, 4);
            entityManager.getTransaction().rollback();

            setProgress(2, 0, 4);

            setMessage("Starting a new transaction...");
            entityManager.getTransaction().begin();

            setProgress(3, 0, 4);

            setMessage("Fetching new data...");
            java.util.Collection data = query.getResultList();
            for (Object entity : data) {
                entityManager.refresh(entity);
            }

            setProgress(4, 0, 4);


            list.clear();
            list.addAll(data);
        } catch(Exception ignore) { }
        return null;
    }
    @Override protected void finished() {
        setMessage("Done.");
        setSaveNeeded(false);
    }
}

@Action
public void showAboutBox() {
    if (aboutBox == null) {
        JFrame mainFrame = MusicShopManagementApp.getApplication().getMainFrame();
        aboutBox = new MusicShopManagementAboutBox(mainFrame);
        aboutBox.setLocationRelativeTo(mainFrame);
    }
    MusicShopManagementApp.getApplication().show(aboutBox);
}
@Action
public void showInventory() {
    JFrame frame1 = new JFrame(); 
            frame1.setContentPane(new InventoryForm());
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setVisible(true);

}

现在我要打开另一个名为JPanel的{​​{1}}。上面代码中的最后一个方法InventoryForm应该打开showInventory(),但是我收到错误:

Exception in thread "AWT-EventQueue-0" java.lang.Error: java.lang.reflect.InvocationTargetException
    at org.jdesktop.application.ApplicationAction.actionFailed(ApplicationAction.java:859)
    at org.jdesktop.application.ApplicationAction.noProxyActionPerformed(ApplicationAction.java:665)
    at org.jdesktop.application.ApplicationAction.actionPerformed(ApplicationAction.java:698)
.................

要么我的整个方法都不正确,要么我(显然)搞砸了某个地方。请帮忙!

2 个答案:

答案 0 :(得分:2)

  

“项目目前正在冻结。”“项目目前正在冻结。”

这不完全正确。您可以在http://kenai.com/projects/bsaf

查看此框架的绝对兼容的分支

它即将发布1.9版本,修复了大量的错误,以及一些重要的改进。

如果您需要帮助解决问题,可以使用此项目的论坛。那里有许多经验丰富的开发人员提供完整堆栈跟踪和代码段,您可以将操作绑定到按钮或菜单项。

答案 1 :(得分:1)

看起来你正在使用我不熟悉的Swing Application Framework (JSR 296),但通常的方法是add()面板到框架,如下所示。

http://platform.netbeans.org/ frame1 = new JFrame(); 
frame1.add(new InventoryForm());
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);

这是一个常见的选择:

frame1.getContentPane().add(new InventoryForm());

附录:来自NetBeans New Project对话框,“请注意,JSR-296(Swing应用程序框架)已不再开发,不会像原计划那样成为官方Java Development Kit的一部分。您仍然可以使用现在的Swing应用程序框架库,但预计不会进一步开发。“

“如果您正在寻找基于Swing的应用程序框架,请考虑使用NetBeans平台platform.netbeans.org,这是一个适用于创建复杂和可伸缩桌面应用程序的全功能平台。该平台包含简化API的API处理窗口,操作,文件和许多其他典型的应用程序元素。“