Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication4;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- public class JavaApplication4 {
- public static void main(String[] args) {
- SwingUtilities.invokeLater(()->{
- try{
- JFrame.setDefaultLookAndFeelDecorated(true);
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
- }catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e){}
- Model model= new Model();
- View view= new View();
- Controller controller= new Controller(model, view);
- controller.setUpGuiController();
- });
- }
- }
- class Model{
- public Model() {
- }
- }
- class View extends JFrame{
- public JPanel jPanel;
- public JButton jButton;
- public JTextField jTextField;
- public View() {
- super("MVC GUI");
- setUpGuiView();
- }
- public final void setUpGuiView(){
- jPanel= new JPanel();
- jButton= new JButton("click");
- jTextField= new JTextField(10);
- jPanel.setBackground(Color.DARK_GRAY);
- jPanel.add(jButton);
- jPanel.add(jTextField);
- this.add(jPanel);
- }
- }
- class Controller implements ActionListener{
- private Model model;
- private View view;
- public Controller(Model model, View view) {
- this.model = model;
- this.view = view;
- actionListenerController();
- setUpGuiController();
- }
- public final void setUpGuiController(){
- view.setVisible(true);
- view.setSize(350,350);
- view.setResizable(true);
- view.setLocationRelativeTo(null);
- view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public final void actionListenerController(){
- view.jButton.addActionListener(this);
- }
- @Override
- public void actionPerformed(ActionEvent ae) {
- view.jButton.addActionListener((ActionEvent actionEvent) -> {
- if(view.jButton.equals(ae.getSource())){
- System.out.println("You re clicking in the controller class.");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement