如何解决未写入 JFrame
我的 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();
}
}
});
}
}
此外,如何在类之间传递事件处理程序?
解决方法
原因是您向错误的 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 阅读有关构造函数的更多信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。