Use JRadioButtons to set two text fields
Very new to Java here. How do I use a JRadioButton to set two different text fields? The three buttons are:
1. 7 at 5.35% 2. 15 at 5.5% 3. 30 at 5.75%Choice 1 sets field1 to 7 and field2 to 5.35
Choice 2 sets field1 to 15 and field2 to 5.5 Choice 3 sets field1 to 30 and field2 to 5.75What's the fastest and easiest way to write this code? Seems easy enough, but I'm having a hell of a time with the JRadioButtons.
Thanks.
EDIT: Here's the code. Looks like I'm almost there, however, I still can't get the radio buttons to put the data in the fields. Says I need to change the type to int, but if I do that it's not an enterable field anymore...
//import all needed functionality
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
public class ScrofaniWk3Alt extends JApplet{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
// Declare variables and put code application logic here
String userInput = null;
JLabel loanAmountLabel = new JLabel("Loan Amount: ");
JTextField loanAmount = new JTextField();
double[] ratesList = {0.0535, 0.055, 0.0575};
JLabel rateLabel=new JLabel("Interest Rate: ");
JTextField rate=new JTextField();
String[] yearsList = {"7","15","30"};
JLabel yearsLabel=new JLabel("Years of Payment: ");
JTextField years=new JTextField();
JRadioButton sevenButton = new JRadioButton("7");
JRadioButton fifteenButton = new JRadioButton("15");
JRadioButton thirtyButton = new JRadioButton("30");
JLabel payLabel=new JLabel("Monthly Payment: ");
JLabel payment=new JLabel();
JButton calculate=new JButton("Calculate");
JButton clear=new JButton("Clear");
JButton quit=new JButton("Quit");
JTextArea payments=new JTextArea();
JScrollPane schedulePane=new JScrollPane(payments);
Container mortCalc = getContentPane();
public void init() {
//Configure the radio buttons to input data into fields
sevenButton.setActionCommand("Radio1");
fifteenButton.setActionCommand("Radio2");
thirtyButton.setActionCommand("Radio3");
ButtonGroup chooseYears = new ButtonGroup();
chooseYears.add(sevenButton);
chooseYears.add(fifteenButton);
chooseYears.add(thirtyButton);
}
public void actionPerformed(ActionEvent e) {
if ("Radio1".equals(e.getActionCommand())) {
years = 7;
rate = 5.35;
}
if ("Radio2".equals(e.getActionCommand())) {
years = 15;
rate = 5.5;
}
if ("Radio3".equals(e.getActionCommand())) {
years = 30;
rate = 5.75;
}
calculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Perform the calculation
double yearsCalc=Integer.parseInt(years.getText())*12;
double rateCalc=Double.parseDouble(rate.getText())/12;
double principalCalc=Double.parseDouble(loanAmount.getText());
double monthlyPayment=principalCalc*Math.pow(1+rateCalc,yearsCalc)*rateCalc/(Math.pow(1+rateCalc,yearsCalc)-1);
DecimalFormat df = new DecimalFormat("$###,###.##");
payment.setText(df.format(monthlyPayment));
// Perform extra calculations to show the loan amount after each subsequent payoff
double principal=principalCalc;
int month;
StringBuffer buffer=new StringBuffer();
buffer.append("Month\tAmount\tInterest\tBalance\n");
for (int f=0; f<yearsCalc; f++) {
month=f+1;
double interest=principal*rateCalc;
double balance=principal+interest-monthlyPayment;
buffer.append(month+"\t"); buffer.append(new String(df.format(principal))+"\t");
buffer.append(new String(df.format(interest))+"\t"); buffer.append(new String(df.format(balance))+"\n");
principal=balance;
}
payments.setText(buffer.toString());
} catch(Exception ex) {
System.out.println(ex);
}
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loanAmount.setText(""); payment.setText(""); payments.setText("");
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
//Config GUI
JPanel panelMort=new JPanel();
panelMort.setLayout(new GridLayout(5,2));
panelMort.add(loanAmountLabel);
panelMort.add(loanAmount);
panelMort.add(new Label());
panelMort.add(sevenButton);
panelMort.add(fifteenButton);
panelMort.add(thirtyButton);
panelMort.add(yearsLabel);
panelMort.add(years);
panelMort.add(rateLabel);
panelMort.add(rate);
panelMort.add(payLabel);
panelMort.add(payment);
JPanel buttons=new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
buttons.add(calculate); buttons.add(clear); buttons.add(quit);
JPanel panelMort2=n开发者_运维百科ew JPanel();
panelMort2.setLayout(new BoxLayout(panelMort2, BoxLayout.Y_AXIS));
panelMort2.add(panelMort); panelMort2.add(buttons);
mortCalc.add(BorderLayout.NORTH, panelMort2);
mortCalc.add(BorderLayout.CENTER, schedulePane);
}
public static void main(String[] args) {
JApplet applet = new ScrofaniWk3Alt();
JFrame frameMort = new JFrame("ScrofaniWk3Alt");
frameMort.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameMort.getContentPane().add(applet);
frameMort.setSize(500,1000);
frameMort.setResizable(true);
frameMort.setVisible(true);
applet.init();
applet.start();
}
}
Read the section from the Swing tutorial on How to Use Radio Buttons. Basically you add an ActionListener to each button and then set the value in the text fields.
If you need more help then post your SSCCE that shows what you have attempted after reading the tutorial.
// put these where you define your buttons
radio1.setActionCommand("RADIO1");
radio2.setActionCommand("RADIO2");
radio3.setActionCommand("RADIO3");
// add this to your actionPerformed method
public void actionPerformed(ActionEvent e) {
if ("RADIO1".equals(e.getActionCommand())) {
field1 = 7;
field2 = 5.35;
}
if ("RADIO2".equals(e.getActionCommand())) {
field1 = 15;
field2 = 5.5;
}
if ("RADIO3".equals(e.getActionCommand())) {
field1 = 30;
field2 = 5.75;
}
}
精彩评论