java - 构造对象并将其放入 ArrayList 时遇到问题

标签 java

我已经为 ProductInfo 类型的对象创建了一个 ArrayList,但是 NetBeans 告诉我在第 256 行找不到 symbol: 方法ProductInfo(int, int, String, String).

下面是代码:

/**
 * Creates new form mVentoryHome
 */
public mVentoryHome() {
    initComponents();

 }

    public class ProductInfo{
    String name, des;
    int ID, num;

}

    /**
 *
 */
public static void Inventory(){

    }


//creat Array
    ArrayList <ProductInfo> Inventory = new ArrayList <ProductInfo> ();

/**
 * 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() {

    IDIn = new javax.swing.JTextField();
    NameIn = new javax.swing.JTextField();
    DesOut = new javax.swing.JLabel();
    IDOut = new javax.swing.JLabel();
    NameOut = new javax.swing.JLabel();
    AddGo = new javax.swing.JButton();
    NameGo = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    IDGo = new javax.swing.JButton();
    Title = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    NameSearch = new javax.swing.JTextField();
    IDSearch = new javax.swing.JTextField();
    DesIn = new javax.swing.JTextField();
    jLabel6 = new javax.swing.JLabel();
    NumIn = new javax.swing.JTextField();
    NumOut = new javax.swing.JLabel();
    Title1 = new javax.swing.JLabel();
    Title2 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(0, 0, 0));
    setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    setEnabled(false);

    AddGo.setText("Add");
    AddGo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            AddGoActionPerformed(evt);
        }
    });

    NameGo.setText("Search Name");
    NameGo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            NameGoActionPerformed(evt);
        }
    });

    jLabel1.setBackground(new java.awt.Color(0, 0, 0));
    jLabel1.setForeground(new java.awt.Color(255, 255, 255));
    jLabel1.setText("Name");

    IDGo.setText("Search ID #");
    IDGo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            IDGoActionPerformed(evt);
        }
    });

    Title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mVentory Logo Small.png"))); // NOI18N

    jLabel5.setBackground(new java.awt.Color(0, 0, 0));
    jLabel5.setForeground(new java.awt.Color(255, 255, 255));
    jLabel5.setText("ID #");

    jLabel4.setBackground(new java.awt.Color(0, 0, 0));
    jLabel4.setForeground(new java.awt.Color(255, 255, 255));
    jLabel4.setText("Name");

    jLabel3.setBackground(new java.awt.Color(0, 0, 0));
    jLabel3.setForeground(new java.awt.Color(255, 255, 255));
    jLabel3.setText("Description");

    jLabel2.setBackground(new java.awt.Color(0, 0, 0));
    jLabel2.setForeground(new java.awt.Color(255, 255, 255));
    jLabel2.setText("ID #");

    jLabel6.setBackground(new java.awt.Color(0, 0, 0));
    jLabel6.setForeground(new java.awt.Color(255, 255, 255));
    jLabel6.setText("Number");

    Title1.setBackground(new java.awt.Color(0, 0, 0));
    Title1.setForeground(new java.awt.Color(255, 255, 255));
    Title1.setText("Add Items");

    Title2.setBackground(new java.awt.Color(0, 0, 0));
    Title2.setForeground(new java.awt.Color(255, 255, 255));
    Title2.setText("Search Items");

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(101, 101, 101)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(157, 157, 157)
                    .add(IDOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(NameOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(NumOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(DesOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 260, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .add(0, 0, Short.MAX_VALUE))
        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .addContainerGap(68, Short.MAX_VALUE)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(jLabel3)
                                .add(jLabel2)
                                .add(jLabel1)
                                .add(jLabel6))
                            .add(18, 18, 18)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createSequentialGroup()
                                    .add(6, 6, 6)
                                    .add(AddGo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 163, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(NumIn)
                                    .add(DesIn)
                                    .add(IDIn)
                                    .add(NameIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 235, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(jLabel4)
                                .add(jLabel5))
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, NameSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 235, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                        .add(IDGo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .add(NameGo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 163, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(IDSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 235, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                    .add(84, 84, 84))
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(Title2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 163, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(133, 133, 133))
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(Title)
                        .add(Title1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 163, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(125, 125, 125))))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(Title)
            .add(79, 79, 79)
            .add(Title1)
            .add(27, 27, 27)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                .add(layout.createSequentialGroup()
                    .add(NameIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(16, 16, 16)
                    .add(IDIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(18, 18, 18)
                    .add(DesIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(18, 18, 18)
                    .add(NumIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(layout.createSequentialGroup()
                    .add(jLabel1)
                    .add(16, 16, 16)
                    .add(jLabel2)
                    .add(18, 18, 18)
                    .add(jLabel3)
                    .add(18, 18, 18)
                    .add(jLabel6)))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(AddGo)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(Title2)
            .add(40, 40, 40)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(jLabel4)
                    .add(46, 46, 46)
                    .add(jLabel5))
                .add(layout.createSequentialGroup()
                    .add(NameSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(NameGo)
                    .add(11, 11, 11)
                    .add(IDSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(18, 18, 18)
                    .add(IDGo)))
            .add(93, 93, 93)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(NameOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(IDOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(NumOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(18, 18, 18)
            .add(DesOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(66, 66, 66))
    );

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

private void AddGoActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // Add Item
    String Name, Description;
    int Identification, Number;

    Name = NameIn.getText();
    Description = DesIn.getText();
    Identification = Integer.parseInt(IDIn.getText());
    Number = Integer.parseInt(NumIn.getText());

    Inventory.add(ProductInfo(Name, Description, Identification, Number));


}                                     

private void NameGoActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // Search by Name


}                                      

private void IDGoActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // Search by ID
}                                    



/**
 * @param args the command line arguments
 */
public static void main(String args[]) {






    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(mVentoryHome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(mVentoryHome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(mVentoryHome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(mVentoryHome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new mVentoryHome().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton AddGo;
private javax.swing.JTextField DesIn;
private javax.swing.JLabel DesOut;
private javax.swing.JButton IDGo;
private javax.swing.JTextField IDIn;
private javax.swing.JLabel IDOut;
private javax.swing.JTextField IDSearch;
private javax.swing.JButton NameGo;
private javax.swing.JTextField NameIn;
private javax.swing.JLabel NameOut;
private javax.swing.JTextField NameSearch;
private javax.swing.JTextField NumIn;
private javax.swing.JLabel NumOut;
private javax.swing.JLabel Title;
private javax.swing.JLabel Title1;
private javax.swing.JLabel Title2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;

最佳答案

  1. 定义第 256 行使用的构造函数:

    public class ProductInfo{
        String name; 
        String des; 
        int ID; 
        int num; 
    
        public ProductInfo(String name, String des, int ID, int num){
          this.name = name;
          this.des = des;
          this.ID = ID;
          this.num = num;
        }
    }
    
  2. 使用new运算符:

    Inventory.add(new ProductInfo(Name, Description, Identification, Number));
    

关于java - 构造对象并将其放入 ArrayList 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22460197/

相关文章:

java - 如何知道 RecyclerView 是否有足够的内容可以滚动?

java - 如何从android应用程序中的文本文件中读取大的json?

java - BufferedInputStream 的缓冲区大小

java - 我如何从 JSP 调用 C++ 函数

java - Android如何将纬度经度转换成度数格式

java - 如何使用套接字制作JavaFX Chat应用程序?

java - 以 extends 和 super 作为参数的无限通配符

java - 2 线程中出现异常 "main"java.lang.OutOfMemoryError : Java heap space

java - 尝试获取一个查询以将 LIKE 函数与 JSP 中的变量一起使用

java - 警告 : Documents NO-HEAD in Eclipse