简体   繁体   English

更新 gui 二进制数组列表

[英]Updating gui binary arraylist

The goal of this program is to read a binary file into an arraylist then display it in the gui window.这个程序的目标是将一个二进制文件读入一个数组列表,然后在 gui 窗口中显示它。 There are 5 indexs and i use curIndex to update it.有 5 个索引,我使用 curIndex 来更新它。 My problem I believe lays in the refreshUI().我相信我的问题在于 refreshUI()。 I cant get it to update with the new the index.我无法让它使用新的索引进行更新。 I also am not sure how to set the fields in the refresh.我也不确定如何在刷新中设置字段。 If you could point me in the right direction that would be awesome!如果你能指出我正确的方向,那就太棒了! thank YOU谢谢你

 ArrayList<SalesAgent> salesForce = new ArrayList<>();
int curIndex = 0;

public RecordViewerPanel() throws ClassNotFoundException, IOException {
    initComponents();
    ObjectInputStream input = new ObjectInputStream(
            new BufferedInputStream(
                    new FileInputStream("sales.bin")));

    while (curIndex < 5) {

        String first = input.readUTF();
        String last = input.readUTF();
        double sales = input.readDouble();
        Date date = (Date) input.readObject();
        salesForce.add(new SalesAgent(first, last, sales, date));
        refreshUI();
    }


}

private void refreshUI() {
salesForce.get(curIndex);
lastTextField.setText(salesForce.getLast());
salesTextField.setText(String.valueOf(salesForce.getSales()));
dateTextField.setText(String.valueOf(salesForce.getDate()));

    jPanel2.repaint();
    curIndex++;

}

/**
 * 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")


    nextButton.setText(">>");
    nextButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nextButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addComponent(firstTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(26, 26, 26)
                    .addComponent(lastTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(salesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(26, 26, 26)
                    .addComponent(dateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(64, 64, 64))
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(39, 39, 39)
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(31, 31, 31)
                            .addComponent(jLabel4)
                            .addGap(70, 70, 70)
                            .addComponent(jLabel5)
                            .addGap(101, 101, 101)
                            .addComponent(jLabel6))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(254, 254, 254)
                            .addComponent(previousButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(nextButton)))))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGap(29, 29, 29)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(jLabel4)
                .addComponent(jLabel5)
                .addComponent(jLabel6))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(salesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(dateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(previousButton)
                .addComponent(nextButton)))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
}// </editor-fold>                        

private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    if (curIndex > 0) {
        curIndex--;
    } else if (curIndex == 0) {
        curIndex = 4;
    }
    salesForce.get(curIndex);
    refreshUI();
}                                              

private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (curIndex < 4) {
        curIndex++;
    } else if (curIndex == 4) {
        curIndex = 0;
    }
    salesForce.get(curIndex);
    refreshUI();
} 

Avoid using "magic" numbers.避免使用“神奇”数字。 Use your List's size to help you out.使用您列表的大小来帮助您。 For example, this is bad:例如,这是不好的:

private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (curIndex < 4) {
        curIndex++;
    } else if (curIndex == 4) {
        curIndex = 0;
    }
    salesForce.get(curIndex);
    refreshUI();
} 

Instead it should be:相反,它应该是:

private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) { 
  curIndex++;
  curIndex %= salesForce.size(); // mod it by size of your List
  currentSalesAgent = salesForce.get(curIndex);
  // salesForce.get(curIndex); // this throws out the result returned by get(...)
  refreshUI();
} 

Where currentSalesAgent is a SalesAgent field that your class uses to get the current guy's stats for display.其中 currentSalesAgent 是一个 SalesAgent 字段,您的类使用它来获取当前人员的统计数据以进行显示。 In your class you'd declare在你的课上你会声明

private SalesAgent currentSalesAgent;

Note that just calling salseForce.get(curIndex);请注意,只是调用salseForce.get(curIndex); does nothing of use since while the method does extract a SalesAgent item from the List, it is wasted since you don't assign the object returned to any variable.没有任何用处,因为虽然该方法确实从 List 中提取了 SalesAgent 项目,但由于您没有将返回的对象分配给任何变量,因此它被浪费了。 That is why I had:这就是为什么我有:

currentSalesAgent = salesForce.get(curIndex);

Then in your refreshUI method, you use the currentSalesAgent:然后在您的 refreshUI 方法中,您使用 currentSalesAgent:

// salesTextField.setText(String.valueOf(salesForce.getSales())); // this shouldn't compile!
salesTextField.setText(String.valueOf(currentSalesAgent.getSales()));

Again, this makes no sense to me whatsoever:同样,这对我来说毫无意义:

salesForce.getSales()

You're calling getSales() on an ArrayList, and ArrayList has no such method.您在 ArrayList 上调用getSales() ,而 ArrayList 没有这样的方法。 How can this not cause a compilation error to occur?这怎么能不导致编译错误发生?


Edit编辑

I'm not sure that you fully understand me, and so I will try to spell out the issue.我不确定你是否完全理解我的意思,所以我会试着把这个问题弄清楚。 You have declared the salesForce variable as an ArrayList variable here:您已在此处将salesForce变量声明为 ArrayList 变量:

ArrayList<SalesAgent> salesForce = new ArrayList<>();

and then here you appear to be making numerous calls on this same variable as if it were a SalesAgent object:然后在这里你似乎对这个变量进行了多次调用,就好像它是一个 SalesAgent 对象:

lastTextField.setText(salesForce.getLast());
salesTextField.setText(String.valueOf(salesForce.getSales()));
dateTextField.setText(String.valueOf(salesForce.getDate()));

Again, this cannot compile since ArrayList does not have any of these methods that you are calling, not unless you have a second local variable of the same name, salesForce that is in fact a SalesAgent object, and that shadows the class field, and one that you never show us that you've declared.同样,这不能编译,因为ArrayList中没有任何的这些方法,你是打电话,除非你有相同的名字,第二局部变量salesForce是实际上是一个SalesAgent对象,而人影类字段,以及一个你从来没有告诉我们你已经声明过。 If you're doing this, then don't .如果你这样做,那么不要 Your code is confusing enough as it is that you should not give two completely different variables that hold different types of objects the same name.您的代码令人困惑,因为您不应该给两个完全不同的变量,这些变量包含相同的名称,这些变量包含不同类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM