简体   繁体   English

在ActionListener之前填充JTextArea

[英]JTextArea populated prior to actionListener

I can't seem to figure out why my JTextArea is populated the instant I run this program. 我似乎无法弄清楚为什么我在运行该程序时立即填充了JTextArea I had set it up to populate the items in the JTextArea once the use hit the start button, and for some reason I lost my work (don't ask). 一旦使用开始按钮,我就将其设置为填充JTextArea的项目,由于某种原因,我失去了工作(不要问)。 So now I am tyring to run it but it populates all the time. 因此,现在我想运行它,但它一直都在填充。 I have tried to set the JTextArea = null ; 我试图设置JTextArea = null ; however NetBeans wants me to set the text area to final. 但是NetBeans希望我将文本区域设置为final。 Maybe that is the problem. 也许这就是问题所在。 posted below is the code 下面发布的是代码

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");

    }
}

The main method calls makeWindow() . 主要方法调用makeWindow()

makeWindow() calls inventory.get(i).output(outputText) and inventoryTotal(inventory, outputText) . makeWindow()调用makeWindow() inventory.get(i).output(outputText)makeWindow() inventoryTotal(inventory, outputText)

These two methods write to the text area: 这两种方法将写入文本区域:

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

and

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

As simple as that. 就如此容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM