好吧,我仍然是一個Java新手。說實話,這是一項家庭作業,但我現在正在撞牆。我不知道該從哪裏出發。java swing ui不顯示重建數組索引越界
我正在爲作業寫一個庫存程序,它必須使用普通數組。不是ArrayList
我被難住的部分是你必須添加一個新的條目到陣列的部分。我通過使用Arrays.copyof
來創建它的第二個版本,然後用增強的長度將它重新複製到原始版本。它似乎工作,通過控制檯,我可以打印我的新陣列沒有問題,完成新的條目。我遇到的問題是我的圖形用戶界面,..每次我嘗試以顯示我的GUI中的新條目,我得到
"Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5"
我敢肯定它的一些愚蠢的錯誤我製作,但是我度過了漫長的一天,我完全沉迷於此。你的問題的
package inventoryprogram3;
public class Inventoryprogram3 {
public static void main(String[] args) {
inventoryGui gui = new inventoryGui();
gui.setVisible(true);
}
}
package inventoryprogram3;
import java.io.Serializable;
class sItem implements Serializable{
int itemNumber;
String itemName;
int itemStock;
double itemPrice;
double stockTotal;
public sItem(int iNumber, String iName, int iStock, double iPrice){
itemNumber = iNumber;
itemName = iName;
itemStock = iStock;
itemPrice = iPrice;
}
}
public class rBook extends sItem {
int pCount;
double rFee;
public rBook(int iNumber, String iName, int iStock, double iPrice, int pageCount) {
super(iNumber, iName, iStock, iPrice);
pCount = pageCount;
rFee = (iPrice * iStock) * 1.05;
}
}
package inventoryprogram3;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
public class inventoryGui extends javax.swing.JFrame {
/**
* Creates new form inventoryGui
*/
static int max = 5;
static rBook[] rpg = new rBook[max];
double invValue;
int counter = -1;
public inventoryGui() {
initComponents();
rpg[0] = new rBook(1, "Mutants & Masterminds", 4, 19.99, 200);
rpg[1] = new rBook(2, "Exalted", 6, 25.99, 542);
rpg[2] = new rBook(3, "D&D Next", 15, 49.95, 350);
rpg[3] = new rBook(4, "Eclipse Phase", 3, 28.99, 426);
rpg[4] = new rBook(5, "The Mutant Epoch", 8, 19.99, 386);
}
private void initComponents() {
jLabel4 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
nameLabel = new javax.swing.JLabel();
titleLabel = new javax.swing.JLabel();
iNumberLabel = new javax.swing.JLabel();
priceLabel = new javax.swing.JLabel();
pCountlabel = new javax.swing.JLabel();
stockLabel = new javax.swing.JLabel();
restockLabel = new javax.swing.JLabel();
entireInventoryLabel = new javax.swing.JLabel();
restockField = new javax.swing.JTextField();
stockField = new javax.swing.JTextField();
pCountField = new javax.swing.JTextField();
priceField = new javax.swing.JTextField();
iNumberField = new javax.swing.JTextField();
titleField = new javax.swing.JTextField();
inventoryValueLabel = new javax.swing.JLabel();
inventoryValueField = new javax.swing.JTextField();
nextButton = new javax.swing.JButton();
entireInventoryField = new javax.swing.JTextField();
buttonLabel = new javax.swing.JLabel();
previousButton = new javax.swing.JButton();
companyLogo = new javax.swing.JLabel();
saveButton = new javax.swing.JButton();
loadButton = new javax.swing.JButton();
addbutton = new javax.swing.JButton();
deleteButton = new javax.swing.JButton();
modifyButton = new javax.swing.JButton();
searchButton = new javax.swing.JButton();
jLabel4.setText("jLabel4");
jLabel2.setText("jLabel2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("RPG Inventory Program v0.4");
nameLabel.setText("Role Playing Game Inventory");
titleLabel.setText("Title");
iNumberLabel.setText("Item Number");
priceLabel.setText("Price");
pCountlabel.setText("Page Count");
stockLabel.setText("Copies in Stock");
restockLabel.setText("Restocking Fee");
entireInventoryLabel.setText("Value of Entire Inventory");
restockField.setEditable(false);
iNumberField.setEditable(false);
inventoryValueLabel.setText("Inventory Value");
nextButton.setText("Next");
nextButton.setToolTipText("Click toDisplay Next Item");
nextButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextButtonActionPerformed(evt);
}
});
entireInventoryField.setEditable(false);
buttonLabel.setText("Item Display");
previousButton.setText("Previous");
previousButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
previousButtonActionPerformed(evt);
}
});
companyLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inventoryprogram3/logo4.png")));
saveButton.setText("Save");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
loadButton.setText("Clear");
loadButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loadButtonActionPerformed(evt);
}
});
addbutton.setText("Add");
addbutton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addbuttonActionPerformed(evt);
}
});
deleteButton.setText("Delete");
deleteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteButtonActionPerformed(evt);
}
});
modifyButton.setText("Modify Current Item");
modifyButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
modifyButtonActionPerformed(evt);
}
});
searchButton.setText("Search");
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)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(pCountlabel)
.addGap(18, 18, 18)
.addComponent(pCountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(entireInventoryLabel))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(restockLabel)
.addGap(192, 192, 192)
.addComponent(previousButton))
.addGroup(layout.createSequentialGroup()
.addComponent(loadButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(addbutton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(deleteButton)
.addGap(18, 18, 18)
.addComponent(modifyButton)
.addGap(12, 12, 12)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(nextButton)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(saveButton))))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(nameLabel)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(priceLabel)
.addGap(18, 18, 18)
.addComponent(priceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(titleLabel)
.addGap(18, 18, 18)
.addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(iNumberLabel)
.addGap(18, 18, 18)
.addComponent(iNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(searchButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(companyLogo)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(restockField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(inventoryValueLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(inventoryValueField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(stockLabel)
.addGap(18, 18, 18)
.addComponent(stockField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(entireInventoryField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(buttonLabel)
.addGap(15, 15, 15)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {iNumberLabel, pCountlabel, priceLabel, restockLabel, stockLabel, titleLabel});
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {iNumberField, inventoryValueField, pCountField, priceField, restockField, stockField, titleField});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(nameLabel)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(titleLabel)
.addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(iNumberLabel)
.addComponent(iNumberField, 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.BASELINE)
.addComponent(priceLabel)
.addComponent(priceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(companyLogo, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(pCountlabel)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(pCountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(entireInventoryLabel)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(stockLabel)
.addComponent(stockField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(entireInventoryField, 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.TRAILING)
.addComponent(inventoryValueLabel)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(inventoryValueField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonLabel)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(restockLabel)
.addComponent(restockField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(previousButton)
.addComponent(nextButton))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(saveButton)
.addComponent(addbutton)
.addComponent(deleteButton)
.addComponent(modifyButton)
.addComponent(loadButton))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(counter< max -1){
counter = counter + 1;
}
else{
counter = 0;
}
NumberFormat realMoney = NumberFormat.getCurrencyInstance(Locale.US);
invValue = 0;
for (int x=0; x < max -1; x++) {
invValue += rpg[x].itemStock * rpg[x].itemPrice;
}
System.out.println(counter);
titleField.setText(rpg[counter].itemName);
iNumberField.setText(String.valueOf(rpg[counter].itemNumber));
pCountField.setText(String.valueOf(rpg[counter].pCount));
priceField.setText(String.valueOf(realMoney.format(rpg[counter].itemPrice)));
stockField.setText(String.valueOf(rpg[counter].itemStock));
restockField.setText(String.valueOf(rpg[counter].rFee));
inventoryValueField.setText(String.valueOf(rpg[counter].itemStock * rpg[counter].itemPrice));
entireInventoryField.setText(String.valueOf(realMoney.format(invValue)));
}
private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(counter>= 1){
counter = counter - 1;
}
else{
counter = max -1;
}
NumberFormat realMoney = NumberFormat.getCurrencyInstance(Locale.US);
invValue = 0;
for (int x=0; x < max -1; x++) {
invValue += rpg[x].itemStock * rpg[x].itemPrice;
}
titleField.setText(rpg[counter].itemName);
iNumberField.setText(String.valueOf(rpg[counter].itemNumber));
pCountField.setText(String.valueOf(rpg[counter].pCount));
priceField.setText(String.valueOf(realMoney.format(rpg[counter].itemPrice)));
stockField.setText(String.valueOf(rpg[counter].itemStock));
restockField.setText(String.valueOf(rpg[counter].rFee));
inventoryValueField.setText(String.valueOf(rpg[counter].itemStock * rpg[counter].itemPrice));
entireInventoryField.setText(String.valueOf(realMoney.format(invValue)));
}
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
ObjectOutputStream savefile = null;
int dirmake = 1;
do{
try {
savefile = new ObjectOutputStream(new FileOutputStream("C:\\inventory\\inventory.dat"));
savefile.writeObject(rpg);
savefile.close();
dirmake = 2 ;
}
catch (Exception e){
String savePath = "c:\\Inventory";
Path invPath = Paths.get(savePath);
try {
Files.createDirectory(invPath);
} catch (IOException ex) {
Logger.getLogger(inventoryGui.class.getName()).log(Level.SEVERE, null, ex);
}
}
}while(dirmake == 1);
}
private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {
titleField.setText("");
iNumberField.setText("");
pCountField.setText("");
priceField.setText("");
stockField.setText("");
restockField.setText("");
inventoryValueField.setText("");
entireInventoryField.setText("");
}
private void modifyButtonActionPerformed(java.awt.event.ActionEvent evt) {
int mINumber = Integer.parseInt(iNumberField.getText());
double ss = Double.parseDouble(priceField.getText().replaceAll("[^\\d.]", ""));
rpg[mINumber -1] = new rBook(mINumber, titleField.getText(), Integer.parseInt(stockField.getText()), ss, Integer.parseInt(pCountField.getText()));
}
private void addbuttonActionPerformed(java.awt.event.ActionEvent evt) {
double ss = Double.parseDouble(priceField.getText().replaceAll("[^\\d.]", ""));
max ++;
rBook[] rpg2 = Arrays.copyOf(rpg, rpg.length);
rBook[] rpg = Arrays.copyOf(rpg2, max);
rpg[max -1] = new rBook(max, titleField.getText(), Integer.parseInt(stockField.getText()), ss, Integer.parseInt(pCountField.getText()));
System.out.println(max + titleField.getText() + Integer.parseInt(stockField.getText()) + ss + Integer.parseInt(pCountField.getText()));
counter = -1;
System.out.println(max);
for (int x=0; x< max; x++) {
System.out.println("Item Number:" + rpg[5].itemNumber);
System.out.println("Rpg Name:" + rpg[5].itemName);
System.out.println("Copies in stock:" + rpg[5].itemStock);
System.out.println("Price: $" + (rpg[5].itemPrice));
System.out.println("Page Count:" + rpg[5].pCount);
System.out.println("Restocking fee: $" + (rpg[5].rFee));
System.out.println();
System.out.println();
System.out.println(rpg.length);
}
}
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
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(inventoryGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(inventoryGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(inventoryGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(inventoryGui.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 inventoryGui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton addbutton;
private javax.swing.JLabel buttonLabel;
private javax.swing.JLabel companyLogo;
private javax.swing.JButton deleteButton;
private javax.swing.JTextField entireInventoryField;
private javax.swing.JLabel entireInventoryLabel;
private javax.swing.JTextField iNumberField;
private javax.swing.JLabel iNumberLabel;
private javax.swing.JTextField inventoryValueField;
private javax.swing.JLabel inventoryValueLabel;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JButton loadButton;
private javax.swing.JButton modifyButton;
private javax.swing.JLabel nameLabel;
private javax.swing.JButton nextButton;
private javax.swing.JTextField pCountField;
private javax.swing.JLabel pCountlabel;
private javax.swing.JButton previousButton;
private javax.swing.JTextField priceField;
private javax.swing.JLabel priceLabel;
private javax.swing.JTextField restockField;
private javax.swing.JLabel restockLabel;
private javax.swing.JButton saveButton;
private javax.swing.JButton searchButton;
private javax.swing.JTextField stockField;
private javax.swing.JLabel stockLabel;
private javax.swing.JTextField titleField;
private javax.swing.JLabel titleLabel;
}
歡迎來到堆棧溢出!不要依賴'max',依靠'rpg.length'。另外,'static'不是你的朋友。一個實際的[可運行示例演示您的問題](https://stackoverflow.com/help/mcve)將涉及更少的猜測工作和更好的響應 – MadProgrammer