GUI输入和输出的问题

时间:2015-04-23 16:03:21

标签: java swing split jtextfield

我有一个项目可以接受不同数量的输入并显示不同数量的输出。我需要使用动态GUI实现。我已经实现了一些代码如下:

package orderMatcher2;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList; 
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class OrderMatcher2 {
    public static Scanner input;
    public static String order;
    public static List<String> sellOrder = new ArrayList<String>();
    public static List<String> buyOrder = new ArrayList<String>();
    public static List<Integer> sellVolume = new ArrayList<Integer>(0);
    public static List<Integer> sellPrice = new ArrayList<Integer>(0);
    public static List<Integer> buyVolume = new ArrayList<Integer>(0);
    public static List<Integer> buyPrice = new ArrayList<Integer>(0);
    public static int tempDiff;
    public static int sellTemp;
    public static int buyTemp;
    public static int priceSell;
    public static int priceBuy;
    public static JTextArea incoming;
    public static JTextField outgoing;

    public OrderMatcher2() {
        // TODO Auto-generated constructor stub

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame("Enter 'Exit or EXIT or exit' to end");
        JPanel mainPanel = new JPanel();
        incoming = new JTextArea(15, 50);
        incoming.setLineWrap(true);
        incoming.setWrapStyleWord(true);
        incoming.setEditable(false);

        JScrollPane qScroller = new JScrollPane(incoming);
        qScroller
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        outgoing = new JTextField(20);
        JButton sendButton = new JButton("Send");
        sendButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                do {
                    System.out.println("> " + outgoing.getText());
                    order = outgoing.getText().trim();
                    // order = input.nextLine();
                    order = order.toUpperCase();

                    System.out.println("read " + order);
                    incoming.append(order + "\n");

                    if (order.equals("EXIT"))
                        System.exit(0);

                    if (!(order.equals("PRINT"))) {
                        System.out.println(order);
                        incoming.append(order);
                        orderOperationValidation(order);
                    }
                    if (order.substring(0, 4).equals("SELL")) {
                        orderOperationSell(order);
                    }
                    if (order.substring(0, 3).equals("BUY")) {
                        orderOperationBuy(order);
                    }

                    if (order.contains("PRINT")) {
                        callPrint();
                    }

                    /*
                     * if (order.equals("PRINT")) callPrint();
                     */

                    outgoing.setText("");
                    outgoing.requestFocus();

                } while ((order.contains("SELL")) || (order.contains("BUY"))
                        || (order.contains("PRINT")));

            }
        });

        mainPanel.add(qScroller);
        mainPanel.add(outgoing);
        mainPanel.add(sendButton);

        frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
        frame.setSize(450, 350);
        frame.setVisible(true);

    }

    public static void orderOperationValidation(String valid) {
        // TODO Auto-generated method stub
        String[] tokens = valid.split("[\\s@]", 0);
        for (String string : tokens) {
            System.out.println(string);
        }

        if ((Integer.parseInt(tokens[1]) < 0)
                || (Integer.parseInt(tokens[2]) < 0)) {
            // System.out.println("Invalid Request");
            incoming.append("Invalid Request");
            new OrderMatcher2();
            OrderMatcher2.main(new String[0]);
        }
    }

    public static void orderOperationSell(String selling) {
        // TODO Auto-generated method stub
        sellOrder.add(selling);
        String[] tempSell = selling.split("[\\s@]", 0);
        sellVolume.addAll(new ArrayList<Integer>(Arrays.asList(Integer
                .parseInt(tempSell[1]))));
        sellPrice.addAll(new ArrayList<Integer>(Arrays.asList(Integer
                .parseInt(tempSell[2]))));
        for (int i = 0; i < buyPrice.size(); i++) {
            for (int j = 0; j < sellPrice.size(); j++) {
                if (buyPrice.get(i) >= sellPrice.get(j)) {
                    tempDiff = buyVolume.get(i) - sellVolume.get(j);
                    if (tempDiff > 0) {
                        sellTemp = sellVolume.get(j);
                        priceSell = sellPrice.get(j);
                        System.out.println("TRADE  " + sellTemp + "@"
                                + priceSell);
                        incoming.append("TRADE  " + sellTemp + "@" + priceSell);
                    } else {
                        buyTemp = buyVolume.get(i);
                        priceSell = sellPrice.get(j);
                        System.out.println("TRADE  " + buyTemp + "@"
                                + priceSell);
                        incoming.append("TRADE  " + buyTemp + "@" + priceSell);
                    }
                    if (tempDiff > 0)
                        buyVolume.set(i, tempDiff);

                }
            }
        }

    }

    public static void orderOperationBuy(String buying) {
        // TODO Auto-generated method stub
        buyOrder.add(buying);
        String[] tempBuy = buying.split("[\\s@]", 0);

        buyVolume.addAll(new ArrayList<Integer>(Arrays.asList(Integer
                .parseInt(tempBuy[1]))));
        buyPrice.addAll(new ArrayList<Integer>(Arrays.asList(Integer
                .parseInt(tempBuy[2]))));

        for (int i = 0; i < buyPrice.size(); i++) {
            for (int j = 0; j < sellPrice.size(); j++) {
                if (buyPrice.get(i) >= sellPrice.get(j)) {
                    tempDiff = buyVolume.get(i) - sellVolume.get(j);
                    if (tempDiff > 0) {
                        sellTemp = sellVolume.get(j);
                        priceSell = sellPrice.get(j);
                        System.out.println("TRADE  " + sellTemp + "@"
                                + priceSell);
                        incoming.append("TRADE  " + sellTemp + "@" + priceSell);
                    } else {
                        buyTemp = buyVolume.get(i);
                        priceSell = sellPrice.get(j);
                        System.out.println("TRADE  " + buyTemp + "@"
                                + priceSell);
                        incoming.append("TRADE  " + buyTemp + "@" + priceSell);
                    }
                    if (tempDiff > 0)
                        buyVolume.set(i, tempDiff);

                }
            }

        }
    }

    public static void callPrint() {
        // TODO Auto-generated method stub
        System.out.println("  PRINT ");
        for (int i = 0; i < buyVolume.size(); i++) {
            if (buyVolume.get(i) != 0) {
                if (i <= 0) {
                    incoming.append("----  BUY  ----");
                    System.out.println("----  BUY  ----");
                }
                buyTemp = buyVolume.get(i);
                priceBuy = buyPrice.get(i);
                System.out
                        .println("BUY  " + Math.abs(buyTemp) + "@" + priceBuy);
                incoming.append("BUY  " + Math.abs(buyTemp) + "@" + priceBuy);
            } else {
                System.out.println("----  BUY  ----");
                incoming.append("----  BUY  ----");
            }
        }
        for (int j = 0; j < sellVolume.size(); j++) {
            if (j <= 0) {
                incoming.append("----  SELL  ----");
                System.out.println("----  SELL  ----");
            }
            if (sellVolume.get(j) != 0) {
                sellTemp = sellVolume.get(j);
                priceSell = sellPrice.get(j);
                incoming.append("SELL  " + Math.abs(sellTemp) + "@" + priceSell);
                System.out.println("SELL  " + Math.abs(sellTemp) + "@"
                        + priceSell);
            } else {
                incoming.append("----  SELL  ----");
                System.out.println("----  SELL  ----");
            }
        }

        if ((buyVolume.size() == 0) && (sellVolume.size() == 0)) {
            incoming.append("----  SELL  ----");
            System.out.println("----  SELL  ----");
            incoming.append("");
            System.out.println();
            incoming.append("----  BUY  ----");
            System.out.println("----  BUY  ----");
        }
    }

}

以下错误.....

  

卖出100 @ 10   阅读卖出100 @ 10   出售100 @ 10   卖   100   10

     

阅读

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    at orderMatcher2.OrderMatcher2.orderOperationValidation(OrderMatcher2.java:122)
    at orderMatcher2.OrderMatcher2$1.actionPerformed(OrderMatcher2.java:79)
    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$500(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)

1 个答案:

答案 0 :(得分:0)

您应该确保orderOperationValidation的输入不为空,然后将其拆分。如果您调试代码,您会在某些时候看到您将空字符串传递给orderOperationValidation,如果((Integer.parseInt(tokens [1])&lt; 0) || (Integer.parseInt(tokens [2])&lt; 0)将抛出ArrayIndexOutOfBoundsException,因为标记没有2或3个项目。