Java radio buttons -
i'm having trouble radio buttons. can click on 3 @ same time. should when click on 1 other 1 turns off?
i'm using grid layout. when try group.add doesn't work.
example:
i have buttons declared this
jradiobutton 7 = new jradiobutton("7 years @ 5.35%", false); jradiobutton fifteen = new jradiobutton("15 years @ 5.5%", false); jradiobutton thirty = new jradiobutton("30 years @ 5.75%", false); buttongroup group = new buttongroup(); grid.add(seven); grid.add(fifteen); grid.add(thirty);
this code:
/*change request #6 write program in java (with graphical user interface) allow user select way want calculate mortgage: input of amount of mortgage, term of mortgage, , interest rate of mortgage payment or input of amount of mortgage , select menu of mortgage loans: - 7 year @ 5.35% - 15 year @ 5.5 % - 30 year @ 5.75% in either case, display mortgage payment amount , then, list loan balance , interest paid each payment on term of loan. allow user loop , enter new amount , make new selection, or quit. insert comments in program document program. */ import java.text.numberformat; import javax.swing.*; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class wk4 extends jframe implements actionlistener { int loanterms[] = { 7, 15, 30 }; double annualrates[] = { 5.35, 5.5, 5.75 }; // labels jlabel amountlabel = new jlabel(" loan amount $ "); jlabel paymentlabel = new jlabel(" monthly payment "); jlabel interestlabel = new jlabel(" interest rate % "); jlabel termlabel = new jlabel(" years of loan "); // text fields jtextfield mortgageamount = new jtextfield(6); jtextfield payment = new jtextfield(6); jtextfield interestrate = new jtextfield(3); jtextfield term = new jtextfield(3); // radio buttons buttongroup radiogroup = new buttongroup(); jradiobutton 7 = new jradiobutton("7 years @ 5.35%"); jradiobutton fifteen = new jradiobutton("15 years @ 5.5%"); jradiobutton thirty = new jradiobutton("30 years @ 5.75%"); // buttons jbutton exitbutton = new jbutton("exit"); jbutton resetbutton = new jbutton("reset"); jbutton calculatebutton = new jbutton("calculate "); // text area jtextarea loanpayments = new jtextarea(20, 50); jtextarea grapharea = new jtextarea(20, 50); jscrollpane scroll = new jscrollpane(loanpayments); public wk4(){ super("mortgage calculator"); //window setsize(700, 400); setlocation(200, 150); setdefaultcloseoperation(jframe.exit_on_close); jpanel pane = new jpanel(new gridlayout(2, 2)); container grid = getcontentpane(); grid.setlayout(new gridlayout(4, 10, 0, 12)); //rows, cols, hgap, vgap pane.add(grid); pane.add(scroll); grid.add(amountlabel); grid.add(mortgageamount); grid.add(interestlabel); grid.add(interestrate); grid.add(termlabel); grid.add(term); grid.add(paymentlabel); grid.add(payment); grid.add(box.createhorizontalstrut(15)); buttongroup group = new buttongroup(); grid.add(seven); grid.add(fifteen); grid.add(thirty); grid.add(calculatebutton); grid.add(resetbutton); grid.add(exitbutton); payment.seteditable(false); setcontentpane(pane); setcontentpane(pane); setvisible(true); // action listeners mortgageamount.addactionlistener(this); interestrate.addactionlistener(this); term.addactionlistener(this); payment.addactionlistener(this); seven.addactionlistener(this); fifteen.addactionlistener(this); thirty.addactionlistener(this); calculatebutton.addactionlistener(this); exitbutton.addactionlistener(this); resetbutton.addactionlistener(this); } public void actionperformed(actionevent e) { object command = e.getsource(); if (command == exitbutton) { system.exit(0); } else if (command == seven) { calcloan(loanterms[0], annualrates[0]); } else if (command == fifteen) { calcloan(loanterms[1], annualrates[1]); } else if (command == thirty) { calcloan(loanterms[2], annualrates[2]); } else if (command == calculatebutton) { double years = 0; double rates = 0; try { years = double.parsedouble(term.gettext()); rates = double.parsedouble(interestrate.gettext()); } catch (exception ex) { loanpayments.settext("invalid amount"); return; } calcloan(years, rates); } else if (command == resetbutton) { mortgageamount.settext(""); payment.settext(""); interestrate.settext(""); term.settext(""); loanpayments.settext(""); } } private void calcloan(double years, double rates) { term.settext(string.valueof(years)); interestrate.settext(string.valueof(rates)); double amount = 0; try { amount = double.parsedouble(mortgageamount.gettext()); } catch (exception ex) { loanpayments.settext("invalid amount"); return; } double interestrate = rates; double intrate = (interestrate / 100) / 12; int months = (int) years * 12; double rate = (intrate / 12); double payment = amount * intrate / (1 - (math.pow(1 / (1 + intrate), months))); double remainingprincipal = amount; double monthlyinterest = 0; double monthlyamt = 0; numberformat currencyformatter = numberformat.getcurrencyinstance(); payment.settext(currencyformatter.format(payment)); loanpayments.settext(" month\tprincipal\tinterest\tending balance\n"); int currentmonth = 0; while (currentmonth < months) { monthlyinterest = (remainingprincipal * intrate); monthlyamt = (payment - monthlyinterest); remainingprincipal = (remainingprincipal - monthlyamt); loanpayments.append((++currentmonth) + "\t" + currencyformatter.format(monthlyamt) + "\t" + currencyformatter.format(monthlyinterest) + "\t" + currencyformatter.format(remainingprincipal) + "\n"); grapharea.append("" + remainingprincipal); } } public static void main(string[] args) { new wk4(); } }
you're adding radio buttons grid need add them button group defined.
maybe this:
buttongroup group = new buttongroup(); grid.add(seven); grid.add(fifteen); grid.add(thirty);
should this: ??? (copy/paste bug?)
buttongroup group = new buttongroup(); group.add(seven); group.add(fifteen); group.add(thirty);
or maybe need both. radio buttons have belong container button group displayed , behave properly.
Comments
Post a Comment