首先在這裏發表,所以請溫柔!對於我的問題,我已經搜索了很多,但都沒有找到答案。MVC在Swing中的多種形式
我努力學習和運用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
}
爲此歡呼。我只是要編輯我的問題...在TopLevelController中,我導致ShowImporter()函數是實例化和顯示新窗體的不正確方法? – swshaun 2013-02-16 13:31:33
ImportForm是一個JFrame btw ...我應該使用JInternalFrame? – swshaun 2013-02-16 13:35:58
乾杯Trashgod ...我也正好碰到這個作爲你的鏈接的結果:http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html這是我與掙扎!我使用Netbeans GUI bulider來更好地理解MVC :-) – swshaun 2013-02-16 13:42:41