0

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

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

应用程序.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();
    }
}

查看_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");


    }
}
4

0 回答 0