Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.text.*;
- import java.util.*;
- public class Widget extends JFrame implements ActionListener
- {
- DataOutputStream output;
- DataInputStream input;
- String[] styleStrings = { "Economy", "Standard", "Advanced", "Exceptional" };
- String[] colorStrings = { "Red", "Green", "Blue", "Yellow" };
- int economyCount, standardCount, advancedCount, exceptionalCount, redCount, greenCount, blueCount, yellowCount, entryValue;
- JPanel economyPanel = new JPanel();
- JPanel standardPanel = new JPanel();
- JPanel advancedPanel = new JPanel();
- JPanel exceptionalPanel = new JPanel();
- JPanel redPanel = new JPanel();
- JPanel greenPanel = new JPanel();
- JPanel bluePanel = new JPanel();
- JPanel yellowPanel = new JPanel();
- JLabel selectStyle = new JLabel("Select a style: ");
- JComboBox styleList = new JComboBox(styleStrings);
- JLabel selectColor = new JLabel("Select a color: ");
- JComboBox colorList = new JComboBox(colorStrings);
- JLabel enterAmount = new JLabel("Enter amount: ");
- JTextField enterField = new JTextField(5);
- JButton submitButton = new JButton("Submit");
- JLabel blankLabel = new JLabel("");
- JLabel blankLabel2 = new JLabel("");
- JLabel totalLabel = new JLabel("Total Widgets:");
- JLabel economyLabel = new JLabel("Economy: " + economyCount);
- JLabel standardLabel = new JLabel("Standard: " + standardCount);
- JLabel advancedLabel = new JLabel("Advanced: " + advancedCount);
- JLabel exceptionalLabel = new JLabel("Exceptional: " + exceptionalCount);
- JLabel redLabel = new JLabel("Red: " + redCount);
- JLabel greenLabel = new JLabel("Green: " + greenCount);
- JLabel blueLabel = new JLabel("Blue: " + blueCount);
- JLabel yellowLabel = new JLabel("Yellow: " + yellowCount);
- public static void main(String[] args)
- {
- Widget w = new Widget();
- w.setSize(500, 500);
- w.setTitle("ABC Computers Inventory Management System");
- w.setResizable(true);
- w.setLocation(300,300);
- w.setVisible(true);
- w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public Widget()
- {
- Container c = getContentPane();
- c.setLayout((new GridLayout(9,2)));
- FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT);
- economyPanel.setLayout(rowSetup);
- standardPanel.setLayout(rowSetup);
- advancedPanel.setLayout(rowSetup);
- exceptionalPanel.setLayout(rowSetup);
- redPanel.setLayout(rowSetup);
- greenPanel.setLayout(rowSetup);
- bluePanel.setLayout(rowSetup);
- yellowPanel.setLayout(rowSetup);
- economyPanel.add(economyLabel);
- standardPanel.add(standardLabel);
- advancedPanel.add(advancedLabel);
- exceptionalPanel.add(exceptionalLabel);
- redPanel.add(redLabel);
- greenPanel.add(greenLabel);
- bluePanel.add(blueLabel);
- yellowPanel.add(yellowLabel);
- c.add(selectStyle);
- c.add(selectColor);
- c.add(styleList);
- c.add(colorList);
- c.add(enterAmount);
- c.add(enterField);
- c.add(submitButton);
- c.add(blankLabel);
- c.add(totalLabel);
- c.add(blankLabel2);
- c.add(economyPanel);
- c.add(redPanel);
- c.add(standardPanel);
- c.add(greenPanel);
- c.add(advancedPanel);
- c.add(bluePanel);
- c.add(exceptionalPanel);
- c.add(yellowPanel);
- styleList.setSelectedIndex(1);
- styleList.setEditable(false);
- styleList.addActionListener(this);
- colorList.setSelectedIndex(1);
- colorList.setEditable(false);
- colorList.addActionListener(this);
- submitButton.addActionListener(this);
- try
- {
- output = new DataOutputStream(new FileOutputStream("widgetdata.dat")); //tries to create a new file with the name created above
- }
- catch(IOException io)
- {
- JOptionPane.showMessageDialog(null, "The program could not create a storage location. Please check the disk drive and then run the program again.", "Error", JOptionPane.INFORMATION_MESSAGE);
- System.exit(1);
- }
- try
- {
- input = new DataInputStream(new FileInputStream("widgetdata.dat")); //tries to read the file with the name created above
- }
- catch(IOException io)
- {
- JOptionPane.showMessageDialog(null, "The program could not find the storage location. Please check the disk drive and then run the program again.", "Error", JOptionPane.INFORMATION_MESSAGE);
- System.exit(1);
- }
- addWindowListener(
- new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and save the file?", "File Submission", JOptionPane.YES_NO_OPTION);
- if (answer == JOptionPane.YES_OPTION) //allows the program to exit when the x is hit
- System.exit(0);
- }
- }
- );
- }
- public JMenuBar createMenuBar()
- {
- JMenuBar mnuBar = new JMenuBar();
- setJMenuBar(mnuBar);
- JMenu mnuFile = new JMenu("File", true);
- mnuFile.setMnemonic(KeyEvent.VK_F);
- mnuFile.setDisplayedMnemonicIndex(0);
- mnuBar.add(mnuFile);
- JMenuItem mnuFileExit = new JMenuItem("Exit");
- mnuFileExit.setMnemonic(KeyEvent.VK_X);
- mnuFileExit.setDisplayedMnemonicIndex(1);
- mnuFile.add(mnuFileExit);
- mnuFileExit.setActionCommand("Exit");
- mnuFileExit.addActionListener(this);
- JMenu mnuHelp = new JMenu("Help", true);
- mnuHelp.setMnemonic(KeyEvent.VK_E);
- mnuHelp.setDisplayedMnemonicIndex(0);
- mnuBar.add(mnuHelp);
- return mnuBar;
- }
- @Override
- public void actionPerformed(ActionEvent e)
- {
- String chosenStyle = (String)styleList.getSelectedItem();
- String chosenColor = (String)colorList.getSelectedItem();
- String entryNumbers = enterField.getText();
- int entryValue = Integer.parseInt(entryNumbers);
- try
- {
- int economyCount = input.readInt();
- int redCount = input.readInt();
- int standardCount = input.readInt();
- int greenCount = input.readInt();
- }
- catch(IOException io)
- {
- JOptionPane.showMessageDialog(null, "The program could not read the data. Please check the disk drive and then run the program again.", "Error", JOptionPane.INFORMATION_MESSAGE);
- System.exit(1);
- }
- if ("Submit".equals(e.getActionCommand()))
- {
- if (styleList.getSelectedItem().equals("Economy"))
- {
- economyCount = economyCount + entryValue;
- economyLabel.setText("Economy: " + economyCount);
- }
- if (styleList.getSelectedItem().equals("Standard"))
- {
- standardCount = standardCount + entryValue;
- standardLabel.setText("Standard: " + standardCount);
- }
- if (styleList.getSelectedItem().equals("Advanced"))
- {
- advancedCount = advancedCount + entryValue;
- advancedLabel.setText("Advanced: " + advancedCount);
- }
- if (styleList.getSelectedItem().equals("Exceptional"))
- {
- exceptionalCount = exceptionalCount + entryValue;
- exceptionalLabel.setText("Exceptional: " + exceptionalCount);
- }
- if (colorList.getSelectedItem().equals("Red"))
- {
- redCount = redCount + entryValue;
- redLabel.setText("Red: " + redCount);
- }
- if (colorList.getSelectedItem().equals("Green"))
- {
- greenCount = greenCount + entryValue;
- greenLabel.setText("Green: " + greenCount);
- }
- if (colorList.getSelectedItem().equals("Blue"))
- {
- blueCount = blueCount + entryValue;
- blueLabel.setText("Blue: " + blueCount);
- }
- if (colorList.getSelectedItem().equals("Yellow"))
- {
- yellowCount = yellowCount + entryValue;
- yellowLabel.setText("Yellow: " + yellowCount);
- }
- try
- {
- output.writeInt(economyCount); //saves the data
- output.writeInt(standardCount);
- output.writeInt(advancedCount);
- output.writeInt(exceptionalCount);
- output.writeInt(redCount);
- output.writeInt(greenCount);
- output.writeInt(blueCount);
- output.writeInt(yellowCount);
- JOptionPane.showMessageDialog(null, "The widget information has been saved.", "Submission Successful", JOptionPane.INFORMATION_MESSAGE);
- }
- catch(IOException c)
- {
- System.exit(1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement