plMortgage Calculator GUI 存在很多问题

发布于 2024-11-24 19:43:09 字数 6510 浏览 3 评论 0原文

我对这个应用程序有很多问题。我一整天都在做这件事,但无法弄清楚这一点。我有一个用于类的 Java 应用程序。我遇到的问题是尝试将 JRadioButtons 分配给数组中的变量,然后将它们传递到公式中。如果有人可以提供帮助,我将非常感激。

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JRadioButton;

public class MortgageCalculatorGUI8 extends JFrame {

    JPanel panel1 = new JPanel();

    double Principal;
    double [] Interest = {5.35, 5.5, 5.75};
    double temp_Interest;
    int [] Length = {7, 15, 30};
    int temp_Length;
    boolean ok = false;


    public MortgageCalculatorGUI8(){

        getContentPane ().setLayout (null);
        setSize (400,400);

        panel1.setLayout (null);
        panel1.setBounds (0, 0, 2000, 800);
        add (panel1);

        JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
        mortgageLabel.setBounds (15, 15, 150, 30);
        panel1.add (mortgageLabel);

        final JTextField mortgageText = new JTextField(10);
        mortgageText.setBounds (130, 15, 150, 30);
        panel1.add (mortgageText);

        JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
        termLabel.setBounds (340, 40, 150, 30);
        panel1.add (termLabel);

        JTextField termText = new JTextField(3);
        termText.setBounds (340, 70, 150, 30);
        panel1.add (termText);

        JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
        intRateLabel.setBounds (340, 94, 150, 30);
        panel1.add (intRateLabel);

        JTextField intRateText = new JTextField(5);
        intRateText.setBounds (340, 120, 150, 30);
        panel1.add (intRateText);

        JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
        mPaymentLabel.setBounds (550, 40, 150, 30);
        panel1.add (mPaymentLabel);

        JTextField mPaymentText = new JTextField(10);
        mPaymentText.setBounds (550, 70, 150, 30);
        panel1.add (mPaymentText);

//      JLabel paymentLabel = new JLabel ("Payment #");
//      JLabel balLabel = new JLabel (" Balance");
//      JLabel ytdPrincLabel = new JLabel (" Principal");
//      JLabel ytdIntLabel = new JLabel (" Interest");

        JTextArea textArea = new JTextArea(10, 31);
        textArea.setBounds (550, 100, 150, 30);
        panel1.add (textArea);
        JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroll.setBounds (450, 200, 300, 150);
        panel1.add (scroll);


    public void rbuttons(){

        JLabel tYears = new JLabel("Years of Loan Amount");
        tYears.setBounds (30, 35, 150, 30);
        panel1.add (tYears);
        JRadioButton b7Yr = new JRadioButton("7 Years",false);
        b7Yr.setBounds (30, 58, 150, 30);
        panel1.add (b7Yr);
        JRadioButton b15Yr = new JRadioButton("15 Years",false);
        b15Yr.setBounds (30, 80, 150, 30);
        panel1.add (b15Yr);
        JRadioButton b30Yr = new JRadioButton("30 Years",false);
        b30Yr.setBounds (30, 101, 150, 30);
        panel1.add (b30Yr);


        JLabel tInterest = new JLabel("Interest Rate Of the Loan");
        tInterest.setBounds (175, 35, 150, 30);
        panel1.add(tInterest);
        JRadioButton b535Int = new JRadioButton("5.35% Interest",false);
        b535Int.setBounds (178, 58, 150, 30);
        panel1.add (b535Int);
        JRadioButton b55Int = new JRadioButton("5.5% Interest",false);
        b55Int.setBounds (178, 80, 150, 30);
        panel1.add (b55Int);
        JRadioButton b575Int = new JRadioButton("5.75% Interest",false);
        b575Int.setBounds (178, 101, 150, 30);
        panel1.add (b575Int);
    }

        JButton calculateButton = new JButton("CALCULATE");
        calculateButton.setBounds (30, 400, 120, 30);
        panel1.add (calculateButton);

        calculateButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {


                    if(e.getSource () == b7Yr){
                        b7Yr = Length[0];
                    }
                    if(e.getSource () == b15Yr){
                        b15Yr = Length[1];
                    }
                    if(e.getSource () == b30Yr){
                        b30Yr = Length[2];
                    }
                    if(e.getSource () == b535Int){
                        b535Int = Interest[0];
                    }
                    if(e.getSource () == b55Int){
                        b55Int = Interest[1];
                    }
                    if(e.getSource () == b575Int){
                        b575Int = Interest[2];
                    }


                double Principal;
//              double [] Interest;
//              int [] Length;
                double M_Interest = Interest /(12*100);
                double Months = Length  * 12;

                Principal = Double.parseDouble (mortgageText.getText());

                double M_Payment = Principal * ( M_Interest / (1 - (Math.pow((1 + M_Interest), - Months))));

                NumberFormat Money = NumberFormat.getCurrencyInstance();



            }
            });




        JButton clearButton = new JButton("CLEAR");
        clearButton.setBounds (160, 400, 120, 30);
        panel1.add (clearButton);

        clearButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                mortgageText.setText (null);
                b7Yr.setSelected (false);
                b15Yr.setSelected (false);
                b30Yr.setSelected (false);
                b535Int.setSelected (false);
                b55Int.setSelected (false);
                b575Int.setSelected (false);
            }
            });


        JButton exitButton = new JButton("EXIT");
        exitButton.setBounds (290, 400, 120, 30);
        panel1.add (exitButton);

        exitButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
            });


    }

    public static void main(String[] args) {

        MortgageCalculatorGUI8 frame = new MortgageCalculatorGUI8();
        frame.setBounds (400, 200, 800, 800);
        frame.setTitle ("Mortgage Calculator 1.0.4");
        frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
        frame.setVisible (true);



    }
}

I am having a lot of issues with this application. I have been at this all day and cannot get this figured out. I have a Java application that is for a class. The issue that I am having is trying to get the JRadioButtons assigned to variables in the array then passing them into the formula. If someone could help I would appreciate it a lot.

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JRadioButton;

public class MortgageCalculatorGUI8 extends JFrame {

    JPanel panel1 = new JPanel();

    double Principal;
    double [] Interest = {5.35, 5.5, 5.75};
    double temp_Interest;
    int [] Length = {7, 15, 30};
    int temp_Length;
    boolean ok = false;


    public MortgageCalculatorGUI8(){

        getContentPane ().setLayout (null);
        setSize (400,400);

        panel1.setLayout (null);
        panel1.setBounds (0, 0, 2000, 800);
        add (panel1);

        JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
        mortgageLabel.setBounds (15, 15, 150, 30);
        panel1.add (mortgageLabel);

        final JTextField mortgageText = new JTextField(10);
        mortgageText.setBounds (130, 15, 150, 30);
        panel1.add (mortgageText);

        JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
        termLabel.setBounds (340, 40, 150, 30);
        panel1.add (termLabel);

        JTextField termText = new JTextField(3);
        termText.setBounds (340, 70, 150, 30);
        panel1.add (termText);

        JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
        intRateLabel.setBounds (340, 94, 150, 30);
        panel1.add (intRateLabel);

        JTextField intRateText = new JTextField(5);
        intRateText.setBounds (340, 120, 150, 30);
        panel1.add (intRateText);

        JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
        mPaymentLabel.setBounds (550, 40, 150, 30);
        panel1.add (mPaymentLabel);

        JTextField mPaymentText = new JTextField(10);
        mPaymentText.setBounds (550, 70, 150, 30);
        panel1.add (mPaymentText);

//      JLabel paymentLabel = new JLabel ("Payment #");
//      JLabel balLabel = new JLabel (" Balance");
//      JLabel ytdPrincLabel = new JLabel (" Principal");
//      JLabel ytdIntLabel = new JLabel (" Interest");

        JTextArea textArea = new JTextArea(10, 31);
        textArea.setBounds (550, 100, 150, 30);
        panel1.add (textArea);
        JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroll.setBounds (450, 200, 300, 150);
        panel1.add (scroll);


    public void rbuttons(){

        JLabel tYears = new JLabel("Years of Loan Amount");
        tYears.setBounds (30, 35, 150, 30);
        panel1.add (tYears);
        JRadioButton b7Yr = new JRadioButton("7 Years",false);
        b7Yr.setBounds (30, 58, 150, 30);
        panel1.add (b7Yr);
        JRadioButton b15Yr = new JRadioButton("15 Years",false);
        b15Yr.setBounds (30, 80, 150, 30);
        panel1.add (b15Yr);
        JRadioButton b30Yr = new JRadioButton("30 Years",false);
        b30Yr.setBounds (30, 101, 150, 30);
        panel1.add (b30Yr);


        JLabel tInterest = new JLabel("Interest Rate Of the Loan");
        tInterest.setBounds (175, 35, 150, 30);
        panel1.add(tInterest);
        JRadioButton b535Int = new JRadioButton("5.35% Interest",false);
        b535Int.setBounds (178, 58, 150, 30);
        panel1.add (b535Int);
        JRadioButton b55Int = new JRadioButton("5.5% Interest",false);
        b55Int.setBounds (178, 80, 150, 30);
        panel1.add (b55Int);
        JRadioButton b575Int = new JRadioButton("5.75% Interest",false);
        b575Int.setBounds (178, 101, 150, 30);
        panel1.add (b575Int);
    }

        JButton calculateButton = new JButton("CALCULATE");
        calculateButton.setBounds (30, 400, 120, 30);
        panel1.add (calculateButton);

        calculateButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {


                    if(e.getSource () == b7Yr){
                        b7Yr = Length[0];
                    }
                    if(e.getSource () == b15Yr){
                        b15Yr = Length[1];
                    }
                    if(e.getSource () == b30Yr){
                        b30Yr = Length[2];
                    }
                    if(e.getSource () == b535Int){
                        b535Int = Interest[0];
                    }
                    if(e.getSource () == b55Int){
                        b55Int = Interest[1];
                    }
                    if(e.getSource () == b575Int){
                        b575Int = Interest[2];
                    }


                double Principal;
//              double [] Interest;
//              int [] Length;
                double M_Interest = Interest /(12*100);
                double Months = Length  * 12;

                Principal = Double.parseDouble (mortgageText.getText());

                double M_Payment = Principal * ( M_Interest / (1 - (Math.pow((1 + M_Interest), - Months))));

                NumberFormat Money = NumberFormat.getCurrencyInstance();



            }
            });




        JButton clearButton = new JButton("CLEAR");
        clearButton.setBounds (160, 400, 120, 30);
        panel1.add (clearButton);

        clearButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                mortgageText.setText (null);
                b7Yr.setSelected (false);
                b15Yr.setSelected (false);
                b30Yr.setSelected (false);
                b535Int.setSelected (false);
                b55Int.setSelected (false);
                b575Int.setSelected (false);
            }
            });


        JButton exitButton = new JButton("EXIT");
        exitButton.setBounds (290, 400, 120, 30);
        panel1.add (exitButton);

        exitButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
            });


    }

    public static void main(String[] args) {

        MortgageCalculatorGUI8 frame = new MortgageCalculatorGUI8();
        frame.setBounds (400, 200, 800, 800);
        frame.setTitle ("Mortgage Calculator 1.0.4");
        frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
        frame.setVisible (true);



    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-12-01 19:43:09

好吧,我将通过一个例子向您展示我在上一条评论中的意思:

我自己,我会为我使用的每个组创建一个 JRadioButton 数组作为类字段,并为每个 JRadioButton 集群创建一个 ButtonGroup 对象。然后,在我的计算 JButton 的 ActionListener 中,我将通过循环单选按钮数组或从 ButtonGroups getSelection 方法获取选定的单选按钮(请注意,这将返回 ButtonModel 对象,如果未选择任何内容,则返回 null)。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class InfoFromRadioBtns extends JPanel {
   private static final long serialVersionUID = 1L;
   private int[] foobars = {1, 2, 5, 10, 20};
   private JRadioButton[] foobarRButtons = new JRadioButton[foobars.length];
   private ButtonGroup foobarBtnGroup = new ButtonGroup();

   public InfoFromRadioBtns() {
      // jpanel to hold one set of radio buttons
      JPanel radioBtnPanel = new JPanel(new GridLayout(0, 1));
      radioBtnPanel.setBorder(BorderFactory
               .createTitledBorder("Choose a Foobar"));

      // iterate through the radio button array creating buttons
      // and adding them to the radioBtnPanel and the
      // foobarBtnGroup ButtonGroup
      for (int i = 0; i < foobarRButtons.length; i++) {
         // string for radiobutton to dislay -- just the number
         String buttonText = String.valueOf(foobars[i]);
         JRadioButton radiobtn = new JRadioButton("foobar " + buttonText);
         radiobtn.setActionCommand(buttonText); // one way to find out which
                                                // button is selected
         radioBtnPanel.add(radiobtn); // add radiobutton to its panel
         foobarBtnGroup.add(radiobtn); // add radiobutton to its button group

         // add to array
         foobarRButtons[i] = radiobtn;
      }

      // one way to get the selected JRadioButton
      JButton getRadioChoice1 = new JButton("Get Radio Choice 1");
      getRadioChoice1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            ButtonModel seletedModel = foobarBtnGroup.getSelection();
            if (seletedModel != null) {
               String actionCommand = seletedModel.getActionCommand();
               System.out.println("selected foobar: " + actionCommand);
            } else {
               System.out.println("No foobar selected");
            }
         }
      });

      // another way to get the selected JRadioButton
      JButton getRadioChoice2 = new JButton("Get Radio Choice 2");
      getRadioChoice2.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            String actionCommand = "";
            for (JRadioButton foobarRButton : foobarRButtons) {
               if (foobarRButton.isSelected()) {
                  actionCommand = foobarRButton.getActionCommand();
               }
            }
            if (actionCommand.isEmpty()) {
               System.out.println("No foobar selected");
            } else {
               System.out.println("selected foobar: " + actionCommand);
            }

         }
      });

      JPanel jBtnPanel = new JPanel();
      jBtnPanel.add(getRadioChoice1);
      jBtnPanel.add(getRadioChoice2);

      // make main GUI use a BordeLayout
      setLayout(new BorderLayout());
      add(radioBtnPanel, BorderLayout.CENTER);
      add(jBtnPanel, BorderLayout.PAGE_END);
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("InfoFromRadioBtns");
      frame.getContentPane().add(new InfoFromRadioBtns());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

