java结果在text中显示_java – 使用getValue for JFormattedText在另一个JFormattedText字段中显示...

我有一个程序,我根据所选的单选按钮计算2个变量中的任何一个.我正在尝试使用getValue从JFormattedText字段获取值并将其显示在另一个JFormattedText字段上(最终我将使用该数字进行一些计算).

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.text.DecimalFormat;

public class FutureValueFrame extends JFrame

{

public FutureValueFrame()

{

setTitle("Sample App");

setSize(400,400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args)

{

JFrame f = new FutureValueFrame();

//GUI and BUTTONS

JRadioButton monthlyRadioButton = new JRadioButton("Monthly Payment");

JRadioButton loanAmountButton = new JRadioButton("Loan Amount");

ButtonGroup selection = new ButtonGroup();

selection.add(monthlyRadioButton);

selection.add(loanAmountButton);

JFormattedTextField loanAmountField = new JFormattedTextField(new DecimalFormat("####.##"));

JFormattedTextField interestRateField = new JFormattedTextField(new DecimalFormat("####.##"));

JFormattedTextField yearField = new JFormattedTextField(new DecimalFormat("####.##"));

JFormattedTextField monthlyPaymentField = new JFormattedTextField(new DecimalFormat("####.##"));

JPanel menuPanel = new JPanel();

menuPanel.setLayout(new GridLayout(1,2));

//ACTION LISTENER FOR RADIO BUTTONS

monthlyRadioButton.addActionListener(new SelectionListener(monthlyRadioButton, loanAmountButton, loanAmountField, monthlyPaymentField));

loanAmountButton.addActionListener(new SelectionListener(monthlyRadioButton, loanAmountButton, loanAmountField, monthlyPaymentField));

JPanel topPanel = new JPanel();

topPanel.setLayout(new GridLayout(1,2));

topPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

topPanel.add(monthlyRadioButton);

topPanel.add(loanAmountButton);

JPanel botPanel = new JPanel();

botPanel.setLayout(new GridLayout(4,2));

botPanel.add(new JLabel("Loan Amount:"));

botPanel.add(loanAmountField);

botPanel.add(new JLabel("Yearly Interest Rate:"));

botPanel.add(interestRateField);

botPanel.add(new JLabel("Number of Years:"));

botPanel.add(yearField);

botPanel.add(new JLabel("Monthly Payment:"));

botPanel.add(monthlyPaymentField);

JPanel container = new JPanel();

container.setLayout(new GridLayout(3,1));

container.add(topPanel);

container.add(botPanel);

container.add(menuPanel);

f.add(container);

JButton calculateButton = new JButton("Calculate");

calculateButton.addActionListener(new CalculateMonthlyListener(loanAmountField, interestRateField, yearField, monthlyPaymentField, selection, monthlyRadioButton, loanAmountButton));

JButton exitButton = new JButton("Exit");

exitButton.addActionListener(new ExitListener());

menuPanel.add(calculateButton);

menuPanel.add(exitButton);

f.setVisible(true);

f.setLocationRelativeTo(null);

}

class CalculateMonthlyListener implements ActionListener

{

private JFormattedTextField loanAmountField;

private JFormattedTextField monthlyPaymentField;

private JFormattedTextField interestRateField;

private JFormattedTextField yearField;

private double result;

private ButtonGroup selection;

private JRadioButton monthlyRadioButton;

private JRadioButton loanAmountButton;

public CalculateMonthlyListener (JFormattedTextField loanAmountField,

JFormattedTextField interestRateField,

JFormattedTextField yearField,

JFormattedTextField monthlyPaymentField,

ButtonGroup selection,

JRadioButton monthlyRadioButton,

JRadioButton loanAmountButton)

{

this.interestRateField = interestRateField;

this.yearField = yearField;

this.loanAmountField = loanAmountField;

this.selection = selection;

this.monthlyRadioButton = monthlyRadioButton;

this.loanAmountButton = loanAmountButton;

this.monthlyPaymentField = monthlyPaymentField;

}

public void actionPerformed(ActionEvent e)

{

if (selection.getSelection().equals(monthlyRadioButton.getModel()))

{

result = ((Double) interestRateField.getValue()).floatValue();

monthlyPaymentField.setValue(new Double(result));

System.out.println("You selected monthly");

}

else

{

loanAmountField.setValue(new Double(12.22));

System.out.println("You selected loan");

}

}

}

class ExitListener implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

//f.dispose();

System.exit(0);

//System.out.println("You clicked exit");

}

}

class SelectionListener implements ActionListener

{

private JRadioButton monthlyRadioButton;

private JRadioButton loanAmountButton;

private JFormattedTextField loanAmountField;

private JFormattedTextField monthlyPaymentField;

public SelectionListener (JRadioButton monthlyRadioButton,

JRadioButton loanAmountButton,

JFormattedTextField loanAmountField,

JFormattedTextField monthlyPaymentField)

{

this.monthlyRadioButton = monthlyRadioButton;

this.loanAmountButton = loanAmountButton;

this.loanAmountField = loanAmountField;

this.monthlyPaymentField = monthlyPaymentField;

}

public void actionPerformed(ActionEvent event)

{

if(event.getSource() == monthlyRadioButton)

{

loanAmountField.setEditable(false);

monthlyPaymentField.setEditable(true);

}

else

{

monthlyPaymentField.setEditable(false);

loanAmountField.setEditable(true);

}

}

}

}

我的问题出现在下面的代码片段中:

public void actionPerformed(ActionEvent e) {

if (selection.getSelection().equals(monthlyRadioButton.getModel())) {

result = ((Double) interestRateField.getValue()).floatValue();

monthlyPaymentField.setValue(new Double(result));

System.out.println("You selected monthly");

} else {

loanAmountField.setValue(new Double(12.22));

System.out.println("You selected loan");

}

}

在这里,我试图将getValue分配给结果.我看过Oracle文档,看起来获取值的代码就是interestRateField.getValue();当我尝试这个时,我得到一个错误,说不能将对象转换为double,所以我添加了floatValue()并将其转换为删除错误.

当我按下calculate时,它不会在monthlyPaymentField中显示来自interestRateField的输入.

如何使用getValue获取我的值(使用DecimalFormat从JFormattedTextField获取),然后将其显示在不同的JFormattedTextField中?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值