简体   繁体   中英

Difficulty with Swing

Currently I'm programming a java app (swing app) and I'm facing a problem while showing " percentage " in a text field. To be more accurate I'll post the code here and I'll mark where is the problem.

Can anyone help me please ?

The 1st class:

public void actionPerformed(ActionEvent ae)
{
    String s=ae.getActionCommand();
    double salary=Integer.parseInt(t12.getText());

    if(s.equals("Clothes"))
    {
        int i=Integer.parseInt(amount.getText());
        sum1=i+sum1;
        t1.setText("You spend "+Integer.toString(sum1)+" in Clothes");
        w2.t1.setText(Integer.toString(sum1));
        per=(sum1/salary)*100;
        S.t11.setText(Double.toString(per));     
    }
    else if(s.equals("Food"))
    {
        int i=Integer.parseInt(amount.getText());
        sum2=i+sum2;
        t1.setText("You spend "+Integer.toString(sum2)+" in Food");
        w2.t2.setText(Integer.toString(sum2));
        per=(sum2/salary)*100;
        S.t22.setText(Double.toString(per));
    }
    else if(s.equals("Bills"))
    {
        int i=Integer.parseInt(amount.getText());
        sum3=i+sum3;
        t1.setText("You spend "+Integer.toString(sum3)+" in Bills");
        w2.t3.setText(Integer.toString(sum3));
        per=(sum3/salary)*100;
        S.t33.setText(Double.toString(per));
    }
    else if(s.equals("Transportation"))
    {
        int i=Integer.parseInt(amount.getText());
        sum4=i+sum4;
        t1.setText("You spend "+Integer.toString(sum4)+" in Transportation");
        w2.t4.setText(Integer.toString(sum4));
        per=(sum4/salary)*100;
        S.t44.setText(Double.toString(per));
    }
    else if(s.equals("Others"))
    {
        int i=Integer.parseInt(amount.getText());
        sum5=i+sum5;
        t1.setText("You spend "+Integer.toString(sum5)+" in Others");
        w2.t5.setText(Integer.toString(sum5));
        per=(sum5/salary)*100;
        S.t55.setText(Double.toString(per));
    }
    else if(s.equals("Next"))
    {
        t1.setText(null);
        amount.setText(null);
        t13.setText(Double.toString(salary-(sum1+sum2+sum3+sum4+sum5)));       
    }
    else if(s.equals("Check"))
    {
        w2.setVisible(true);
        w2.setSize(600,400);  
    }
    else
    System.exit(0);

The 2nd class :

public void actionPerformed(ActionEvent ae)
{
    String s=ae.getActionCommand();
    Wallet w=new Wallet();
    Statistics S=new Statistics();

    else if (s.equals("Statistics")){
        S.setVisible(true);
        S.setSize(600,400);
        S.t11.setText(Double.toString(w.per));
        S.t22.setText(Double.toString(w.per));
        S.t33.setText(Double.toString(w.per));
        S.t44.setText(Double.toString(w.per));
        S.t55.setText(Double.toString(w.per));
    }
}

but it comes like this (zeros instead of the percentage )

because int sum1,sum2,sum3..; and per = sum1\\*integer type*\\/salary\\*integer type*\\ -> per is interger. Try this:

double per; 
per = (double)sum1/(double)salary;
per = (double)sum3/(double)salary;
.....

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