首页 \ 问答 \ 需要帮忙。(Need help. Im trying to create a combobox and some textfields for a class project on taxable income)

需要帮忙。(Need help. Im trying to create a combobox and some textfields for a class project on taxable income)

我正在为CS类创建一个税务项目。 我正在使用netbeans。 我的项目完全正常工作,除了我总是得到的税收返回值是0.0。 我是Java新手。 我认为问题出在我的“收入”变量的某个地方。 用户输入他们的应税收入,我取用户输入的字符串并将其解析为整数并称之为“收入”。 然后程序检查“收入”以查看它是否大于或小于某些值,以查看用户所在的税率范围。税率范围受用户组合框选择“填充状态”的影响。 到目前为止,我的程序只有最小的税率和单一的填充状态用于调试目的...一旦我得到这个代码工作,我应该能够相当容易地填写其余部分。 我想知道是否有人可以查看代码,看看它是什么我错过了。 我很难将组合框与用户jtextfield条目连接起来进行计算。 我不确定我的问题是在我的数组中,在我的“收入”变量中,还是在其他地方。 我花了很多时间浏览书籍和java教程。 如果您可以一瞥并提供帮助,那将不胜感激:)

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class IncomeTax2 extends JFrame implements ItemListener
{

    private JTextField jtfIncome;
    private JTextField jtfTax;
    private JButton jbCalculate;
    private JComboBox jcb;
    private JLabel info;
    private String[] fsChoice =
    {
        "", "Single", "Married filing jointly", "Married filing separately",
        "Head of Household"
    };
    int income;
    double tax;
    private String fillingStatus;

    public static void main(String[] args)
    {
        JFrame frame = new IncomeTax2();
        frame.pack();
        frame.setTitle("Income Tax Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public IncomeTax2()
    {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(3, 2, 5, 5));
        p.setBorder(new EmptyBorder(5, 5, 5, 5));
        p.add(jcb = new JComboBox(fsChoice));
        p.add(jtfIncome = new JTextField(8));
        p.add(info = new JLabel("Pick filling status and "
                + "enter taxable income"));
        p.add(jbCalculate = new JButton("Calculate"));
        p.add(jtfTax = new JTextField(16));
        jtfTax.setEditable(false);
        setLayout(new BorderLayout());
        jtfTax.setHorizontalAlignment(JTextField.RIGHT);
        add(p, BorderLayout.CENTER);

        jtfIncome.addKeyListener(new KeyAdapter()
        {
            public void keyTyped(KeyEvent e)
            {
                char c = e.getKeyChar();
                if (!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || 
                        c == KeyEvent.VK_DELETE || c == KeyEvent.VK_ENTER))
                {
                    Toolkit.getDefaultToolkit().beep();
                    System.out.println("User must enter a digit.");
                    e.consume();
                }
            }
        });

        jcb.addItemListener(this);

        jbCalculate.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                calculate();
            }
        });

         jtfIncome.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String inputStr = jtfIncome.getText();
                income = Integer.parseInt(inputStr);
            }
        });
    }

    public void itemStateChanged(ItemEvent e)
    {
        if (e.getStateChange() == ItemEvent.SELECTED)
        {
            fillingStatus = (String) jcb.getSelectedItem();
            switch (fillingStatus)
            {
                case "Single":
                    if (income <= 8350)
                    {
                        tax = income * 0.10;
                        System.out.println("case[1]");
                        System.out.println("Tax = " + tax);
                    }
                    ;
                    break;
            }
        }
    }

    public void calculate()
    {
        jtfTax.setText(((int)(tax * 100) / 100.0 + ""));
    }

}

