在actionListener之前填充了JTextArea

时间:2013-11-23 16:08:44

标签: java swing netbeans jtextarea

我似乎无法弄清楚为什么我在运行此程序时填充了JTextArea。我曾经设置它来填充JTextArea中的项目,一旦使用点击开始按钮,由于某种原因我丢失了我的工作(不要问)。所以现在我想要运行它,但它总是填充。我试图设置JTextArea = null;但NetBeans希望我将文本区域设置为final。也许这就是问题所在。下面发布的是代码

public class InventoryGUI2 {

    private static ArrayList<inventoryItem> inventory = new ArrayList<>();

    public static void main(String[] args) {
        makeWindow();

    }

    public static void makeWindow() {

        final JTextArea outputText = new JTextArea(30,40);
        JFrame newFrame = new JFrame();
        //outputText = new JTextArea(30, 40);

        newFrame.setSize(600, 600);
        newFrame.setLocationRelativeTo(null);
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension dim = tk.getScreenSize(); //get screen size from host OS
        int xPos = (dim.width / 2) - (newFrame.getWidth() / 2); //Center the Screen horizontally
        int yPos = (dim.height / 2) - (newFrame.getHeight() / 2); //center the screen vertically

        newFrame.setLocation(xPos, yPos);
        newFrame.setResizable(false);

        newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set window to close once pressing the close button
        newFrame.setTitle("Welcome"); //title of the window

        JPanel thePanel = new JPanel(); //new panel
        JLabel label1 = new JLabel("Inventory Program"); //label at the top of the window
        thePanel.add(label1); //add the label to the panel

        JButton startButton = new JButton("Start"); //create start button
        startButton.setToolTipText("Start Program"); //set tooltip for start button
        startButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                outputText.append(inventory.toString());
            }

        }); //listen for actions on start button
        //  startButton.addActionListener(new ActionListener() {});

        JButton exitButton = new JButton("Exit"); //create exit button
        exitButton.setToolTipText("Exit Program"); //set tooltip for exit button
        exitButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);

            }

        });
      //  outputText = new JTextArea(30, 40);

        outputText.setLineWrap(true);
        outputText.setWrapStyleWord(true);
        JScrollPane scrollbar = new JScrollPane(outputText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        thePanel.add(scrollbar);
        newFrame.add(thePanel);
        thePanel.add(startButton);
        thePanel.add(exitButton);
        newFrame.setVisible(true);

        inventory.add(new inventoryItem("Pencil", 1111, 50, .25));
        inventory.add(new inventoryItem("Pen", 2222, 50, 1.00));
        inventory.add(new inventoryItem("Canned Air", 3333, 5, 2.00));
        inventory.add(new inventoryItem("Notebook", 4444, 10, 2.50));
        inventory.add(new inventoryItem("Staples", 5555, 5, 1.00));
        inventory.add(new inventoryItem("Paper Clips", 6666, 100, .25));
        inventory.add(new inventoryItem("Rubber Bands", 7777, 10000, .10));
        inventory.add(new officeSupplyItem("Mechanical Pencil", 1112, 25, .50));
        inventory.add(new officeSupplyItem("Lead Pencil", 1113, 25, .25));
        inventory.add(new officeSupplyItem("Blue Pen", 2221, 25, 1.00));
        inventory.add(new officeSupplyItem("Black Pen", 2223, 5, 1.00));
        inventory.add(new officeSupplyItem("Red Pen", 2224, 20, 1.00));
        inventory.add(new officeSupplyItem("Steno Notebook", 4441, 5, 2.50));
        inventory.add(new officeSupplyItem("Legal Pad", 4442, 5, 2.50));

        inventory = sortInventory(inventory);
        for (int i = 0; i < inventory.size(); i++) {
            inventory.get(i).output(outputText);
        }
        inventoryTotal(inventory, outputText);
        sortInventory(inventory);
    }

    static ArrayList sortInventory(ArrayList<inventoryItem> unsorted) {
        ArrayList<inventoryItem> sorted = new ArrayList<>();
        inventoryItem alpha = null;
        int lowestIndex = -1;
        while (unsorted.size() > 0) {
            for (int i = 0; i < unsorted.size(); i++) {
                if (alpha == null) {
                    alpha = unsorted.get(i);
                    lowestIndex = i;
                } else if (unsorted.get(i).itemName.compareToIgnoreCase(alpha.itemName) < 0) {
                    alpha = unsorted.get(i);
                    lowestIndex = i;
                }

            }
            sorted.add(alpha);
            unsorted.remove(lowestIndex);
            alpha = null;
            lowestIndex = -1;
        }
        return sorted;

    }

    static void inventoryTotal(ArrayList<inventoryItem> inventory, JTextArea outputText) {
        double total = 0;

        for (int i = 0; i < inventory.size(); i++) {
            total = total + inventory.get(i).value;

        }
        String totalS;
        totalS = String.valueOf((double) total);
        outputText.append("Total value of inventory $" + totalS);
    }

}

