1

我正在使用适当的 GUI 和所有内容创建一个用于数据库操作的程序。我也在使用swing。无论如何,当我运行应用程序时,会打开以下窗口:

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);

}

现在我想开另一个JPanelInventoryForm. 上面代码中的最后一个方法showInventory()应该是 open InventoryForm,但我得到一个错误:

线程“AWT-EventQueue-0”中的异常 java.lang.Error: java.lang.reflect.InvocationTargetException
    在 org.jdesktop.application.ApplicationAction.actionFailed(ApplicationAction.java:859)
    在 org.jdesktop.application.ApplicationAction.noProxyActionPerformed(ApplicationAction.java:665)
    在 org.jdesktop.application.ApplicationAction.actionPerformed(ApplicationAction.java:698)
.....................

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

4

2 回答 2

2

“项目当前被冻结。”“项目当前被冻结。”

这并不完全正确。您可以在http://kenai.com/projects/bsaf查看此框架的绝对兼容分支

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

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

于 2010-09-27T09:22:30.630 回答
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());

附录:从 NetBeansNew Project对话框中,“请注意,JSR-296(Swing 应用程序框架)已不再开发,并且不会按原计划成为官方 Java 开发工具包的一部分。您仍然可以按原样使用 Swing 应用程序框架库,但预计不会有进一步的发展。”

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

于 2010-09-26T12:57:52.063 回答