Java 基本MVC构造,ActionListner不起作用

Java 基本MVC构造,ActionListner不起作用,java,swing,model-view-controller,Java,Swing,Model View Controller,我想做一个非常简单的MVC项目,当我点击按钮时,我想在我的控制台上看到“napis”,但是ActionListener不能正常工作。有什么想法吗?(如果我的帖子有问题,请理解我的第一篇帖子:)) } } ofc所有导入都是固定的。model.napis()不在控制台中写入任何内容: public String napis (){ return "napis"; } 它只返回一个字符串 这应该做到: @Override public void actionPerformed(Action

我想做一个非常简单的MVC项目,当我点击按钮时,我想在我的控制台上看到“napis”,但是ActionListener不能正常工作。有什么想法吗?(如果我的帖子有问题,请理解我的第一篇帖子:))

}

}

ofc所有导入都是固定的。

model.napis()不在控制台中写入任何内容:

public String napis (){
    return "napis";
}
它只返回一个
字符串

这应该做到:

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println(model.napis());
}

在控制器类中,更改

model.napis();


您的问题是,动作侦听器接收字符串,但不处理它。您需要做的是打印返回的字符串。

好的,因此您的代码有点混乱。您的
控制器
正在创建
视图
的实例,但它也被传递了
视图
的实例

它创建的实例没有附加
ActionListener
,但您传入的实例有

两个
视图都创建并显示
JFrame

因为您要传递的
视图
的实例首先被创建,它首先出现,但是
控制器
创建的
视图
的实例第二次被创建,它出现在顶部-但它没有附加
ActionListener

因此,您应该做的第一件事是去掉
控制器正在创建的
视图的实例

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        Controller controller = new Controller(new Model(), new View());
    }

    public class Model {

        public String napis() {
            return "napis";
        }
    }

    public class View {

        private JFrame frame;
        private JLabel label;
        private JButton button;

        public View() {
            frame = new JFrame();
            label = new JLabel("Napis");
            button = new JButton("click");

            frame.add(label);
            frame.add(button);
            frame.setSize(500, 500);
            button.setSize(30, 30);
            frame.setVisible(true);
        }

        public void addActionListener(ActionListener click) {
            button.addActionListener(click);
        }
    }

    public class Controller {

        private Model model;
        private View view;

        public Controller(final Model model, View view) {
            this.view = view;
            this.model = model;
            view.addActionListener(
                new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Clicked");
                    this.model.napis();
                }
            });
        }
    }
}
您还应该关注
视图的构造函数正在创建的副作用,创建和显示框架可能不是正确的选择


我还鼓励您对“代码到接口而不是实现”的概念进行一些研究,因为这是MVC的关键方面之一

是的,刚刚尝试过,但仍然没有结果,这是一个简单的示例,但昨天我无法通过public void actionPerformed(ActionEvent e)访问另一个窗口{}“这是一个简单的示例”。对于实际代码,我看不出有任何问题。添加一个main(String[]args)方法,它应该可以工作。例如:
publicstaticvoidmain(String[]args){View View View View=newview();newcontroller(newmodel(),View);}
使用另一个代码,如果问题中没有指定,我猜不出问题出在哪里。因此,我正在查看您的代码,我感到困惑。您似乎从未构造过
控制器
,而且,在
控制器
中,您创建了
视图
的新实例,但也传递了
视图
的实例。。。那么哪个是哪个?
@Override
public void actionPerformed(ActionEvent e) {
    System.out.println(model.napis());
}
model.napis();
System.out.println(model.napis());
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        Controller controller = new Controller(new Model(), new View());
    }

    public class Model {

        public String napis() {
            return "napis";
        }
    }

    public class View {

        private JFrame frame;
        private JLabel label;
        private JButton button;

        public View() {
            frame = new JFrame();
            label = new JLabel("Napis");
            button = new JButton("click");

            frame.add(label);
            frame.add(button);
            frame.setSize(500, 500);
            button.setSize(30, 30);
            frame.setVisible(true);
        }

        public void addActionListener(ActionListener click) {
            button.addActionListener(click);
        }
    }

    public class Controller {

        private Model model;
        private View view;

        public Controller(final Model model, View view) {
            this.view = view;
            this.model = model;
            view.addActionListener(
                new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Clicked");
                    this.model.napis();
                }
            });
        }
    }
}