无论您做什么,都不要尝试将任何代码复制并粘贴到您的程序中,因为它根本不会以这种方式工作(故意)。发布它只是为了说明我上面讨论的概念。

Shoot, I'll show you in an example of what I meant in my last comment:

Myself, I'd create an array of JRadioButtons as a class field for each group that I used as well as a ButtonGroup object for each cluster of JRadioButtons. Then in my calculate JButton's ActionListener, I'd get the selected radiobutton by either looping through the radio button array or from the ButtonGroups getSelection method (note though that this returns a ButtonModel object or null if nothing is selected).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class InfoFromRadioBtns extends JPanel {
   private static final long serialVersionUID = 1L;
   private int[] foobars = {1, 2, 5, 10, 20};
   private JRadioButton[] foobarRButtons = new JRadioButton[foobars.length];
   private ButtonGroup foobarBtnGroup = new ButtonGroup();

   public InfoFromRadioBtns() {
      // jpanel to hold one set of radio buttons
      JPanel radioBtnPanel = new JPanel(new GridLayout(0, 1));
      radioBtnPanel.setBorder(BorderFactory
               .createTitledBorder("Choose a Foobar"));

      // iterate through the radio button array creating buttons
      // and adding them to the radioBtnPanel and the
      // foobarBtnGroup ButtonGroup
      for (int i = 0; i < foobarRButtons.length; i++) {
         // string for radiobutton to dislay -- just the number
         String buttonText = String.valueOf(foobars[i]);
         JRadioButton radiobtn = new JRadioButton("foobar " + buttonText);
         radiobtn.setActionCommand(buttonText); // one way to find out which
                                                // button is selected
         radioBtnPanel.add(radiobtn); // add radiobutton to its panel
         foobarBtnGroup.add(radiobtn); // add radiobutton to its button group

         // add to array
         foobarRButtons[i] = radiobtn;
      }

      // one way to get the selected JRadioButton
      JButton getRadioChoice1 = new JButton("Get Radio Choice 1");
      getRadioChoice1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            ButtonModel seletedModel = foobarBtnGroup.getSelection();
            if (seletedModel != null) {
               String actionCommand = seletedModel.getActionCommand();
               System.out.println("selected foobar: " + actionCommand);
            } else {
               System.out.println("No foobar selected");
            }
         }
      });

      // another way to get the selected JRadioButton
      JButton getRadioChoice2 = new JButton("Get Radio Choice 2");
      getRadioChoice2.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            String actionCommand = "";
            for (JRadioButton foobarRButton : foobarRButtons) {
               if (foobarRButton.isSelected()) {
                  actionCommand = foobarRButton.getActionCommand();
               }
            }
            if (actionCommand.isEmpty()) {
               System.out.println("No foobar selected");
            } else {
               System.out.println("selected foobar: " + actionCommand);
            }

         }
      });

      JPanel jBtnPanel = new JPanel();
      jBtnPanel.add(getRadioChoice1);
      jBtnPanel.add(getRadioChoice2);

      // make main GUI use a BordeLayout
      setLayout(new BorderLayout());
      add(radioBtnPanel, BorderLayout.CENTER);
      add(jBtnPanel, BorderLayout.PAGE_END);
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("InfoFromRadioBtns");
      frame.getContentPane().add(new InfoFromRadioBtns());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

