1

第一次在这里发帖,所以请温柔!我已经广泛搜索了我的问题的答案,但一无所获。

我正在尝试学习和应用 MVC 架构来创建 Java Swing 应用程序。我想我理解模型、视图和控制器的不同角色。

但是,我的应用程序有一个 JMenuBar(文件、编辑等...)。

我想要做的是在单击一个菜单项后,一个要弹出的表单(它从 DVSDesk 类委托给控制器)​​。

我遇到的困难是如何显示接受控制器和模型的表单——根据我的阅读,每个 JFrame 都需要它自己的线程,这就是我感到困惑的地方。由于invokeLater它在自己独特的线程中,我似乎无法传递模型或控制器。

抱歉,如果这是一个愚蠢的问题,但我一直在四处寻找,似乎无处可去!

编辑——我真正的问题—— showImporterForm() 是创建和显示新表单的正确方法吗?

下面是主线程的代码(DVSMain.java)

public static void main(String[] args) {
    TopLevelController TLC;
    TopLevelModel TLM;

     java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            TopLevelModel TLM = new TopLevelModel();
            TopLevelController TLC = new TopLevelController(TLM);
        }
    });
}

下面是控制器的代码(TopLevelController.java)

public class TopLevelController {    
// Initialise model and view
TopLevelModel TLM;
DVSDesk TLV;

public TopLevelController(TopLevelModel model) {
    // Get a reference to the view and model
    TLM = model;
    TLV = new DVSDesk(this,model);
    TLV.setVisible(true);
}

public void showImporter() {
    ImportForm importFm = new ImportForm(this,TLM);
    importFm.setVisible(true);
}

/*public void showForm(final Form fm) {        
    // Show the form which has been passed in

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {                
            fm.show();
        }
    });
}*/

public void quit() {System.exit(0);};
}

下面是来自 DVSDesk.java 的代码(基于菜单的 GUI)......

public class DVSDesk extends javax.swing.JFrame {

/**
 * Creates new form DVSDesk
 */

TopLevelController TLC;
TopLevelModel TLM;

public DVSDesk(TopLevelController controller, TopLevelModel model) {
    initComponents();
    TLC = controller;
    TLM = model;
}

/**
 * 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() {

    jLayeredPane1 = new javax.swing.JLayeredPane();
    jSeparator1 = new javax.swing.JSeparator();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    fImportDiags = new javax.swing.JMenuItem();
    jSeparator2 = new javax.swing.JPopupMenu.Separator();
    fQuit = new javax.swing.JMenuItem();
    jMenu2 = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jMenu1.setText("File");

    fImportDiags.setText("Import Diagrams...");
    fImportDiags.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            fImportDiagsActionPerformed(evt);
        }
    });
    jMenu1.add(fImportDiags);
    jMenu1.add(jSeparator2);

    fQuit.setText("Quit...");
    fQuit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            fQuitActionPerformed(evt);
        }
    });
    jMenu1.add(fQuit);

    jMenuBar1.add(jMenu1);

    jMenu2.setText("Edit");
    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 800, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 579, Short.MAX_VALUE)
    );

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

private void fQuitActionPerformed(java.awt.event.ActionEvent evt) {                                      
    TLC.quit();
}                                     

private void fImportDiagsActionPerformed(java.awt.event.ActionEvent evt) {                                             
    TLC.showImporter();
}                                            

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(DVSDesk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(DVSDesk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(DVSDesk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(DVSDesk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
}
// Variables declaration - do not modify                     
private javax.swing.JMenuItem fImportDiags;
private javax.swing.JMenuItem fQuit;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2;
// End of variables declaration                   
}
4

1 回答 1

2

所有 Swing 组件共享一个公共事件调度线程,尽管应用程序通常使用一个 JFrame. 您可以在此处了解有关 MVC 和 Swing的更多信息。

附录:顺便说一句,不要让 GUI 设计师决定你的 GUI 设计。您需要了解如何手动使用 Swing,以便了解使用设计器时遇到的问题。如此处所示,您可以将内容添加到您选择的顶级容器中,并限制对需要它的组件使用设计器。

于 2013-02-16T13:24:41.183 回答