简体   繁体   中英

how to show variable's value in pop up dialog with java swing

I am a beginner in programming. I was messing around with java swing and tried to make a silly app. This is my code. It's not showing any errors, but it's not working. When the dialog pops up, the variable's value doesn't show on the dialog. The logic i used is right, because when i run it in the compiler without the GUI stuff, it works perfectly. If anyone could tell what am i doing wrong in here, it would be really helpful.

this is my code for the calculations.

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class swingApp extends JFrame{
    JLabel label;
    JButton button;
    public JLabel label2;
    JTextField textField = new JTextField("enter you nicknamae");
    String username=textField.getText();
    public int geniusMeter;
    public int sum=0;
    public String sumAsString;
    public void swingAppLogic(String name){
        char[] letters = username.toCharArray();
        char [] alphabet={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        int [] measure={7,3,1,13,8,26,15,18,2,5,22,20,16,9,5,11,23,12,25,6,4,21,24,14,17,10};

        for(int count=0;count<letters.length;count++){
            for (int count1=0;count1<alphabet.length;count1++){
                if (letters[count]==alphabet[count1]){
                    geniusMeter=measure[count1];
                    sum=sum+geniusMeter;
                    break;
                }
            }
        }
        sumAsString = Integer.toString(sum);

    }

    public swingApp(){
        super("Genius Calculator");
        setLayout(new FlowLayout());
        label = new JLabel("Wanna know how genius you are?"); 
        label2 = new JLabel(" ");
        button = new JButton("press enter and then click me to know the results");
        handlerClass handler = new handlerClass();
        button.addActionListener(handler);
        add(label);
        add(label2);
        add(button);
        add(textField);
    }
    public class handlerClass implements ActionListener{
        public void actionPerformed(ActionEvent event){
            JOptionPane.showMessageDialog (null, sumAsString, "Title", JOptionPane.INFORMATION_MESSAGE);

        }
    }
}

and this is my main method

import javax.swing.JFrame;

public class swingAppTester{
    public static void main (String [] args){
        swingApp object=new swingApp();

        object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        object.setSize(500,400);
        object.setVisible(true);
    }
}

You need to use your method swingAppLogic(String name) in the actionPerformed() -method of the button:

public class handlerClass implements ActionListener{
    public void actionPerformed(ActionEvent event){
      swingAppLogic(textField.getText());
        JOptionPane.showMessageDialog (null, sumAsString, "Title", JOptionPane.INFORMATION_MESSAGE);
    }
}

Another problem is, that your sum keeps getting bigger with every run. You need to reset it to 0 at every start:

public void swingAppLogic(String name){
    sum = 0;
    char[] letters = username.toCharArray();

It would also be helpful for you, if you learn a bit about coding standards. For instance class names should start with an uppercase letter to stop confusion in the code. And naming a JLabel label gets confusing incredibly fast when you have more than a handful.

hth!

PS: Your logic for calculating the genius sum seems to output always the same value of 150?

[EDIT]:

You are trying to get the name from the textfield that the user has entered. But you are doing this at the wrong time, while the textfield still contains "enter your nickname". So instead of username.toCharArray() use name.toCharArray() . The variable username then isn't needed anymore.

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