Whatever you do, don't try to copy and paste any of this code into your program, because it simply isn't going to work that way (on purpose). It was posted only to illustrate the concepts that I've discussed above.

暖心男生 2024-12-01 19:43:09

我将扩展 JRadioButton 以创建一个能够保存所需变量的类。您可以将其作为内部类来执行,以使事情变得简单。

private double pctinterest;
private int numyears; // within scope of your containing class

private class RadioButtonWithYears extends JRadioButton {
    final private int years;
    private int getYears() { return years; }
    public RadioButtonWithYears(int years) {
        super(years + " years",false);
        this.years = years;
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numyears = getYears();
            }
        });
    }
}

// elsewhere
RadioButtonWithYears b7Yr = new RadioButtonWithYears(7);
RadioButtonWithYears b15Yr = new RadioButtonWithYears(15);
RadioButtonWithYears b30Yr = new RadioButtonWithYears(30);

// then later
double M_Interest = java.lang.Math.pow((pctinternet / 100)+1, numyears);

更新:它还没有到挽救的地步。我已经按照 Eels 的建议合并了 ButtonGroup,并使 GUI 部分工作(尽管您必须修复布局)并标记了需要整理计算的位置。

package stack.swing;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.text.NumberFormat;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JRadioButton;

public class MortgageCalculatorGUI8 extends JFrame {