I'm creating a tax project for a CS class. Im working with netbeans. My project is working entirely except for the fact that the return value I always get for the tax is 0.0 . I am new to Java. I think that the problem lies somewhere with my "income" variable. The user inputs their taxable income, I take that string the user entered and parse it into an integer and call it "income". The program then checks the "income" to see if it is greater or less than certain values to see what tax bracket the user is in. The tax bracket is affected by the user's combobox choice for "filling status". So far my program only has the smallest tax bracket and the filling status of single for debugging purposes... once i get this code working I should be able to fill in the rest fairly easily. I'm wondering if anyone can look over the code and see what it is I'm missing. Im having a hard time connecting the combobox with the user jtextfield entry to make a calculation. Im not sure if my problem lies in my array, in my "income" variable, or somewhere else. I've spent a ton of time looking through books and java tutorials. If you can take a glance and help, that would be appreciated :)

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class IncomeTax2 extends JFrame implements ItemListener
{

    private JTextField jtfIncome;
    private JTextField jtfTax;
    private JButton jbCalculate;
    private JComboBox jcb;
    private JLabel info;
    private String[] fsChoice =
    {
        "", "Single", "Married filing jointly", "Married filing separately",
        "Head of Household"
    };
    int income;
    double tax;
    private String fillingStatus;

    public static void main(String[] args)
    {
        JFrame frame = new IncomeTax2();
        frame.pack();
        frame.setTitle("Income Tax Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public IncomeTax2()
    {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(3, 2, 5, 5));
        p.setBorder(new EmptyBorder(5, 5, 5, 5));
        p.add(jcb = new JComboBox(fsChoice));
        p.add(jtfIncome = new JTextField(8));
        p.add(info = new JLabel("Pick filling status and "
                + "enter taxable income"));
        p.add(jbCalculate = new JButton("Calculate"));
        p.add(jtfTax = new JTextField(16));
        jtfTax.setEditable(false);
        setLayout(new BorderLayout());
        jtfTax.setHorizontalAlignment(JTextField.RIGHT);
        add(p, BorderLayout.CENTER);

        jtfIncome.addKeyListener(new KeyAdapter()
        {
            public void keyTyped(KeyEvent e)
            {
                char c = e.getKeyChar();
                if (!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || 
                        c == KeyEvent.VK_DELETE || c == KeyEvent.VK_ENTER))
                {
                    Toolkit.getDefaultToolkit().beep();
                    System.out.println("User must enter a digit.");
                    e.consume();
                }
            }
        });

        jcb.addItemListener(this);

        jbCalculate.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                calculate();
            }
        });

         jtfIncome.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String inputStr = jtfIncome.getText();
                income = Integer.parseInt(inputStr);
            }
        });
    }

    public void itemStateChanged(ItemEvent e)
    {
        if (e.getStateChange() == ItemEvent.SELECTED)
        {
            fillingStatus = (String) jcb.getSelectedItem();
            switch (fillingStatus)
            {
                case "Single":
                    if (income <= 8350)
                    {
                        tax = income * 0.10;
                        System.out.println("case[1]");
                        System.out.println("Tax = " + tax);
                    }
                    ;
                    break;
            }
        }
    }

    public void calculate()
    {
        jtfTax.setText(((int)(tax * 100) / 100.0 + ""));
    }

}

原文:https://stackoverflow.com/questions/22493102
更新时间:2023-05-06 22:05

最满意答案

