未写入 JFrame
JFrame not being written to
我的 JFrame 打开时一片空白 window。
我试图创建一个 calcView class 的实例,以便打开和写入 JFrame Window,但它没有正常工作。
基本上我必须使用 MVC 方法来开发仅乘法计算器所以我有 3 个单独的 classes 显示在这里:
计算视图
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();
}
}
});
}
}
此外,如何在 class 之间传递事件处理程序?
原因是您将内容面板添加到错误的 JFrame。
在这行代码中,您创建了一个新的 JFrame。
JFrame window = new CalcView();
但是在这些代码行中,您将内容 JPanel 添加到它自己的 CalcView class。
this.setContentPane(content);
this.pack();
有两个选项可以使其可见,第一个是不扩展 JFrame,并将内容 JPanel 添加到 window JFrame。第二个选项是扩展 JFrame 并且不创建新的 window 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");
}
}
此外,您可以使用构造函数在 class 之间传递事件处理程序,或者简单地创建一个 setter 函数。您可以在此处阅读有关构造函数的更多信息 https://www.javatpoint.com/java-constructor.
我的 JFrame 打开时一片空白 window。
我试图创建一个 calcView class 的实例,以便打开和写入 JFrame Window,但它没有正常工作。
基本上我必须使用 MVC 方法来开发仅乘法计算器所以我有 3 个单独的 classes 显示在这里:
计算视图
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();
}
}
});
}
}
此外,如何在 class 之间传递事件处理程序?
原因是您将内容面板添加到错误的 JFrame。 在这行代码中,您创建了一个新的 JFrame。
JFrame window = new CalcView();
但是在这些代码行中,您将内容 JPanel 添加到它自己的 CalcView class。
this.setContentPane(content);
this.pack();
有两个选项可以使其可见,第一个是不扩展 JFrame,并将内容 JPanel 添加到 window JFrame。第二个选项是扩展 JFrame 并且不创建新的 window 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");
}
}
此外,您可以使用构造函数在 class 之间传递事件处理程序,或者简单地创建一个 setter 函数。您可以在此处阅读有关构造函数的更多信息 https://www.javatpoint.com/java-constructor.