2012-10-02 36 views
0

我添加了組合框。它需要具有值1到20,並需要從構造函數調用方法。 現在我在放置代碼時很糟糕。我感到困惑的地方,我需要插入代碼,並總是最終得到錯誤。Java組合框錯誤

我在運行我的程序時出錯。它說對於組合框:沒有合適的方法 我已經命名框年份組合框

請幫我修復錯誤。我不知道是否需要將數組的值設置爲1到20?如果是這樣,我不知道該把代碼放在哪裏。 我在代碼中寫有BOLD的錯誤。好吧,我試着加粗它。這個代碼是有點不同,當我從淨豆複製它。任何有年歲的人都會錯誤地說沒有合適的方法。幫我。我在你的憐憫之下。我最討厭的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

我跑你的代碼和YearsComboBox確實包含值從1到20.所以你有什麼問題? –

回答