我希望制作一个可以被多种方法使用的数组。我通过创建一个全局数组来尝试这个。但是发现,一旦将数据添加到数组的方法运行并终止,数据就会丢失,并且所有内容都会再次为空。
数组在此初始化:
package book;
import book.BookIO;
import java.awt.BorderLayout;
import java.awt.*;
import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
*
*
*/
public class UserInterface implements ActionListener {
int j=0;
int i;
//Containers
String hello;
JFrame frame = new JFrame("Java Program");
JPanel toppane = new JPanel(new GridBagLayout());
JPanel bottomPane = new JPanel(new GridBagLayout());
//Buttons
JButton processItem = new JButton("Process Item #1");
JButton confirmItem = new JButton("Confirm Item #1");
JButton viewOrder = new JButton("View Order");
JButton finishOrder = new JButton("Finish Order ");
JButton newOrder = new JButton("New Order");
JButton exit = new JButton("Exit");
//TextFields
JTextField amount = new JTextField();
JTextField id = new JTextField();
JTextField quantity = new JTextField();
JTextField info = new JTextField("");
JTextField total = new JTextField();
//Labels
JLabel num = new JLabel("Enter Number of Items in this Order:");
JLabel bookID = new JLabel("Enter Book ID for Item #1:");
JLabel quantityItem = new JLabel("Enter Quantity for Item #1:");
JLabel itemInfo = new JLabel("Item #1:");
JLabel subtotal = new JLabel("Order subtotal for 0 Items(s):");
public String[] order = new String[i];
int k = 0;
public void startUI() {
UserInterface gui = new UserInterface();
gui.bookingUI();
}
public void bookingUI() {
//sets windows, and pane in the UI
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints c = new GridBagConstraints();
frame.setSize(800, 300);
//adding the labels to the panel
c.insets = new Insets(5, 0, 0, 0);
c.gridx = 2;
c.gridy = 1;
toppane.add(num, c);
c.gridx = 2;
c.gridy = 2;
toppane.add(bookID, c);
c.gridx = 2;
c.gridy = 3;
toppane.add(quantityItem, c);
c.gridx = 2;
c.gridy = 4;
toppane.add(itemInfo, c);
c.gridx = 2;
c.gridy = 5;
toppane.add(subtotal, c);
toppane.setBackground(Color.GREEN);
frame.add(toppane);
//add textfield to panel
c.ipadx = 400;
c.insets = new Insets(5, 10, 0, 0);
c.gridx = 3;
c.gridy = 1;
toppane.add(amount, c);
c.gridx = 3;
c.gridy = 2;
toppane.add(id, c);
c.gridx = 3;
c.gridy = 3;
toppane.add(quantity, c);
c.gridx = 3;
c.gridy = 4;
toppane.add(info, c);
c.gridx = 3;
c.gridy = 5;
toppane.add(total, c);
//----------------------------------------------------------BUTTOM PANE-------------------------
//adding the buttons to the pane.---------------------------------------------------------------
GridBagConstraints b = new GridBagConstraints();
b.insets = new Insets(5, 5, 5, 5);
b.ipadx = 10;
b.ipady = 10;
b.gridx = 1;
b.gridy = 0;
bottomPane.add(processItem, b);
processItem.addActionListener(this);
b.gridx = 2;
b.gridy = 0;
bottomPane.add(confirmItem, b);
confirmItem.setEnabled(false);
confirmItem.addActionListener(this);
b.gridx = 3;
b.gridy = 0;
bottomPane.add(viewOrder, b);
viewOrder.setEnabled(true);
viewOrder.addActionListener(this);
b.gridx = 4;
b.gridy = 0;
bottomPane.add(finishOrder, b);
finishOrder.setEnabled(true);
finishOrder.addActionListener(this);
b.gridx = 5;
b.gridy = 0;
bottomPane.add(newOrder, b);
newOrder.addActionListener(this);
b.gridx = 6;
b.gridy = 0;
bottomPane.add(exit, b);
exit.addActionListener(this);
bottomPane.setBackground(Color.BLUE);
frame.add(bottomPane, BorderLayout.SOUTH);
frame.setSize(810, 310);
frame.setVisible(true);
}
//action listener for the buttons
public void actionPerformed(ActionEvent e) {
int i = Integer.valueOf(amount.getText());
order = new String[i];
if (e.getSource() == processItem) {
processItem();
} else if (e.getSource() == confirmItem) {
confirmItem();
setProcessItemBtn();
} else if (e.getSource() == viewOrder) {
System.out.println(order.length);
} else if (e.getSource() == finishOrder) {
} else if (e.getSource() == newOrder) {
newOrder();
} else if (e.getSource() == exit) {
System.exit(0);
}
}
//Creating getters and setters to change the text for the buttons and labels, as well as getting text from the textfields.
public void setProcessItemBtn() {
processItem.setText("Process Item #");
processItem.validate();
processItem.repaint();
}
public void setConfirmItemBtn(int num) {
confirmItem.setText("Confirm Item #" + num);
confirmItem.validate();
confirmItem.repaint();
}
public void setViewOrderBtn(String title) {
viewOrder.validate();
viewOrder.repaint();
}
public void setInfo(String title, String price) {
info.setText(title + " " + price);
info.validate();
info.repaint();
}
public String getAmount() {
String str = amount.getText();
return str;
}
public void confirmItem(){
i = Integer.valueOf(amount.getText());
int amountOrdered = Integer.valueOf(quantity.getText());
System.out.println(k);
for (k = 0; k < amountOrdered; k++){
order[j] = id.getText() + " " + info.getText();
j++;
}
System.out.println(order[2]);
System.out.println(k);
if ((i-1)<j){
processItem.setEnabled(false);
confirmItem.setEnabled(false);
}else{
processItem.setEnabled(true);
confirmItem.setEnabled(false);
}
}
public void processItem(){
boolean ifFound = false;
confirmItem.setEnabled(true);
processItem.setEnabled(false);
BookIO findInfo = new BookIO();
ifFound = findInfo.readFile(id.getText(), this);
if (ifFound == true)
JOptionPane.showMessageDialog(null, "Found it");
else {
JOptionPane.showMessageDialog(null, "Book ID " + id.getText() + " not found in file");
confirmItem.setEnabled(false);
processItem.setEnabled(true);
}
}
public void newOrder(){
amount.setText(" ");
id.setText(" ");
quantity.setText(" ");
info.setText(" ");
total.setText(" ");
}
}
方法(confirmItem)基本上从文本字段中获取用户信息并将其存储到数组中以便稍后打印到消息框上,该消息框将是另一种方法,即从单独的事件激活。在整个程序生命周期中多次调用此方法以从相同的文本字段获取其他信息(这是相同的相关信息)。但是在调用时,最后一次方法调用的数据不存在于数组中。它只是空的。
有没有办法将数据保存在数组中而不会通过单独的方法调用丢失它?此数组也由另一个承载相同问题的方法使用。
谢谢。
答案 0 :(得分:0)
每当您按下UI中的任何按钮时,actionPerformed
方法都会执行以下行:
order = new String[i];
正在做的是创建一个长度为i
的新的空白数组,并将order
变量设置为指向新的空白数组,以便旧数组将丢失。如果要展开数组,则必须将旧数组的内容复制到新数组中:
String[] newOrder = new String[newLength];
System.arraycopy(order, 0, newOrder, 0, order.length);
order = newOrder;
Arrays.copyOf
是在一行中执行此操作的标准,简单方法:
order = java.util.Arrays.copyOf(order, order.length + 1); // increase array length by 1
List
也可以使这种事情比裸阵列更容易,因为那时你可以做:orders.add(someNewOrder);
并且它会自动扩大尺寸。
在任何情况下,每次点击任何按钮时,您可能都无法无条件地执行此操作。我不确定你打算如何让程序表现出来。