class inventoryItem { //Delcare variables below

    String itemName;
    String newUnit;
    String newFee;
    String newValue;
    String newInventoryValue;
    int itemNumber;
    int inStock;
    double unitPrice;
    double value;
    double restockingFee;

    double inventoryValue;

    public inventoryItem(String itemName, int itemNumber, int inStock, double unitPrice) { //declare variables for this class
        this.itemName = itemName;
        this.itemNumber = itemNumber;
        this.inStock = inStock;
        this.unitPrice = unitPrice;
        restockingFee = .05 * value;
        stockValue();

    }

    public void stockValue() {
        value = unitPrice * inStock;
        restockingFee = .05 * unitPrice;
        inventoryValue = restockingFee + value;
        newUnit = String.valueOf((double) unitPrice);
        newFee = String.valueOf((double) restockingFee);
        newValue = String.valueOf((double) value);
        newInventoryValue = String.valueOf(inventoryValue);

    }

    public void output(JTextArea outputText) {
        outputText.append("Item Name = " + itemName + " \n"); //print out the item name
        outputText.append("Item Number = " + itemNumber + " \n"); //print out the item number
        outputText.append("In Stock = " + inStock + " \n"); //print out how many of the item are in stock
        outputText.append("Item Price = $" + newUnit + " \n"); //print out the price per item
        outputText.append("Restocking fee is $" + newFee + " per item \n");
        outputText.append("Value of item inventory = $" + newValue + " \n"); //calculate how much the inventory is worth for this one item
        outputText.append("Cost of inventory including restocking fee = $" + newInventoryValue + " \n");

    }
}

class officeSupplyItem extends inventoryItem {

    double feeToRestock;

    public officeSupplyItem(String itemName, int itemNumber, int inStock, double unitPrice) {
        super(itemName, itemNumber, inStock, unitPrice);
        feeToRestock = .05 * unitPrice;

    }

    @Override
    public void output(JTextArea outputText) {
        outputText.append("Item Name = " + itemName + " \n"); //print out the item name
        outputText.append("Item Number = " + itemNumber + " \n"); //print out the item number
        outputText.append("In Stock = " + inStock + " \n"); //print out how many of the item are in stock
        outputText.append("Item Price = $" + newUnit + "\n"); //print out the price per item
        outputText.append("Restocking fee is $" + newFee + " per item \n");
        outputText.append("Value of item inventory = $" + newValue + " \n"); //calculate how much the inventory is worth for this one item
        outputText.append("Cost of inventory including restocking fee = $" + newInventoryValue + "\n");

    }
}

1 个答案:

答案 0 :(得分:1)

主要方法调用makeWindow()

makeWindow()来电inventory.get(i).output(outputText)inventoryTotal(inventory, outputText)

这两种方法写入文本区域:

outputText.append("Total value of inventory $" + totalS);

outputText.append("Item Name = " + itemName + " \n");
...        

就这么简单。