Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我在哪里实现我的功能?_Java_Swing_Oop_Calculator - Fatal编程技术网

Java 我在哪里实现我的功能?

Java 我在哪里实现我的功能?,java,swing,oop,calculator,Java,Swing,Oop,Calculator,我正在尝试用JavaSwing来练习我的OOP技能,但我现在被卡住了。我正在尝试制作一个类似于你在手机上看到的计算器gui。我不知道我应该如何实现每个按键的功能。现在我只想在按下按钮时在屏幕上显示数字(JLabel对象)。我还附上了一张到目前为止我所拥有的GUI的图片 我应该在一个单独的.java文件中实现这些函数吗?或者应该在Calculator.java或Keyboard.java文件中实现它们 另外,它们是如何实现的,因为如果我的按钮对象在Keyboard.java文件中,我不知道如何在C

我正在尝试用JavaSwing来练习我的OOP技能,但我现在被卡住了。我正在尝试制作一个类似于你在手机上看到的计算器gui。我不知道我应该如何实现每个按键的功能。现在我只想在按下按钮时在屏幕上显示数字(JLabel对象)。我还附上了一张到目前为止我所拥有的GUI的图片

我应该在一个单独的.java文件中实现这些函数吗?或者应该在Calculator.java或Keyboard.java文件中实现它们

另外,它们是如何实现的,因为如果我的按钮对象在Keyboard.java文件中,我不知道如何在Calculator.java文件中的JLabel对象上显示

Calculator.java

package calculator;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator extends JFrame
{
    public static void main(String[] args)
    {
        new Calculator();
    }

    public Calculator() //Calculator constructor??
    {
        setLayout(new GridLayout(2,1));
        this.setSize(400,600);
        this.setLocationRelativeTo(null); //center the window
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel display = new JLabel();
        this.add(display);

        Keyboard kb = new Keyboard();
        this.add(kb);

        this.setVisible(true);
    }

}
package calculator;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class Keyboard extends JPanel implements ActionListener
{
    public Keyboard()
    {
        setLayout(new GridLayout(4,4));

        JButton one = new JButton("1");
        this.add(one);
        JButton two = new JButton("2");
        this.add(two);
        JButton three = new JButton("3");
        this.add(three);
        JButton plus = new JButton("+");
        this.add(plus);
        JButton four = new JButton("4");
        this.add(four);
        JButton five = new JButton("5");
        this.add(five);
        JButton six = new JButton("6");
        this.add(six);
        JButton minus = new JButton("-");
        this.add(minus);
        JButton seven = new JButton("7");
        this.add(seven);
        JButton eight = new JButton("8");
        this.add(eight);
        JButton nine = new JButton("9");
        this.add(nine);
        JButton times = new JButton("x");
        this.add(times);
        JButton zero = new JButton("0");
        this.add(zero);
        JButton clear = new JButton("clear");
        this.add(clear);
        JButton equals = new JButton("=");
        this.add(equals);
        JButton divide = new JButton("/");
        this.add(divide);
    }

    public void actionPerformed(ActionEvent e) {

    }
}
Keyboard.java

package calculator;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator extends JFrame
{
    public static void main(String[] args)
    {
        new Calculator();
    }

    public Calculator() //Calculator constructor??
    {
        setLayout(new GridLayout(2,1));
        this.setSize(400,600);
        this.setLocationRelativeTo(null); //center the window
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel display = new JLabel();
        this.add(display);

        Keyboard kb = new Keyboard();
        this.add(kb);

        this.setVisible(true);
    }

}
package calculator;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class Keyboard extends JPanel implements ActionListener
{
    public Keyboard()
    {
        setLayout(new GridLayout(4,4));

        JButton one = new JButton("1");
        this.add(one);
        JButton two = new JButton("2");
        this.add(two);
        JButton three = new JButton("3");
        this.add(three);
        JButton plus = new JButton("+");
        this.add(plus);
        JButton four = new JButton("4");
        this.add(four);
        JButton five = new JButton("5");
        this.add(five);
        JButton six = new JButton("6");
        this.add(six);
        JButton minus = new JButton("-");
        this.add(minus);
        JButton seven = new JButton("7");
        this.add(seven);
        JButton eight = new JButton("8");
        this.add(eight);
        JButton nine = new JButton("9");
        this.add(nine);
        JButton times = new JButton("x");
        this.add(times);
        JButton zero = new JButton("0");
        this.add(zero);
        JButton clear = new JButton("clear");
        this.add(clear);
        JButton equals = new JButton("=");
        this.add(equals);
        JButton divide = new JButton("/");
        this.add(divide);
    }

    public void actionPerformed(ActionEvent e) {

    }
}

您应该在“Keyboard.java”文件本身中编写actionPerformed()方法

Keyboard.java

