基于MVC的基本Swing应用程序

时间:2012-12-08 12:12:28

标签: java swing model-view-controller jgoodies

所以我遇到了以下问题:我想用我的App类中的控制器实例初始化Swing构造函数。这是我启动回购和控制器的地方。当我想传递给swing Gui类时,控制器参数我意识到它有自己的main方法。

请您查一下代码,告诉我这是否正确?因为我不确定,我找不到从TUI应用程序到GUI应用程序的基本示例。谢谢!

  

App.java

package app;

import repository.*;
import view.View_gui;
import controller.*;

public class App{
    RepoInterface ri;
    ControllerInterface c;
    View_gui gui;

    public App() {
        ri = new Repo();
        c = new Controller(ri);
        gui = new View_gui(c);
    }

    public static void main(String[] args) {
        App app = new App();
    }
}
  

View_gui.java

package view;

import controller.*;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.BorderLayout;
import java.awt.Label;
import java.awt.Panel;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JSeparator;
import javax.swing.JPanel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.GridLayout;

public class View_gui {

    private JFrame frame;
    private static ControllerInterface ci; //added the variable

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    View_gui window = new View_gui(ci); // parametrizied
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public View_gui(ControllerInterface c) {
        ci = c;                          // add + param by me
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame("Countries");
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel lblNewLabel = new JLabel("Country");

        JLabel lblCapital = new JLabel("Capital");

        JLabel lblArea = new JLabel("Area");


    }
}

0 个答案:

没有答案