我添加了一个组合框。它需要具有 1 到 20 的值,并且需要从构造函数调用方法。现在我不擅长放置代码。我很困惑我需要在哪里插入代码并且总是会出错。
运行程序时出现错误。它对组合框说:没有合适的方法我已将框命名为yearscombobox
请帮我修复错误。我不知道我是否需要为具有 1 到 20 的值放置数组?如果是这样,我不知道在哪里放置该代码。我在代码中用粗体编写的行中有错误。好的,我尝试将其加粗。当我从网豆复制这段代码时,它有点不同。任何有 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
}