Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.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 JFrame未被写入_Java_Swing_Model View Controller_Jframe - Fatal编程技术网

Java JFrame未被写入

Java JFrame未被写入,java,swing,model-view-controller,jframe,Java,Swing,Model View Controller,Jframe,我的JFrame正在打开一个空白窗口。 我试图创建calcView类的实例,以便打开并写入JFrame窗口,但它没有正常工作。 基本上,我必须使用MVC方法来开发一个只使用乘法运算的计算器,所以这里显示了3个单独的类: CalcView CalcModel import java.awt.event.*; public class CalcModel{ public static final String INITIAL_VALUE = "1"; p

我的JFrame正在打开一个空白窗口。
我试图创建calcView类的实例,以便打开并写入JFrame窗口,但它没有正常工作。 基本上,我必须使用MVC方法来开发一个只使用乘法运算的计算器,所以这里显示了3个单独的类:

CalcView

CalcModel

import java.awt.event.*;
public class CalcModel{
    public static final String INITIAL_VALUE = "1";
    
    public float multiply(){
        float i = 0;
        return i;
    }
}


钙控制器

import javax.swing.*;

public class CalcController {
    public static void main(String[] args) {
    CalcView v = new CalcView();
    v.CalcGUI();
    }
  
    public void listener() {
        m_clearBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                m_total = new BigInteger(INITIAL_VALUE);
                m_totalTf.setText(INITIAL_VALUE);
            }
        });
        m_multiplyBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    CalcModel m = new CalcModel();
                    m.multiply(e);
                } catch (NumberFormatException nex) {
                   CalcView v = new CalcView();
                   v.throwExcept();
                }
            }
        });
    }
}


此外,如何在类之间传递事件处理程序?

原因是您将内容面板添加到了错误的JFrame。 在这行代码中,您创建了一个新的JFrame

JFrame window = new CalcView();
但是在这些代码行中,您将内容JPanel添加到CalcView类中,而不是它自己

this.setContentPane(content);
this.pack();
有两个选项可以使其可见,第一个选项是不扩展JFrame,并将内容JPanel添加到窗口JFrame。第二个选项是ExtendJFrame,不创建新的窗口JFrame

第一个选项显示在下面的代码中

import javax.swing.*;
import java.math.BigInteger;
import java.awt.*;
public class CalcView{
    private static final String INITIAL_VALUE = "1";

    private JTextField m_totalTf = new JTextField(10);
    private JTextField m_userInputTf = new JTextField(10);
    private JButton m_multiplyBtn = new JButton("Multiply");
    private JButton m_clearBtn = new JButton("Clear");

    private BigInteger m_total; // The total current value state.

    JFrame window = new JFrame();

    public void CalcGUI() {
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Performs Simple Multiplications");
        window.setVisible(true);
        m_total = new BigInteger(INITIAL_VALUE);
        m_totalTf.setText(INITIAL_VALUE);
        m_totalTf.setEditable(false);

        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("Input"));
        content.add(m_userInputTf);
        content.add(m_multiplyBtn);
        content.add(new JLabel("Total"));
        content.add(m_totalTf);
        content.add(m_clearBtn);

        window.setContentPane(content);
        window.pack();
    }

    public void throwExcept(){
        JOptionPane.showMessageDialog(window, "Bad Number");
    }
}
import javax.swing.*;
import java.math.BigInteger;
import java.awt.*;
public class CalcView extends JFrame{
    private static final String INITIAL_VALUE = "1";

    private JTextField m_totalTf = new JTextField(10);
    private JTextField m_userInputTf = new JTextField(10);
    private JButton m_multiplyBtn = new JButton("Multiply");
    private JButton m_clearBtn = new JButton("Clear");

    private BigInteger m_total; // The total current value state.

    public void CalcGUI() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Performs Simple Multiplications");
        this.setVisible(true);
        m_total = new BigInteger(INITIAL_VALUE);
        m_totalTf.setText(INITIAL_VALUE);
        m_totalTf.setEditable(false);

        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("Input"));
        content.add(m_userInputTf);
        content.add(m_multiplyBtn);
        content.add(new JLabel("Total"));
        content.add(m_totalTf);
        content.add(m_clearBtn);

        this.setContentPane(content);
        this.pack();

    }
    public void throwExcept(){
        JOptionPane.showMessageDialog(CalcView.this, "Bad Number");
    }
}
第二个选项显示在下面的代码中

import javax.swing.*;
import java.math.BigInteger;
import java.awt.*;
public class CalcView{
    private static final String INITIAL_VALUE = "1";

    private JTextField m_totalTf = new JTextField(10);
    private JTextField m_userInputTf = new JTextField(10);
    private JButton m_multiplyBtn = new JButton("Multiply");
    private JButton m_clearBtn = new JButton("Clear");

    private BigInteger m_total; // The total current value state.

    JFrame window = new JFrame();

    public void CalcGUI() {
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Performs Simple Multiplications");
        window.setVisible(true);
        m_total = new BigInteger(INITIAL_VALUE);
        m_totalTf.setText(INITIAL_VALUE);
        m_totalTf.setEditable(false);

        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("Input"));
        content.add(m_userInputTf);
        content.add(m_multiplyBtn);
        content.add(new JLabel("Total"));
        content.add(m_totalTf);
        content.add(m_clearBtn);

        window.setContentPane(content);
        window.pack();
    }

    public void throwExcept(){
        JOptionPane.showMessageDialog(window, "Bad Number");
    }
}
import javax.swing.*;
import java.math.BigInteger;
import java.awt.*;
public class CalcView extends JFrame{
    private static final String INITIAL_VALUE = "1";

    private JTextField m_totalTf = new JTextField(10);
    private JTextField m_userInputTf = new JTextField(10);
    private JButton m_multiplyBtn = new JButton("Multiply");
    private JButton m_clearBtn = new JButton("Clear");

    private BigInteger m_total; // The total current value state.

    public void CalcGUI() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Performs Simple Multiplications");
        this.setVisible(true);
        m_total = new BigInteger(INITIAL_VALUE);
        m_totalTf.setText(INITIAL_VALUE);
        m_totalTf.setEditable(false);

        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("Input"));
        content.add(m_userInputTf);
        content.add(m_multiplyBtn);
        content.add(new JLabel("Total"));
        content.add(m_totalTf);
        content.add(m_clearBtn);

        this.setContentPane(content);
        this.pack();

    }
    public void throwExcept(){
        JOptionPane.showMessageDialog(CalcView.this, "Bad Number");
    }
}
此外,您可以使用构造函数在类之间传递事件处理程序,或者只创建一个setter函数。您可以在此处阅读有关构造函数的更多信息。

此外,如何在类之间传递事件处理程序每个帖子一个问题。