2011-07-21 72 views
1

我在單選按鈕時遇到問題。我可以同時點擊所有三個。它應該是當你點擊一個另一個關閉?Java單選按鈕

我正在使用網格佈局。所以當我嘗試group.add時,它不起作用。

例子:

我已經聲明如下

JRadioButton seven = new JRadioButton("7 years at 5.35%", false); 
JRadioButton fifteen = new JRadioButton("15 years at 5.5%", false); 
JRadioButton thirty = new JRadioButton("30 years at 5.75%", false); 

ButtonGroup group = new ButtonGroup();   
grid.add(seven); 
grid.add(fifteen); 
grid.add(thirty); 

按鈕這是我的代碼:

/*Change Request #6 
Write the program in Java (with a graphical user interface) 
so that it will allow the user to select which way 
they want to calculate a mortgage: 
by input of the amount of the mortgage, 
the term of the mortgage, 
and the interest rate of the mortgage payment 
or by input of the amount of a mortgage and 
then select from a menu of mortgage loans: 

- 7 year at 5.35% 
- 15 year at 5.5 % 
- 30 year at 5.75% 

In either case, display the mortgage payment amount 
and then, list the loan balance and interest paid 
for each payment over the term of the loan. 
Allow the user to loop back and enter a new amount 
and make a new selection, or quit. 
Insert comments in the program to document the 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 seven = new JRadioButton("7 years at 5.35%"); 
    JRadioButton fifteen = new JRadioButton("15 years at 5.5%"); 
    JRadioButton thirty = new JRadioButton("30 years at 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(); 
    } 

    } 
+1

它看起來像你和[這傢伙](http://stackoverflow.com/questions/6727933/plmortgage-calculator-gui-issue-with-a-lot)正在做同樣的功課。 –

回答

5

您要添加的單選按鈕到電網,但你還需要將它們添加到您定義的按鈕組中。

也許這:

ButtonGroup group = new ButtonGroup();   
grid.add(seven); 
grid.add(fifteen); 
grid.add(thirty); 

應該是這樣:??? (複製/粘貼錯誤?)

ButtonGroup group = new ButtonGroup();   
group.add(seven); 
group.add(fifteen); 
group.add(thirty); 

或者您可能需要同時執行這兩個操作。單選按鈕必須屬於一個容器以及一個要顯示的按鈕組並且行爲正確。

+0

當我這樣做時,他們消失 – rhonda

+0

Opps我的錯誤我知道了。 – rhonda