gpt4 book ai didi

Java 组合框错误

转载 作者:行者123 更新时间:2023-12-01 18:59:37 25 4
gpt4 key购买 nike

我添加了一个组合框。它需要具有 1 到 20 的值,并且需要从构造函数调用方法。现在我不擅长放置代码。我对需要在哪里插入代码感到困惑,并且总是会出现错误。

运行程序时出现错误。它对组合框说:没有合适的方法我已将该框命名为yearscombobox

请帮我修复错误。我不知道是否需要放置具有 1 到 20 值的数组?如果是这样,我不知道该代码该放在哪里。我在代码中用粗体编写的行中有错误。好吧,我尝试将其加粗。当我从 net beans 复制这段代码时,它几乎没有什么不同。任何有yearscomboox的东西都会出错,说没有合适的方法。帮我。我任由你摆布。我讨厌Java

  package murach.ui;

import java.text.NumberFormat;
import murach.business.FinancialCalculations;

public class FutureValueFrame extends javax.swing.JFrame {

/** Creates new form FutureValueFrame */
public FutureValueFrame() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
monthlyPaymentTextField = new javax.swing.JTextField();
yearlyRateTextField = new javax.swing.JTextField();
calculateButton = new javax.swing.JButton();
exitButton = new javax.swing.JButton();
**YearsComboBox = new javax.swing.JComboBox();**
jScrollPane1 = new javax.swing.JScrollPane();
FutureValueList = new javax.swing.JList();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Future Value Calculator");
setResizable(false);

jLabel1.setText("Monthly Payment:");

jLabel2.setText("Yearly Interest Rate:");

jLabel3.setText("Number of Years:");

jLabel4.setText("Future Value:");

calculateButton.setMnemonic('c');
calculateButton.setText("Calculate");
calculateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calculateButtonActionPerformed(evt);
}
});

exitButton.setMnemonic('x');
exitButton.setText("Exit");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});

YearsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" }));
YearsComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
YearsComboBoxActionPerformed(evt);
}
});

FutureValueList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Year 1", "Year 2", "Year 3", "Year 4", "Year 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(FutureValueList);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(yearlyRateTextField)
.addComponent(monthlyPaymentTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
.addComponent(YearsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(calculateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
.addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1))))
.addGap(36, 36, 36))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(monthlyPaymentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(yearlyRateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(YearsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calculateButton)
.addComponent(exitButton))
.addContainerGap(100, Short.MAX_VALUE))
);

pack();
}// </editor-fold>


private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (isValidData())
{
double p = Double.parseDouble(monthlyPaymentTextField.getText());
double r = Double.parseDouble(yearlyRateTextField.getText());
int y = Integer.parseInt(YearsComboBox.getItemCount());

double fv = FinancialCalculations.calculateFutureValue(p, r, y);

NumberFormat currency = NumberFormat.getCurrencyInstance();
FutureValueList.setText(currency.format(fv));
}
}

private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private void YearsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int y = YearsComboBox.getSelectedIndex();

}

private boolean isValidData()
{
SwingValidator sv = new SwingValidator();
return
sv.isPresent(monthlyPaymentTextField, "Monthly Investment") &&
sv.isDouble(monthlyPaymentTextField, "Monthly Investment") &&
sv.isPresent(yearlyRateTextField, "Interest Rate") &&
sv.isDouble(yearlyRateTextField, "Interest Rate") &&
sv.isPresent(YearsComboBox, "Number of Years") &&
sv.isInteger(YearsComboBox, "Number of Years");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// new FutureValueFrame().setVisible(true); // generated code
FutureValueFrame frame = new FutureValueFrame();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
});
}

// Variables declaration - do not modify
private javax.swing.JList FutureValueList;
public javax.swing.JComboBox YearsComboBox;
private javax.swing.JButton calculateButton;
private javax.swing.JButton exitButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField monthlyPaymentTextField;
private javax.swing.JTextField yearlyRateTextField;
// End of variables declaration

}

最佳答案

  1. 为了获得更好的帮助,请尽快发布 SSCCE ,没人知道剩下的类(class)...,

  2. IntegerDouble 值放入 JComboBoxModel 没有问题或JList

  3. 使用 J FormattedTextField使用数字格式化程序

  4. 那么您可以减少从/解析 StringIntegerDouble 时可能出现的异常

    <
  5. 您可以使用JSpinner with SpinnerDateModel而不是 JComboBox

关于Java 组合框错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12697967/

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