请参阅最新的帖子this one,以获取进一步的评论。
所以我正在尝试这个程序,我试图让用户通过JPanel中的文本框输入他们的输入。
顺便说一下,当我从System.in用Scanner获取输入时,一切正常。关于导致问题的textField
变更的一些事情。我在另一个程序上有“主”,所以这也不是问题。
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JPanel implements ActionListener {
LinkedList<FoodItem> foodItemList = new LinkedList<FoodItem>();
CategoryList<String> categoryList = new CategoryList<String>();
Scanner in = new Scanner(System.in);
protected JButton[] buttons = new JButton[6];
String[] buttonNames = {"Add Item", "Remove Item", "Search for Item", "Update Item", "Check if List is Empty", "Print Items"};
String[] buttonCommands = {"add", "remove", "search", "update", "isEmpty", "print"};
protected JTextField textField;
public GUI() {
textField = new JTextField(20);
textField.addActionListener(this);
add(textField);
for (int i = 0; i < 6; i++) {
buttons[i] = new JButton(buttonNames[i]);
buttons[i].setActionCommand(buttonCommands[i]);
buttons[i].setVerticalTextPosition(AbstractButton.CENTER);
buttons[i].setHorizontalTextPosition(AbstractButton.LEADING);
buttons[i].addActionListener(this);
add(buttons[i]);
}
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals(buttonCommands[0]))
add();
else if (command.equals(buttonCommands[1]))
remove();
else if (command.equals(buttonCommands[2]))
search();
else if (command.equals(buttonCommands[3]))
update();
else if (command.equals(buttonCommands[4]))
isEmpty();
else
print();
}
public void add() {
System.out.println("Please enter the name of the product you want to add.");
String text = textField.getText();
try {
if (foodItemList.containsName(text))
throw new DuplicateProductException();
else
foodItemList.add(enterFoodItem(text));
}
catch(DuplicateProductException ex) {
System.out.println("The product has already been added to the list.");
System.out.println("Would you like to update or remove this item? (U for update, R for remove, N for neither)");
String response = textField.getText();
if (response.equals("U"))
update();
if (response.equals("R"))
remove();
}
}
public void remove(String s) {
if (foodItemList.size() == 1)
foodItemList.list = null;
else
foodItemList.oneBeforeContainingName(s).setLink(foodItemList.oneBeforeContainingName(s).getLink().getLink());
System.out.println("The product has been removed.");
}
public void remove() {
System.out.println("Please enter the name of the product you want to remove.");
String text = textField.getText();
try {
if (foodItemList.containsName(text))
remove(text);
else
throw new NonexistentProductException();
}
catch(NonexistentProductException ex) {
System.out.println("The product does not exist in the list.");
System.out.println("Would you like to add this product? (Y/N)");
String response = in.nextLine();
if (response.equals("Y"))
add();
}
}
public void search() {
System.out.println("Please enter the name of the product you want to search for.");
String text = textField.getText();
if (foodItemList.containsName(text)) {
System.out.println("The product is found.");
System.out.println("Would you like to print it or update it? (P for print, U for update, N for neither)");
String response = in.nextLine();
if (response.equals("P"))
print(text);
if (response.equals("U"))
update(text);
}
else
System.out.println("The product does not exist in the list.");
}
public void update(String s){
foodItemList.nodeContainingName(s).setElement(enterFoodItem(s));
}
public void update() {
System.out.println("Please enter the name of the product you want to update.");
String text = textField.getText();
try {
if (foodItemList.containsName(text))
update(text);
else
throw new NonexistentProductException();
}
catch(NonexistentProductException ex) {
System.out.println("The product does not exist in the list.");
System.out.println("Would you like to add this product? (Y/N)");
String response = in.nextLine();
if (response.equals("Y"))
add();
}
}
public void isEmpty() {
if (foodItemList.isEmpty())
System.out.println("The list is empty.");
else
System.out.println("The list is not empty.");
}
public void print(String s){
LinkedListNode<FoodItem> temp = foodItemList.list;
while (!temp.getElement().name.equals(s))
temp = temp.getLink();
System.out.println(temp.getElement().toString());
}
public void print() {
if (foodItemList.isEmpty())
System.out.println("The list is empty.");
else
System.out.println("Here is the list:" + foodItemList.toString());
}
public FoodItem enterFoodItem(String input) {
String[] requiredEntry = {"price", "quantity", "description", "size", "special order", "category"};
double p = 0.0;
int q = 0;
String d = "";
double s = 0.0;
String so = "";
String c = "";
for (int i = 0; i < 6; i++) {
System.out.println("Please enter the " + requiredEntry[i] + " of the product.");
if (i == 0)
p = Double.parseDouble(textField.getText());
if (i == 1)
q = Integer.parseInt(textField.getText());
if (i == 2)
for (int j = 0; j < 2; j++)
d = textField.getText();
if (i == 3)
s = Double.parseDouble(textField.getText());
if (i == 4)
for (int j = 0; j < 2; j++)
so = textField.getText();;
if (i == 5)
c = textField.getText();
}
return new FoodItem(input,p,q,d,s,so,c);
}
public class DuplicateProductException extends Exception {
public DuplicateProductException() {;}
}
public class NonexistentProductException extends Exception {
public NonexistentProductException() {;}
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Vendor Interface");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI newContentPane = new GUI();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
}
对不起它可能太长了(这只是项目中很多项目中的一个......)但是为了解决问题的核心,当按钮出现时,我应该能够进入首先是产品的名称。但由于某种原因,它不仅会“跳过”名称,还会“跳过”价格。这是我得到的错误:
Please enter the name of the product you want to add.
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
Please enter the price of the product.
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at GUI.enterFoodItem(GUI.java:150)
at GUI.add(GUI.java:49)
at GUI.actionPerformed(GUI.java:30)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
再一次,我为这个庞大的帖子道歉,但我不知道为了找到这个程序中的错误,我需要提供足够多的信息。非常感谢您的帮助!