这个程序的目标是将二进制文件读入数组列表,然后在gui窗口中显示它。有5个索引,我使用curIndex来更新它。我认为我的问题在于refreshUI()。我不能让它更新新的索引。我也不确定如何在刷新中设置字段。如果你能给我指个方向就太好了!谢谢你
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();
}
避免使用"魔术"数字。利用清单的大小来帮助你。例如,这是坏的:
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (curIndex < 4) {
curIndex++;
} else if (curIndex == 4) {
curIndex = 0;
}
salesForce.get(curIndex);
refreshUI();
}
应该是:
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();
}
其中currentSalesAgent是一个SalesAgent字段,您的类使用它来获取要显示的当前对象的统计信息。在你的类中你要声明
private SalesAgent currentSalesAgent;
请注意,仅仅调用salseForce.get(curIndex);
没有任何用处,因为虽然该方法确实从列表中提取了SalesAgent项,但由于没有将返回的对象赋值给任何变量,因此浪费了它。这就是为什么我有:
currentSalesAgent = salesForce.get(curIndex);
然后在refreshUI方法中,使用currentSalesAgent:
// salesTextField.setText(String.valueOf(salesForce.getSales())); // this shouldn't compile!
salesTextField.setText(String.valueOf(currentSalesAgent.getSales()));
再一次,这对我来说没有任何意义:
salesForce.getSales()
你在数组列表上调用getSales()
,而数组列表没有这样的方法。这样做如何不会导致编译错误?
编辑
我不确定你是否完全理解我的意思,所以我将试着把这个问题解释清楚。您已经在这里将salesForce
变量声明为ArrayList变量:
ArrayList<SalesAgent> salesForce = new ArrayList<>();
,然后这里你似乎对这个变量进行了多次调用,就好像它是一个SalesAgent对象:
lastTextField.setText(salesForce.getLast());
salesTextField.setText(String.valueOf(salesForce.getSales()));
dateTextField.setText(String.valueOf(salesForce.getDate()));
再次,这不能编译,因为ArrayList没有你正在调用的任何这些方法,除非你有第二个同名的本地变量,salesForce
实际上是一个SalesAgent对象,并且遮蔽了类字段,并且你从未向我们显示你已经声明过。如果您正在这样做,那么不要。你的代码已经足够令人困惑了,因为你不应该给两个完全不同的变量,保存不同类型的对象相同的名称。