有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

netbeans Java不在自己创建的私有void中工作,但在执行按钮操作时工作

我对java非常陌生,在制作第一个GUI界面时遇到了一些困难,不知道如何搜索解决方案

因此,在netbeans中,我创建了一个带有一些按钮和文本字段的界面。当我点击其中一个按钮时,代码被执行并调用一个方法,在这个被调用的方法中,我尝试更改textfield的文本,但它不起作用,当我尝试更改netbeans创建的方法(button actionperformed)中的textfield时,它起作用了

编辑:整个代码:

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”实际上是执行的,因为在我的项目中,它也会在终端中输出(它确实工作)


共 (1) 个答案

  1. # 1 楼答案

    既然没有答案,我看到了我的问题, 问题已经解决了

    问题是在GUI中显示值的方法从未被调用, 如果我没记错的话,是因为打字错误