    JPanel panel1 = new JPanel();

    Integer Principal;
    boolean ok = false;

    private BigDecimal temp_Interest;
    private int temp_Length; // within scope of your containing class

    private class JRadioButtonWithYears extends JRadioButton {
        final private int years;
        private int getYears() { return years; }
        public JRadioButtonWithYears(int years) {
            super(years + " years",false);
            this.years = years;
            addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    temp_Length = getYears();
                }
            });
        }
    }

    private class JRadioButtonWithPct extends JRadioButton {
        final private BigDecimal pct;
        private BigDecimal getPct() { return pct; }
        public JRadioButtonWithPct(String pct) {
            super(pct + "%",false);
            this.pct = new BigDecimal(pct);
            addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    temp_Interest = getPct();
                }
            });
        }
    }

    public MortgageCalculatorGUI8() {

        getContentPane ().setLayout (null);
        setSize (400,400);

        panel1.setLayout (null);
        panel1.setBounds (0, 0, 2000, 800);
        add (panel1);

        JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
        mortgageLabel.setBounds (15, 15, 150, 30);
        panel1.add (mortgageLabel);

        final JTextField mortgageText = new JTextField(10);
        mortgageText.setBounds (130, 15, 150, 30);
        panel1.add (mortgageText);

        JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
        termLabel.setBounds (340, 40, 150, 30);
        panel1.add (termLabel);

        JTextField termText = new JTextField(3);
        termText.setBounds (340, 70, 150, 30);
        panel1.add (termText);

        JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
        intRateLabel.setBounds (340, 94, 150, 30);
        panel1.add (intRateLabel);

        JTextField intRateText = new JTextField(5);
        intRateText.setBounds (340, 120, 150, 30);
        panel1.add (intRateText);

        JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
        mPaymentLabel.setBounds (550, 40, 150, 30);
        panel1.add (mPaymentLabel);

        JTextField mPaymentText = new JTextField(10);
        mPaymentText.setBounds (550, 70, 150, 30);
        panel1.add (mPaymentText);

//      JLabel paymentLabel = new JLabel ("Payment #");
//      JLabel balLabel = new JLabel (" Balance");
//      JLabel ytdPrincLabel = new JLabel (" Principal");
//      JLabel ytdIntLabel = new JLabel (" Interest");

        JTextArea textArea = new JTextArea(10, 31);
        textArea.setBounds (550, 100, 150, 30);
        panel1.add (textArea);
        JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroll.setBounds (450, 200, 300, 150);
        panel1.add (scroll);

        // jpanel to hold one set of radio buttons
        JPanel yearsPanel = new JPanel(new GridLayout(0, 1));
        yearsPanel.setBorder(BorderFactory
                 .createTitledBorder("Years of Loan Amount"));
        yearsPanel.setBounds(30, 55, 150, 150);
        panel1.add (yearsPanel);
        final ButtonGroup yearsGroup = new ButtonGroup();

        int years[] = { 7, 15, 30 };
        for (int i = 0; i < years.length; i++) {
           JRadioButtonWithYears radiobtn = new JRadioButtonWithYears(years[i]);
           yearsPanel.add(radiobtn); // add radiobutton to its panel
           yearsGroup.add(radiobtn); // add radiobutton to its button group
        }

        // jpanel to hold one set of radio buttons
        JPanel pctPanel = new JPanel(new GridLayout(0, 1));
        pctPanel.setBorder(BorderFactory
                 .createTitledBorder("Interest Rate Of the Loan"));
        pctPanel.setBounds(175, 55, 180, 150);
        panel1.add (pctPanel);
        final ButtonGroup pctGroup = new ButtonGroup();

        String pct[] = { "5.35", "5.5", "5.75" };
        for (int i = 0; i < pct.length; i++) {
           JRadioButtonWithPct radiobtn = new JRadioButtonWithPct(pct[i]);
           pctPanel.add(radiobtn); // add radiobutton to its panel
           pctGroup.add(radiobtn); // add radiobutton to its button group
        }

        final JButton calculateButton = new JButton("CALCULATE");
        calculateButton.setBounds (30, 400, 120, 30);
        panel1.add (calculateButton);

        calculateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double M_Interest = temp_Interest.doubleValue() /(12*100);
                double Months = temp_Length  * 12;

                Principal = Integer.parseInt(mortgageText.getText());

                double M_Payment = Principal * ( M_Interest / (1 - (Math.pow((1 + M_Interest), - Months))));

                NumberFormat Money = NumberFormat.getCurrencyInstance();

                /** MORE STUFF TO HAPPEN HERE */
            }
            });

        JButton clearButton = new JButton("CLEAR");
        clearButton.setBounds (160, 400, 120, 30);
        panel1.add (clearButton);

        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mortgageText.setText (null);
                yearsGroup.clearSelection();
                pctGroup.clearSelection();
            }
            });

        JButton exitButton = new JButton("EXIT");
        exitButton.setBounds (290, 400, 120, 30);
        panel1.add (exitButton);

        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
            });
    }

    public static void main(String[] args) {
        MortgageCalculatorGUI8 frame = new MortgageCalculatorGUI8();
        frame.setBounds (400, 200, 800, 800);
        frame.setTitle ("Mortgage Calculator 1.0.4");
        frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
        frame.setVisible (true);
    }
}

I would extend JRadioButton to create a class capable of holding the variables you want. You can do this as an inner class to keep things simple.

private double pctinterest;
private int numyears; // within scope of your containing class

private class RadioButtonWithYears extends JRadioButton {
    final private int years;
    private int getYears() { return years; }
    public RadioButtonWithYears(int years) {
        super(years + " years",false);
        this.years = years;
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numyears = getYears();
            }
        });
    }
}

// elsewhere
RadioButtonWithYears b7Yr = new RadioButtonWithYears(7);
RadioButtonWithYears b15Yr = new RadioButtonWithYears(15);
RadioButtonWithYears b30Yr = new RadioButtonWithYears(30);

// then later
double M_Interest = java.lang.Math.pow((pctinternet / 100)+1, numyears);

Update: It isn't too far gone to salvage. I have incorporated the ButtonGroup as per Eels suggestion, and made the GUI part of it work (although you'll have to fix the layout) and marked where you need to sort out the calculation.

package stack.swing;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.text.NumberFormat;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JRadioButton;

public class MortgageCalculatorGUI8 extends JFrame {

    JPanel panel1 = new JPanel();

    Integer Principal;
    boolean ok = false;

    private BigDecimal temp_Interest;
    private int temp_Length; // within scope of your containing class

    private class JRadioButtonWithYears extends JRadioButton {
        final private int years;
        private int getYears() { return years; }
        public JRadioButtonWithYears(int years) {
            super(years + " years",false);
            this.years = years;
            addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    temp_Length = getYears();
                }
            });
        }
    }

    private class JRadioButtonWithPct extends JRadioButton {
        final private BigDecimal pct;
        private BigDecimal getPct() { return pct; }
        public JRadioButtonWithPct(String pct) {
            super(pct + "%",false);
            this.pct = new BigDecimal(pct);
            addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    temp_Interest = getPct();
                }
            });
        }
    }

    public MortgageCalculatorGUI8() {

        getContentPane ().setLayout (null);
        setSize (400,400);

        panel1.setLayout (null);
        panel1.setBounds (0, 0, 2000, 800);
        add (panel1);

        JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
        mortgageLabel.setBounds (15, 15, 150, 30);
        panel1.add (mortgageLabel);

        final JTextField mortgageText = new JTextField(10);
        mortgageText.setBounds (130, 15, 150, 30);
        panel1.add (mortgageText);

        JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
        termLabel.setBounds (340, 40, 150, 30);
        panel1.add (termLabel);

        JTextField termText = new JTextField(3);
        termText.setBounds (340, 70, 150, 30);
        panel1.add (termText);

        JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
        intRateLabel.setBounds (340, 94, 150, 30);
        panel1.add (intRateLabel);

        JTextField intRateText = new JTextField(5);
        intRateText.setBounds (340, 120, 150, 30);
        panel1.add (intRateText);

        JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
        mPaymentLabel.setBounds (550, 40, 150, 30);
        panel1.add (mPaymentLabel);

        JTextField mPaymentText = new JTextField(10);
        mPaymentText.setBounds (550, 70, 150, 30);
        panel1.add (mPaymentText);

//      JLabel paymentLabel = new JLabel ("Payment #");
//      JLabel balLabel = new JLabel (" Balance");
//      JLabel ytdPrincLabel = new JLabel (" Principal");
//      JLabel ytdIntLabel = new JLabel (" Interest");

        JTextArea textArea = new JTextArea(10, 31);
        textArea.setBounds (550, 100, 150, 30);
        panel1.add (textArea);
        JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroll.setBounds (450, 200, 300, 150);
        panel1.add (scroll);

        // jpanel to hold one set of radio buttons
        JPanel yearsPanel = new JPanel(new GridLayout(0, 1));
        yearsPanel.setBorder(BorderFactory
                 .createTitledBorder("Years of Loan Amount"));
        yearsPanel.setBounds(30, 55, 150, 150);
        panel1.add (yearsPanel);
        final ButtonGroup yearsGroup = new ButtonGroup();

        int years[] = { 7, 15, 30 };
        for (int i = 0; i < years.length; i++) {
           JRadioButtonWithYears radiobtn = new JRadioButtonWithYears(years[i]);
           yearsPanel.add(radiobtn); // add radiobutton to its panel
           yearsGroup.add(radiobtn); // add radiobutton to its button group
        }

        // jpanel to hold one set of radio buttons
        JPanel pctPanel = new JPanel(new GridLayout(0, 1));
        pctPanel.setBorder(BorderFactory
                 .createTitledBorder("Interest Rate Of the Loan"));
        pctPanel.setBounds(175, 55, 180, 150);
        panel1.add (pctPanel);
        final ButtonGroup pctGroup = new ButtonGroup();

        String pct[] = { "5.35", "5.5", "5.75" };
        for (int i = 0; i < pct.length; i++) {
           JRadioButtonWithPct radiobtn = new JRadioButtonWithPct(pct[i]);
           pctPanel.add(radiobtn); // add radiobutton to its panel
           pctGroup.add(radiobtn); // add radiobutton to its button group
        }

        final JButton calculateButton = new JButton("CALCULATE");
        calculateButton.setBounds (30, 400, 120, 30);
        panel1.add (calculateButton);

        calculateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double M_Interest = temp_Interest.doubleValue() /(12*100);
                double Months = temp_Length  * 12;

                Principal = Integer.parseInt(mortgageText.getText());

                double M_Payment = Principal * ( M_Interest / (1 - (Math.pow((1 + M_Interest), - Months))));

                NumberFormat Money = NumberFormat.getCurrencyInstance();

                /** MORE STUFF TO HAPPEN HERE */
            }
            });

        JButton clearButton = new JButton("CLEAR");
        clearButton.setBounds (160, 400, 120, 30);
        panel1.add (clearButton);

        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mortgageText.setText (null);
                yearsGroup.clearSelection();
                pctGroup.clearSelection();
            }
            });

        JButton exitButton = new JButton("EXIT");
        exitButton.setBounds (290, 400, 120, 30);
        panel1.add (exitButton);

        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
            });
    }

    public static void main(String[] args) {
        MortgageCalculatorGUI8 frame = new MortgageCalculatorGUI8();
        frame.setBounds (400, 200, 800, 800);
        frame.setTitle ("Mortgage Calculator 1.0.4");
        frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
        frame.setVisible (true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文