Results 1 to 3 of 3
- 11-21-2010, 02:02 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
no return- problem with actionlistener
I'm trying to write a program that calculates tax for user inputted income and filing status. i've wrote everything so that the compiler proceeds without error, but upon running the program, nothing is returned. i'm thinking it is a problem with my action listener. help?
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Trying extends JFrame implements ActionListener { BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); public JPanel content; public JLabel income; public JTextField field; public JLabel status; public JButton calculate; public JLabel result; public JTextField amount; public JComboBox box; double i; int s; String tax; public Trying(){ content = new JPanel(); content.setLayout(new FlowLayout()); income = new JLabel("Enter Income:"); content.add(income); field = new JTextField(20); content.add(field); status = new JLabel("Choose a filing status"); content.add(status); String[] filing = {"Married", "Single", "Married filing separately", "Qualifying Widow or Widower", "Head of Household"}; box = new JComboBox(filing); content.add(box); calculate = new JButton("Calculate Tax"); content.add(calculate); result = new JLabel("Your tax is:"); content.add(result); amount = new JTextField(20); content.add(amount); amount.setText(this.calculate(i)); calculate.addActionListener(this); setContentPane(content); pack(); setTitle("Income Tax Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); } public double single(double i) { if (i <= 6000) return(.1*i); else if (i <= 27950) return((.1*6000) + (.15*(i-6000))); else if (i <= 67700) return((.1*6000) + (.15*(27950-6000)) + (.27*(i-27950))); else if (i <= 141250) return((.1*6000) + (.15*(27950-6000)) + (.27*(67700-27950)) + (.3*(i-67700))); else if (i <= 307050) return((.1*6000) + (.15*(27950-6000)) + (.27*(67700-27950)) + (.3*(141250-67700)) + (.35*(i-141250))); else return((.1*6000) + (.15*(27950-6000)) + (.27*(67700-27950)) + (.3*(141250-67700)) + (.35*(307050-141250)) + (.386*(i-307050))); } public double married(double i) { if (i <= 12000) return(.1*i); else if (i <= 46700) return((.1*12000) + (.15*(i-12000))); else if (i <= 112850) return((.1*12000) + (.15*(46700-12000)) + (.27*(i-47600))); else if (i <= 171950) return((.1*12000) + (.15*(46700-12000)) + (.27*(112850-46700)) + (.3*(i-112850))); else if (i <=307050 ) return((.1*12000) + (.15*(46700-12000)) + (.27*(112850-46700)) + (.3*(171950-112850)) + (.35*(i-307050))); else return((.1*12000) + (.15*(46700-12000)) + (.27*(112850-46700)) + (.3*(171950-112850)) + (.35*(307050-171950)) + (.389*(i-307050))); } public double fileseparate(double i) { if (i <= 6000) return(.1*i); else if (i <= 23350) return((.1*6000) + (.15*(i-6000))); else if (i <= 56425) return((.1*6000) + (.15*(23350-6000)) + (.27*(i-23350))); else if (i <= 85975) return((.1*6000) + (.15*(23350-6000)) + (.27*(67700-23350)) + (.3*(i-56425))); else if (i <= 153525) return((.1*6000) + (.15*(23350-6000)) + (.27*(56425-23350)) + (.3*(85975-56425)) + (.35*(i-85975))); else return((.1*6000) + (.15*(23350-6000)) + (.27*(56425-23350)) + (.3*(85975-56425)) + (.35*(153525-85975)) + (.386*(i-153525))); } public double household(double i) { if (i <= 10000) return(.1*i); else if (i <= 37450) return((.1*10000) + (.15*(i-10000))); else if (i <= 96700) return((.1*10000) + (.15*(37450-10000)) + (.27*(i-37450))); else if (i <= 156600) return((.1*10000) + (.15*(37450-10000)) + (.27*(96700-37450)) + (.3*(i-96700))); else if (i <= 307050) return((.1*10000) + (.15*(37450-10000)) + (.27*(96700-37450)) + (.3*(156600-96700)) + (.35*(i-156600))); else return((.1*10000) + (.15*(37450-10000)) + (.27*(96700-37450)) + (.3*(156600-96700)) + (.35*(307050-156600)) + (.386*(i-307050))); } public void actionPerformed(ActionEvent evt){ if (evt.getSource()== calculate) {String income = field.getText(); i=Double.parseDouble(income);}; if (evt.getSource()==calculate) {int status = box.getSelectedIndex(); s=status;}; if (evt.getSource() == calculate) this.calculate(i); } public String calculate(double i){ if(s == 0) return Double.toString(this.single(i)); else if (s == 1) return Double.toString(this.married(i)); else if (s == 2) return Double.toString(this.fileseparate(i)); else if (s == 3) return Double.toString(this.fileseparate(i)); else if (s == 4) return Double.toString(this.household(i)); else return ("Error"); } public static void main(String args[]) { Trying trying = new Trying(); trying.setVisible(true); } }Last edited by lparadiso; 11-21-2010 at 02:04 AM. Reason: small typo
-
You set the text in your amount JTexField only once, here:
But this occurs when the GUI is created before any useful data has been entered and before any buttons have been pressed, and since you never call setText on this method ever again, it never changes. No matter how many times calculate is called, there is no way that it can retroactively change the value held by this JTextField. I think that you will want to call setText on this field from within the calculate method.Java Code:amount = new JTextField(20); content.add(amount); amount.setText(this.calculate(i));
- 11-21-2010, 02:31 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Problem implementing ActionListener??
By ryanonnfire54 in forum New To JavaReplies: 3Last Post: 11-16-2010, 04:39 PM -
Problem Actionlistener
By Questionmark in forum New To JavaReplies: 18Last Post: 08-07-2010, 12:29 PM -
Problem w/ ActionListener on a button
By qwertyuiop23 in forum AWT / SwingReplies: 2Last Post: 11-02-2009, 06:25 AM -
ActionListener run automatically problem
By cassysumandak in forum New To JavaReplies: 1Last Post: 03-23-2009, 09:42 PM -
ActionListener Applet problem
By xander5511 in forum Java AppletsReplies: 1Last Post: 02-21-2009, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks