Advertisement
chris5855

MVC-GUI

Jul 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package javaapplication4;
  2.  
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. import javax.swing.JTextField;
  10. import javax.swing.SwingUtilities;
  11. import javax.swing.UIManager;
  12. import javax.swing.UnsupportedLookAndFeelException;
  13.  
  14.  
  15.  
  16.  
  17. public class JavaApplication4 {
  18.     public static void main(String[] args) {
  19.         SwingUtilities.invokeLater(()->{
  20.             try{
  21.                 JFrame.setDefaultLookAndFeelDecorated(true);
  22.                 UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  23.             }catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e){}
  24.        
  25.         Model model= new Model();
  26.         View view= new View();
  27.         Controller controller= new Controller(model, view);
  28.        
  29.         controller.setUpGuiController();
  30.         });
  31.     }
  32. }
  33.  
  34. class Model{
  35.  
  36.     public Model() {
  37.     }
  38. }
  39.  
  40. class View extends JFrame{
  41.     public JPanel jPanel;
  42.     public JButton jButton;
  43.     public JTextField jTextField;
  44.    
  45.    
  46.     public View() {
  47.        super("MVC GUI");
  48.        setUpGuiView();
  49.      
  50.     }
  51.    
  52.     public final void setUpGuiView(){
  53.         jPanel= new JPanel();
  54.         jButton= new JButton("click");
  55.         jTextField= new JTextField(10);
  56.        
  57.         jPanel.setBackground(Color.DARK_GRAY);
  58.         jPanel.add(jButton);
  59.         jPanel.add(jTextField);
  60.         this.add(jPanel);
  61.     }
  62. }
  63.  
  64. class Controller implements ActionListener{
  65.     private Model model;
  66.     private View view;
  67.  
  68.     public Controller(Model model, View view) {
  69.         this.model = model;
  70.         this.view = view;
  71.        
  72.         actionListenerController();
  73.         setUpGuiController();
  74.     }
  75.    
  76.     public final void setUpGuiController(){
  77.         view.setVisible(true);
  78.         view.setSize(350,350);
  79.         view.setResizable(true);
  80.         view.setLocationRelativeTo(null);
  81.         view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  82.     }
  83.    
  84.     public final void actionListenerController(){
  85.         view.jButton.addActionListener(this);
  86.  
  87.     }
  88.  
  89.     @Override
  90.     public void actionPerformed(ActionEvent ae) {
  91.         view.jButton.addActionListener((ActionEvent actionEvent) -> {
  92.             if(view.jButton.equals(ae.getSource())){
  93.                 System.out.println("You re clicking in the controller class.");
  94.             }
  95.         });
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement