使用JRadioButtons设置两个文本字段

时间:2011-02-15 06:04:30

标签: java swing listeners jradiobutton

这里非常新的Java。如何使用JRadioButton设置两个不同的文本字段?三个按钮是:
1. 7在5.35%
2. 15为5.5%
3. 30在5.75%

选择1将field1设置为7,将field2设置为5.35
选择2将field1设置为15,将field2设置为5.5
选择3将field1设置为30,将field2设置为5.75

编写此代码的最快速,最简单的方法是什么?看起来很简单,但我对JRadioButtons有一段时间了。

感谢。

编辑:这是代码。看起来我几乎在那里,但是,我仍然无法通过单选按钮将数据放入字段中。说我需要将类型更改为int,但如果我这样做,那就不再是一个可进入的字段......

//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=new 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();

}
}

2 个答案:

答案 0 :(得分:0)

阅读How to Use Radio Buttons上的Swing教程中的部分。基本上,您为每个按钮添加一个ActionListener,然后在文本字段中设置值。

如果您需要更多帮助,请在阅读完教程后发布显示您尝试过的SSCCE

答案 1 :(得分:0)

// 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;   
    }    
}