gpt4 book ai didi

java - 使用 JRadioButtons 设置两个文本字段

转载 作者:行者123 更新时间:2023-12-01 05:49:52 26 4
gpt4 key购买 nike

这里对 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();

}
}

最佳答案

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

如果您需要更多帮助,请发布您的SSCCE这显示了您在阅读本教程后所尝试的内容。

关于java - 使用 JRadioButtons 设置两个文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5000408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com