与MVC多种形式在Swing(Multiple Forms with MVC in Swing)

2019-10-17 17:06发布

第一篇文章在这里,所以请温柔! 我已搜查甚广的答案,我的问题,但都却一无所获。

我想学习和运用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                   
}

Answer 1:

所有Swing组件都有一个共同的事件调度线程 ,虽然应用程序通常使用一个单一 JFrame 。 您可以了解更多关于MVC和Swing 在这里 。

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



文章来源: Multiple Forms with MVC in Swing