该程序的目标是将二进制文件读入arraylist然后在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();
}
答案 0 :(得分:0)
避免使用&#34;魔法&#34;数字。使用您的列表大小来帮助您。例如,这很糟糕:
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);
没有任何用处,因为当方法从List中提取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()
你在ArrayList上调用getSales()
,而ArrayList没有这样的方法。这怎么可能不会导致编译错误?
修改
我不确定你是否完全了解我,所以我会尽力说明问题。您已在此处将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没有您正在调用的任何这些方法,除非您有第二个同名的 local 变量,salesForce
事实上是一个SalesAgent对象,它隐藏了类字段,并且一个你从未向我们展示过你已声明过的字段。如果你这样做,那么不要。你的代码很容易混淆,因为你不应该给出两个完全不同的变量,这些变量包含不同类型的对象同名。