Java 无法在自己创建的私有空隙中工作,但在执行按钮操作时可以
Java not working in self created private void, but on button action performed it does
我对 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 很陌生,在制作我的第一个 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 中显示值的方法, 如果我没记错的话是因为一些错别字。