简体   繁体   中英

Java not working in self created private void, but on button action performed it does

I'm quite new to java and on making my first GUI interface it ran into some difficulties and don't know how to search for a solution.

so in.netbeans i created an interface with some buttons and a textfield. When i click on one of the buttons code is executed and a method is called, in this called method i tried changing the text of the textfield but it does not work, when i tried to change the textfield in a method (button actionperformed) which is created by.netbeans and there is does work.

EDIT: Entire code:

public class NewJFrame extends javax.swing.JFrame {

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

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

//I deleted the code that is automatically generated by.netbeans here

//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                   

}

I also know 'show' is actually executed since in my project it also outputs in the terminal (which does work)

Since there is not an answer and I saw my question, The problem has been resolved.

The problem being that the method that showed the value in the GUI was never called, due to some typo if I remember correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM