gpt4 book ai didi

Java 无法在自行创建的 private void 中工作,但在执行按钮操作时却可以

转载 作者:行者123 更新时间:2023-12-01 16:30:03 26 4
gpt4 key购买 nike

我对 java 很陌生,在制作我的第一个 GUI 界面时遇到了一些困难,并且不知道如何寻找解决方案。

所以在 netbeans 中我创建了一个带有一些按钮和文本字段的界面。当我单击其中一个按钮时,将执行代码并调用一个方法,在这个调用的方法中,我尝试更改文本字段的文本,但它不起作用,当我尝试更改方法(执行按钮操作)中的文本字段时,由 netbeans 创建,并且确实有效。

编辑:整个代码:

public class NewJFrame extends javax.swing.JFrame {

//Creat new JFrame form.
public NewJFrame() {
initComponents();
}

//Decalre variables.
List<String> Expressions = new ArrayList<String>();

//我这里删除了netbeans自动生成的代码

//Button number 1.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Call function to show expressions on screen.
showExpressions("1");
}

//Button 2 - 9 exactly the same as 1

//Button back.
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
//Get the size of the list
int size = Expressions.size();

//Remove the last expression from the list
Expressions.remove(size-1);

//Show expressions in terminal.
printExpressions();
}

//Button plus.
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
//Add a comma to the string.
addComma();

//Call function to show expressions on screen.
showExpressions("+");

//Add a comma to the string.
addComma();
}

//Button minus the same as button plus.

//button equal.
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
//Join the list
String joined = String.join("",Expressions);

//Split the list on a ','.
String parts[] = joined.split("[\\,]");

//Declare the outcome and operator.
double outcome = 0;
boolean operator = true;

//Loop through all the parts.
for (String part : parts) {

//Check if part is a number or operator.
if (part.equals("-") || part.equals("+")) {

//part is an operator.
switch (part) {
case "+":
operator = true;
break;
case "-":
operator = false;
break;
}//Switch.

} else {

//Part is a number.
if (operator)
outcome += Double.valueOf(part);
else
outcome -= Double.valueOf(part);

} //Try catch.

}//Foreach.

//Show outcome on screen
System.out.println(outcome);

//Clear the array.
Expressions.clear();

}

//showExpressions. (Add them to the list array.)
private void showExpressions(String value) {

Expressions.add(value);
System.out.println(Expressions);
}

//Add a comma before and after entering an operator, to split afterwards.
private void addComma() {
Expressions.add(",");
}

//Show the expression in the terminal and on the screen.
private void printExpressions() {
//Show the expression in the terminal.
System.out.println(Expressions);
jLabel1.setText("TESTTT");

//Declare variables.
String output = "";

//Generate the numbers shown in the textfield
for (String Expression : Expressions) {
if (!Expression.equals("-") && !Expression.equals("+")) {
output.concat(Expression);
}//If.
}//For.

//Show in textfield on GUI.
jTextField1.setText(output);

}

//Main static void.
public static void main(String args[]) {

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

}

我也知道“show”实际上是执行的,因为在我的项目中它也在终端中输出(确实有效)

最佳答案

由于没有答案并且我看到了我的问题,问题已解决。

问题是在 GUI 中显示值的方法从未被调用,如果我没记错的话,由于一些错字。

关于Java 无法在自行创建的 private void 中工作,但在执行按钮操作时却可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62066655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com