- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个程序,可以根据所选的单选按钮计算 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();
但是,当我尝试这样做时,我收到一条错误消息 Can't convert object to double
所以我添加了 floatValue()
并将其强制转换以消除错误。
当我按下计算时,它不显示来自 interestRateField
的输入在 monthlyPaymentField
.
如何使用 JFormattedTextField
获取我的值(从 DecimalFormat
使用 getValue
)然后以不同的方式显示它 JFormattedTextField
?
最佳答案
稍微调整了 Hovercraft Full Of Eels 的答案以使用 JFormattedTextField
的 getValue
和 setValue
方法并跳过手动解析/格式化
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
public class FutureValueFrame extends JFrame {
public static void main( String[] args ) {
DecimalFormat format = new DecimalFormat( "####.##" );
//in case you always want to see the 2 fraction digits
//format.setMinimumFractionDigits( 2 );
final JFormattedTextField field1 = new JFormattedTextField(
format );
final JFormattedTextField field2 = new JFormattedTextField(
format );
field1.setColumns( 15 );
field2.setColumns( 15 );
JButton btn = new JButton( new AbstractAction( "Multiply by 2" ) {
@Override
public void actionPerformed( ActionEvent e ) {
Number value = ( Number ) field1.getValue();
if ( value != null ){
field2.setValue( 2 * value.doubleValue() );
}
}
} );
JPanel panel = new JPanel();
panel.add( field1 );
panel.add( btn );
panel.add( field2 );
JOptionPane.showMessageDialog( null, panel );
}
}
这至少按设计使用了 JFormattedTextField
,但仍然具有 JFormattedTextField
的所有可用性“怪癖”。
the tips4java site 上有一篇博文关于improving the JFormattedTextField
它解决了大部分问题,可能值得多看一眼(尽管我仍然认为该版本可以改进,例如,通过在输入无效数据后立即将背景着色为红色,以便为用户提供视觉线索。)
关于java - 使用 JFormattedText 的 getValue 显示在另一个 JFormattedText 字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11192461/
在 FireBaseDp 中检索数据时,可以使用上述方法之一来获取值。我想知道有什么区别(任何优点/缺点)? 最佳答案 确切的行为取决于您提取的数据类型。 如果您要提取字符串值,则两个代码段的行为是相
我在 output1 和 output2 中得到了不同的结果。第一个给了我在注册表中实际看到的值,而后者只给了我默认值。我想念什么? String output1 = Registry.GetValu
我正在使用Kotlin多平台构建iOS框架,除奇怪的问题外,其他一切都运行良好。 来自Kotlin的代码: fun example(): Map { val result = muta
我最近一直在开发一些 JSF 应用程序,并且对 Web 组件 API 的不一致感到不安。 我注意到在服务器端代码中的 JSF 组件对象上调用 .getValue() 或 .getSubmittedVa
我正在开发 JavaFX 表单。该表单应该获取值并将其添加到数据库中。每当我尝试通过 .getValue 获取值时,它都会产生错误 Incompatible Types: Object cannot
我有下面的代码, var data = new Dictionary { { "A1", new TestData { Name = "N1",
返回的错误是: "TypeError: Cannot call method "getValues" of undefined. (line 7, file "Code")" function set
在 Google 表格中,将范围内的数据存储到数组中作为脚本的一部分,然后检查特定列中是否有“Y”,这样我就可以遍历并将这些列存储在新数组中。 我有以下代码,但出现此错误 - “TypeError:无
我正在尝试编写一个通用方法来比较两个对象(我有意引入两种不同的类型。第二个具有与第一个相同的属性。第一个具有更多属性。)。 我想确保属性具有相同的值。以下代码适用于我在对象中拥有的大多数属性,但偶尔会
我写了这门课。这不是原始类的完整描述,为了使我的问题陈述简洁,我给出了所需的内容 template class RB { class Child { members are Child ar
我正在尝试根据我的团队在工作中使用的项目跟踪表来设置电子邮件警报系统。当 K 列中的任务状态更改为“完成”时,我需要它发送电子邮件。我得到了可以在测试表上运行的代码,但是当我将其复制到实时表时, ge
尝试创建获取任何枚举属性值的枚举扩展方法时,我遇到了以下情况: 编译以下代码时,VS 2010 显示错误:“GetValue”方法没有重载需要 1 个参数。注释代码和使用监视工具时 - 该行返回值(见
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我想转换一个 Map>所以它变成了Map .如果它只是 Map在 Java8 中很容易; stream().collect(k -> k.getValue().getMyKey(), Entry::g
我目前正在使用 Scene Builder 制作 javafx 场景。我想以特定格式从日期选择器中获取值。只需使用 datePicker.getValue() 即可以 yyyy-mm-dd 形式返回日
问题是我们无法获取仅存在于具有泛型类型的基类中的字段(非泛型)的值。请看下面的代码片段。呼唤 f.GetValue(a) 将抛出异常消息:后期绑定(bind)操作不能在类型为 Type.Contain
为什么会这样: $RegistryKeys = (Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\services\VMSMP\Parameters\
我试图让我的函数在另一张表的表中查找值。 表格的行用数字标记,但列用字符串命名。 这是我的功能代码 Function GetValue(row As String, col As String)
我试图模拟顶级(不是任何部分的一部分)配置值(.NET Core 的 IConfiguration),但徒劳无功。例如,这些都不起作用(使用 NSubstitute,但它与 Moq 或我相信的任何模拟
我得到了一个代码,它从一个名为频率的列表中获取所有最小值。然后它将最小值与总值的百分比放入一个字符串中。要计算百分比,我想调用 minEntryes.getValue()(minEntryes 是 M
我是一名优秀的程序员,十分优秀!