Slippy,通常您将一组客户端放入单个应用程序实例中,并在需要时启动一个新客户端。 但是如果需要,可以为每个用户运行单独的应用程序实例。 看看提供实例隔离的Docker。 如果您想在单独的服务器上运行每个实例,您需要拥有一个托管,允许您通过API添加新机器(如DigitalOcean,例如: https//www.digitalocean.com/community/tutorials/how-to- use-the-digitalocean-api-v2 )。


Slippy, usually you put a set of clients into a single instance of application and launch a new one when required. But if you want, you can run a separate instance of application for each user. Have a look at Docker that provides isolation of instances. If you want to run each instance at separate server you need to have a hosting that allows you to add new machines via API (like DigitalOcean, for example: https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-api-v2).

相关问答

更多
  • 如果您想将计算机上的TCP端口8001转发到手机上的端口8001,则可以使用以下命令: adb forward tcp:8001 tcp:8001 如果需要,您可以更改手机或设备上的端口。 该命令的文档位于以下位置: http : //developer.android.com/guide/developing/tools/adb.html#forwardports 至于为什么这不应该工作 - 有一些信息从你的问题中遗漏。 这些只是标准的TCP套接字。 Java版本不应该有任何区别,所以我不明白你的问题。 ...
  • 没有大剂量的密码术就无法做到明智。 只要另一端完全控制,他们就无法伪造答案。 Kerberos所做的事情模糊地相似,协议非常复杂。 罗斯安德森的“安全工程”将是我第一次看看如何做到这一点。 Can't be done sensibly without a heavy dose of cryptography. As long as the other end has complete control, there just is no way they can't fake the answers. Som ...
  • 我终于联系了开发Maven Ear插件的人Stephane Nicolle,他刚刚创建了一个maven-acr-plugin Java EE应用程序客户端,并容纳了Maven EAR插件来识别新的app-client打包类型。 因此,对于Maven EAR插件2.6,Java EE app客户端有官方maven支持 谢谢 I finally contacted Stephane Nicolle, the person who developed Maven Ear plugin and he just cr ...
  • 如果要直接部署EJB JAR,那么查找应该是 UserDataRemote userRemote = (UserDataRemote) namingContext.lookup("testEJB/UserData!entities.UserDataRemote"); 如果EJB JAR打包在EAR中,那么您需要添加EAR名称(应用程序名称) UserDataRemote userRemote = (UserDataRemote) namingContext.lookup("/t ...
  • 好吧,我发现我需要的是调用window.location ="http://localhost:8008/logout"; (Authorozation服务器的URL),并在我的中央授权服务器中创建一个自定义logoutSuccessHandler,以在成功注销后返回到客户端引用URL @Override public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authenti ...
  • 右键单击解决方案,单击“属性”,选择“启动项目”,然后将两个项目都设置为运行。 Right-click the solution, click Properties, select Startup Project, and set both projects to run.
  • Slippy,通常您将一组客户端放入单个应用程序实例中,并在需要时启动一个新客户端。 但是如果需要,可以为每个用户运行单独的应用程序实例。 看看提供实例隔离的Docker。 如果您想在单独的服务器上运行每个实例,您需要拥有一个托管,允许您通过API添加新机器(如DigitalOcean,例如: https : //www.digitalocean.com/community/tutorials/how-to- use-the-digitalocean-api-v2 )。 Slippy, usually yo ...
  • 独立应用程序可以在没有任何网络连接的情况下工作,但胖客户端需要定期网络连接。 在您的情况下,您的应用程序需要通过网络连接到数据库服务器,因此它将被称为胖客户端。 这些术语使用起来很宽松,所以不要太担心。 A standalone application can work without any network connection, but a thick client needs periodical network connection. In your case your application re ...
  • 您可能想要检查以下Stack Overflow帖子的已接受答案,该帖子描述了如何使用服务器端的PHP实现长轮询的一个非常基本的示例: 简单的“长轮询”示例代码 使用长轮询时,您的客户端应用程序会启动对HTTP服务器的请求,并具有无限超时(或非常长的超时)。 现在,只要有新数据,服务器就会找到一个活动连接就绪,因此它可以立即推送数据。 在传统轮询中,您必须等到应用程序启动新轮询,再加上在发送新数据之前到达服务器的网络延迟。 然后,当发送数据时,连接将关闭,但您的应用程序应立即打开一个新连接,以便始终与服务器建 ...
  • Java在服务器端(您的服务器)工作,除非您像客户端或JavaFX应用程序一样将Java应用程序发送到客户端,否则不会在客户端上运行。 如果仅使用Web应用程序,则无法使用Java完成此操作,如果您使用的是Applet / JavaFX方法,则可以获取该信息,但可能无法将其发送到服务器。 IMO在Web应用程序中使用这些功能是非常不安全的。 想象一下一个Web应用程序,访问它的每个人和Web应用程序都可以访问您的SD卡,检索所有内容并通过网络将它们发送到服务器端,从而可以访问您的所有信息和每个用户的信息。访 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)