简体   繁体   中英

Different Action depending on what radio button is chosen

class personalFrame {

    JTextField totalIncome = new JTextField(10);
    private JFrame frame3 = new JFrame("Personal Tax Calculator");
    JButton Calculate = new JButton("Calculate");
     JRadioButton residentTax = new JRadioButton("Resident Tax");
     JRadioButton nonresidentTax = new JRadioButton("Working Tax");
     JRadioButton workingTax = new JRadioButton("Non-working Tax");

    public personalFrame() {

        frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame3.setSize(300, 100);
        frame3.setVisible(true);
        frame3.setLayout(new FlowLayout());

        frame3.add(new JLabel("Total Income "));
        frame3.add(totalIncome);
        frame3.add(Calculate);
        frame3.add(residentTax);
        frame3.add(nonresidentTax);
        frame3.add(workingTax);

        Calculate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String Income = totalIncome.getText();
                Double totalIncome = Double.parseDouble(Income);
                double expenseTax = 0;
                double totalTax = totalIncome - expenseTax;
                String Tax = String.valueOf(totalTax);
                JOptionPane.showMessageDialog(null, "Tax payable is A$" + Tax, "Total tax", JOptionPane.INFORMATION_MESSAGE);

            }

        });

           residentTax.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ie){
                double expenseTax = 1000;
            }
        });

           nonresidentTax.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ie){
                 double expenseTax = 1500;

            }
        });

           workingTax.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ie){
                 double expenseTax = 2000;

            }
        });

    }
}

The above Code is for a Calculate Tax program im working on. This frame is an option that a user can choose from. Once here they can enter the total income and the tax will be calculated(im yet to link the method to this, right now its just a placeholder calculation until the buttons wokr)

Im new to jswing so im a bit confused on the functions. I want the double eexpenseTax in the calculator ActionListener to be equal to whatever Radio button is chosen by the user(resident, nonresident or working tax, each with their own expenseTax varialbe)

how does one achieve this? thankyou

JButton Calculate = new JButton("Calculate");

Variable names should NOT start with an upper case character. Be consistent!

double expenseTax = 0;
double totalTax = totalIncome - expenseTax;

The above code makes no sense. The value of expenseTax is always zero.

double expenseTax = 1000;

The code from your ActionListeners also does nothing you are defining a "local variable" which can't be used anywhere else in your program.

So the solution is to use an "instance variable" in your class. The JRadioButton ActionListeners will update this variable. Then the JButton ActionListener will use this variable in the calculation.

So where you define the buttons you define the variable:

private couble expenseTax;

In the JRadioButton listeners you then use:

//double expenseTax = 1000;
expenseTax = 1000;

And finally in the JButton ActionListener you use:

//double expenseTax = 0;
double totalTax = totalIncome - expenseTax;

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