java - 在 Java 程序的 GUI 中打印结果的小问题
问题描述
图形界面照片:
我正在创建一个简单的 Java 程序,其中用户输入一个数字,然后单击计算按钮,输入数字的平方根的结果出现在该字段中。但是我在制作这个小程序时遇到了麻烦。供用户输入输入数字的字段称为 inputText。我创建了一个名为 inputText 的字符串变量来接收用户以字符串格式输入的内容。然后我创建了一个名为 inputValueInteger 的整数变量来转换为整数。在计算结果的按钮中,我调用了calcButton
. 在这个按钮的方法中,我调用了calculate( inputValueInInteger
)方法。这个inputValueInInteger
变量必须取自resultFieldActionPerformed
。在calcButtonActionPerformed
方法中,我调用了calculate( inputValueInInteger
)方法。这种方法(inputValueInInteger
) 计算作为参数传递的数字的平方的结果。但是,我无法让 GUIresultField
字段打印结果。请问有人可以帮忙吗?
public class Calc extends javax.swing.JFrame {
int inputValueInInteger;
public Calc() {
initComponents();
}
private void inputTextActionPerformed(java.awt.event.ActionEvent evt) {
String inputFieldInString = inputText.getText().toString();
inputValueInInteger = Integer.parseInt(inputFieldInString);
}
private void resultFieldActionPerformed(java.awt.event.ActionEvent evt) {
int result = calculate(inputValueInInteger);
String resultInString = String.valueOf(result);
resultField.setText(resultInString);
}
private void calcButtonActionPerformed(java.awt.event.ActionEvent evt) {
calculate(inputValueInInteger);
}
public int calculate(inputValueInInteger) {
return inputValueInInteger * inputValueInInteger;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InterfaceGrafica().setVisible(true);
calculate(inputValueInInteger);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton calcButton;
private javax.swing.JTextField inputText;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField resultField;
// End of variables declaration
}
解决方案
推荐阅读
- python - 如何从多个子图中迭代地删除 X 轴标签
- python - 改变刻度 matplotlib 的频率
- python - Windows 上的 Python mecab 包导入错误“未定义”
- html - spring boot 从文件目录提供静态图片
- java - 在 Android 项目中使用 java.lang.ref.Cleaner
- typescript - TypeScript 类型错误和枚举对象道具的方法
- tableau-api - 在 Tableau 中从州到县地图的交互式钻取
- .net - .Net Api 角度认证
- javascript - CS50 未捕获类型错误:无法将属性“禁用”设置为 null
- python - tkinter 的 PhotoImages 真的需要根窗口吗?为什么?