有 Java 编程相关的问题?

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

类java ncome税务计算器

我正在尝试将一个类中的方法链接到一个主要的税务计算器程序。 我在我的方法中使用一个Tax类。

我是不是做错了什么,所以主程序无法从Tax类调用方法? 我有一种感觉,那就是我的名字

主要预制代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;

import HW5.Tax;

public class TaxCalculator extends JApplet{
private final JButton btnAmount= new JButton("<html>Tax<br/>Amount</html>");
private final JButton btnMarginal= new JButton("<html>Marginal<br/>Tax Rate</html>");
private final JButton btnAverage= new JButton("<html>Average<br/>Tax Rate</html>");
private final JButton btnTaxable= new JButton("<html>Taxable<br/>Income</html>");
private final JButton btnCalculate= new JButton("<html>Calculate<br/>Taxable Amount</html>");
private final JLabel lblOutput = new JLabel("Result");
private final JLabel lblDummy1 = new JLabel(" ");
private final JLabel lblDummy2 = new JLabel(" ");
private  JFormattedTextField tfTaxable ;
private final JRadioButton radSingle = new JRadioButton("Single", true);
private final JRadioButton radMarried = new JRadioButton("Married");
private  JFormattedTextField tfResult;
JPanel contentPanel = new JPanel(new BorderLayout());
JPanel mainPanel = new JPanel(new GridLayout(3,3,4,4));
JPanel buttonPanel = new JPanel(new GridLayout(1,4,3,4));
private  NumberFormat amountFormat;

private final int SINGLE=1, MARRIED=2;
private int status=SINGLE;

public void init() {
    initFormatter();
    tfTaxable = new JFormattedTextField(amountFormat);
    tfResult = new JFormattedTextField(amountFormat);

    mainPanel.add(new JLabel("Taxable Income:"));
    mainPanel.add(tfTaxable); mainPanel.add(lblDummy1);

    mainPanel.add(new JLabel("Filing Status:"));
    ButtonGroup bg = new ButtonGroup();
    bg.add(radSingle);
    bg.add(radMarried);
    JPanel radioPanel = new JPanel(new GridLayout(1,2,3,4));
    radioPanel.add(radSingle); radioPanel.add(radMarried);
    mainPanel.add(radioPanel); mainPanel.add(lblDummy2);

    mainPanel.add(lblOutput);
    mainPanel.add(tfResult); tfResult.setEditable(false);
    mainPanel.add(btnCalculate);
    btnCalculate.setVisible(false); 
    btnCalculate.setEnabled(false);

    contentPanel.add(mainPanel, BorderLayout.NORTH);

    buttonPanel.add(btnAmount);
    buttonPanel.add(btnMarginal);
    buttonPanel.add(btnAverage);
    buttonPanel.add(btnTaxable);
    contentPanel.add(buttonPanel, BorderLayout.SOUTH);
    contentPanel.setBorder(new EmptyBorder(10, 10, 10, 10) );
    contentPanel.setBorder(
            BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"2012 Tax Year")); 

    this.add(contentPanel);
    this.setVisible(true);

    btnAmount.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            toggleUI(false);
            lblOutput.setText("Tax amount: ");
            double income = ((Number)tfTaxable.getValue()).doubleValue();
            double tax = Tax.getTaxAmount(income, status);
            tfResult.setValue(new Double(tax));

        }
    });

    btnMarginal.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            toggleUI(false);
            lblOutput.setText("Marginal Tax Rate (%): ");
            double income = ((Number)tfTaxable.getValue()).doubleValue();
            double marginalRate = 100*Tax.getMarginalRate(income, status);
            tfResult.setValue(new Double(marginalRate));
        }
    });

    btnAverage.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            toggleUI(false);
            lblOutput.setText("Average Tax Rate (%): ");
            double income = ((Number)tfTaxable.getValue()).doubleValue();
            double averageRate = 100*Tax.getAverageRate(income, status);
            tfResult.setValue(new Double(averageRate));
            /*tfResult.setText(
                    String.format("%,.2f", //change the following method
                            100*Tax.getTaxAmount(getInput(tfTaxable),status) ));*/
        }
    });

    btnTaxable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            lblOutput.setText("After tax amount: ");
            toggleUI(true);
            tfResult.requestFocus();
            tfTaxable.setText("");
        }
    });

    btnCalculate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double postTax = ((Number)tfResult.getValue()).doubleValue();
            double preTax = Tax.getPreTaxAmount(postTax, status);
            tfTaxable.setValue(new Double(preTax));

        }
    });


    radSingle.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TaxCalculator.this.status=TaxCalculator.this.SINGLE;
        }
    });

    radMarried.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TaxCalculator.this.status=TaxCalculator.this.MARRIED;
        }
    });

}


private void toggleUI(boolean getPreTax) {
    tfResult.setEditable(getPreTax);
    btnCalculate.setEnabled(getPreTax);
    btnCalculate.setVisible(getPreTax);
}

private void initFormatter() {
    amountFormat = NumberFormat.getNumberInstance();
    //percentFormat = NumberFormat.getNumberInstance();
    amountFormat.setMinimumFractionDigits(2);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame("Tax Calculator");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            TaxCalculator applet = new TaxCalculator();
            frame.setContentPane(applet);//.getContentPane().add(applet, BorderLayout.CENTER);
            applet.init();
            applet.start();
            //frame.setSize(700, 500);
            frame.pack();
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            frame.setLocation((d.width - frame.getSize().width) / 2,
              (d.height - frame.getSize().height) / 2);
            frame.setVisible(true);
        }
    });

}
}

我在单独计税表中使用的方法有:

getTaxAmount:

public static double getTaxAmount(double income, int status){
    if (status == 1){
        if (income < 8700) return 8700 * .10;
        if (income < 35350) return 8700 * .10 + (income - 8700) * .15;
        if (income < 85650) return 8700 * .10 + (35350 - 8700) * .15 + (income - 35350) * .25;
        if (income < 178650) return 8700 * .10 + (35350 - 8700) * .15 + (85650 - 35350) * .25 + (income - 85650) * .28;
        if (income < 335000) 
            return 8700 *.10 + (35350 - 8700) * .15 + (75000 - 35350) * .25 + (115000 - 85650) * .28 + (income - 178650) * .33;
        else
            return income * .35;
        }
    else{}
        return income;

getAverageRate

    public static double getAverageRate(double income, int status){
        double averageRate;
        if(status == 1){
            if (income < 8700) 
                return ((income * .10) / income);
            if (income < 35350) 
                return ((8700 * .10 + (income - 8700) * .15) / income);
            if (income < 75000) 
                return ((8700 * .10 + (35350 - 8700) * .15 + (income - 35350) * .20) / income);
            if (income < 115000) 
                return ((8700 * .10 + (35350 - 8700) * .15 + (75000 - 35350) * .20 + (income - 75000) * .25) / income);
            if (income < 335000) 
                return ((8700 *.10 + (35350 - 8700) * .15 + (75000 - 35350) * .20 + (115000 - 75000) * .25 + (income - 115000) * .30) / income);
            else{}
                return averageRate;     


        }           


    return averageRate;
    }   

共 (1) 个答案

  1. # 1 楼答案

    如果您的方法与主方法位于一个单独的类文件中,那么原因是因为static只是局部作用域。去掉方法签名中的静态部分,您应该不会有问题。或者,如果将方法移动到与主方法相同的类文件中,请保留静态修饰符,并且它应该仍然有效