【问题标题】:JFrame not being written toJFrame 没有被写入
【发布时间】:2021-02-18 01:36:47
【问题描述】:

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

计算视图

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() {
         JFrame window = new CalcView();
        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);

        this.setContentPane(content);
        this.pack();
   
    }
    public void throwExcept(){
        JOptionPane.showMessageDialog(CalcView.this, "Bad Number");
    }
}


计算模型

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();
                }
            }
        });
    }
}

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

【问题讨论】:

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

标签: java swing model-view-controller jframe


【解决方案1】:

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

JFrame window = new CalcView();

但在这些代码行中,您将内容 JPanel 添加到它自己的 CalcView 类中。

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

有两个选项让它可见,第一个是不扩展 JFrame,并将内容 JPanel 添加到窗口 JFrame。第二个选项是扩展 JFrame 并且不创建新窗口 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");
    }
}

此外,您可以使用构造函数在类之间传递事件处理程序,或者简单地创建一个 setter 函数。你可以在这里阅读更多关于构造函数的信息https://www.javatpoint.com/java-constructor。

【讨论】:

    猜你喜欢
    • 2018-07-31
    • 2017-02-17
    • 2012-06-04
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多