2011-07-21 4 views
1

ラジオボタンに問題があります。私は同時に3つすべてをクリックすることができます。もう片方をオフにするといいでしょうか?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

あなたと[this guy](http://stackoverflow.com/questions/6727933/plmortgage-calculator-guississ-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