I want to write an application in Java SWING using the MVC architecture, but I have problem with using multiple controllers because I don't know how to do this.
public class Application {
public static void main(String[] args) {
Application app = new Application();
app.Start();
}
public void Start()
{
MainModel model = new MainModel();
MainView view = new MainView();
MainController controller = new MainController(view,model);
view.setVisible(true);
}}
MainController:
public class MainController {
private MainView theView;
private MainModel theModel;
public MainController(MainView theView, MainModel theModel)
{
this.theModel = theModel;
this.theView = theView;
}}
MainView:
public class MainView extends JFrame{
private JMenu menu = new JMenu();
public MainView()
{
JPanel panel = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,800);
panel.add(menu);
this.add(panel);
}}
I do not put here because the model is now empty.
Okay, so I don't know how to use multiple controllers. For example: I want to create new controller UserController and I don't know how to implement this. Should I create new controller in MainController? If I create UserController in MainController how to use it?
Just create UserController in your Application class where you added your MainController.
Example: if you have 2 buttons you can add actionlistener.
Button1 will do UserController.doSomething();
Button2 will do MainController.doSomeOtherThings();
Thats the way you use it ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.