one.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        displaystring = displaystring + "1";
        display.setText (displaystring);`
    }

您必须通过键盘类的构造函数传递display的引用,并将其分配给该类的某个实例变量,以便您也可以在键盘类中更新display标签

您应该在'Keyboard.java'文件本身中编写actionPerformed()方法

Keyboard.java

one.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        displaystring = displaystring + "1";
        display.setText (displaystring);`
    }

您必须通过键盘类的构造函数传递display的引用,并将其分配给该类的某个实例变量,以便您也可以在键盘类中更新display标签

使用
MVC
模式,将进入
actionPerformed(…)
方法的逻辑代码(或任何与此相关的逻辑代码)放入控制器类中的相应方法中


MVC
模式由三个类组成,
模型
视图
,和
控件
,它们共同构成GUI

MVC
模式的简要概述,最初取自,但稍作修改:

  • 您是用户-您与视图交互。控制器执行您的操作并解释它们。如果你点击一个按钮,控制器的工作就是弄清楚这意味着什么,以及如何根据这个动作操纵模型
  • 控制器要求模型更改其状态。当控制器接收到来自视图的操作时,可能需要通知视图进行更改。例如,控制器可以启用或禁用界面中的某些按钮或菜单项
  • 模型在其状态发生更改时通知视图。当模型中的某些内容发生更改时,无论是基于您所采取的某些操作(如单击按钮)还是其他内部更改(如播放列表中的下一首歌曲已开始),模型都会通知视图其状态已更改
  • 控制器也可能要求视图更改。
  • 视图向模型询问其状态。视图直接从模型获取其显示的状态。例如,当模型通知视图新歌曲已开始播放时,视图会从模型请求歌曲名称并显示它。作为控制器请求对视图进行某些更改的结果,视图还可能向模型询问状态
  • 关于
    MVC
    模式的更详细、更深入的解释可以在上面的链接中找到,只需一个简单的谷歌搜索


    我选择用于模型视图交互

    使用
    MVC
    结构的程序示例:

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Observable;
    import java.util.Observer;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    
    class Test {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    final Model model = new Model();
                    final Control control = new Control(model);
                    final View view = new View(model, control);
    
                    final JFrame frame = new JFrame("MVC Example");
                    frame.getContentPane().add(view);
                    frame.pack();
                    frame.setLocationByPlatform(true);
                    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
    
            });
        }
    
        static class Model extends Observable {
    
            private int number;
    
            void setNumber(int newValue) {
                number = newValue;
                setChanged();
                notifyObservers(number);
            }
    
            int getNumber() {
                return number;
            }
    
        }
    
        static class View extends JPanel {
    
            View(Model model, Control control) {
                final JLabel label = new JLabel(Integer.toString(model.getNumber()));
                final JButton button = new JButton("Click me!");
                button.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        control.buttonPressed();
                    }
    
                });
    
                setLayout(new BorderLayout());
                add(label);
                add(button, BorderLayout.SOUTH);
    
                model.addObserver(new Observer() {
    
                    @Override
                    public void update(Observable o, Object arg) {
                        label.setText(Integer.toString((int) arg));
                    }
    
                });
            }
    
        }
    
        static class Control {
    
            private Model model;
    
            Control(Model model) {
                this.model = model;
            }
    
            void buttonPressed() {
                model.setNumber(model.getNumber() + 1);
            }
    
        }
    
    }
    
    关于代码的注释:

    • 请查看和,以帮助您简化按钮的声明
    • 在添加所有组件以使
      JFrame
      适合其子项大小后,请在
      JFrame
      上调用
      pack()
      ,而不是设置
      JFrame
      的大小
    • 不要扩展
      JFrame
      ,而是创建一个实例并进行必要的修改
    • EDT
      (事件调度线程)上运行代码,以避免GUI中的“冻结”。有关更多信息,请参阅
    • <> Ly>下一次你考虑投稿,为了更好的帮助,请尽快发布.
    使用
    MVC
    模式,将进入
    actionPerformed(…)
    方法的逻辑代码(或与此相关的任何逻辑代码)放入控制器类中的相应方法中


    MVC
    模式由三个类组成,
    模型
    视图
    ,和
    控件
    ,它们共同构成GUI

    MVC
    模式的简要概述,最初取自,但稍作修改:

  • 您是用户-您与视图交互。控制器执行您的操作并解释它们。如果你点击一个按钮,控制器的工作就是弄清楚这意味着什么,以及如何根据这个动作操纵模型
  • 控制器要求模型更改其状态。当控制器接收到来自视图的操作时,可能需要通知视图进行更改。例如,控制器可以启用或禁用界面中的某些按钮或菜单项
  • 模型在其状态发生更改时通知视图。当模型中的某些内容发生更改时,无论是基于您所采取的某些操作(如单击按钮)还是其他内部更改(如播放列表中的下一首歌曲已开始),模型都会通知视图其状态已更改
  • 控制器也可能要求视图更改。
  • 视图向模型询问其状态。视图直接从模型获取其显示的状态。例如,当模型通知视图新歌曲已开始播放时,视图会从模型请求歌曲名称并显示