Advertisement
soupbones

widgets java

May 19th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.04 KB | None | 0 0
  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.text.*;
  6. import java.util.*;
  7.  
  8. public class Widget extends JFrame implements ActionListener
  9. {
  10.     DataOutputStream output;
  11.     DataInputStream input;
  12.    
  13.     String[] styleStrings = { "Economy", "Standard", "Advanced", "Exceptional" };
  14.     String[] colorStrings = { "Red", "Green", "Blue", "Yellow" };
  15.    
  16.     int economyCount, standardCount, advancedCount, exceptionalCount, redCount, greenCount, blueCount, yellowCount, entryValue;
  17.    
  18.     JPanel economyPanel = new JPanel();
  19.     JPanel standardPanel = new JPanel();
  20.     JPanel advancedPanel = new JPanel();
  21.     JPanel exceptionalPanel = new JPanel();
  22.     JPanel redPanel = new JPanel();
  23.     JPanel greenPanel = new JPanel();
  24.     JPanel bluePanel = new JPanel();
  25.     JPanel yellowPanel = new JPanel();
  26.    
  27.     JLabel selectStyle = new JLabel("Select a style: ");
  28.         JComboBox styleList = new JComboBox(styleStrings);
  29.            
  30.     JLabel selectColor = new JLabel("Select a color: ");
  31.         JComboBox colorList = new JComboBox(colorStrings);
  32.    
  33.     JLabel enterAmount = new JLabel("Enter amount: ");
  34.         JTextField enterField = new JTextField(5);
  35.        
  36.     JButton submitButton = new JButton("Submit");
  37.    
  38.     JLabel blankLabel = new JLabel("");
  39.     JLabel blankLabel2 = new JLabel("");
  40.    
  41.     JLabel totalLabel = new JLabel("Total Widgets:");
  42.    
  43.     JLabel economyLabel = new JLabel("Economy: " + economyCount);
  44.     JLabel standardLabel = new JLabel("Standard: " + standardCount);
  45.     JLabel advancedLabel = new JLabel("Advanced: " + advancedCount);
  46.     JLabel exceptionalLabel = new JLabel("Exceptional: " + exceptionalCount);
  47.    
  48.     JLabel redLabel = new JLabel("Red: " + redCount);
  49.     JLabel greenLabel = new JLabel("Green: " + greenCount);
  50.     JLabel blueLabel = new JLabel("Blue: " + blueCount);
  51.     JLabel yellowLabel = new JLabel("Yellow: " + yellowCount);
  52.    
  53.     public static void main(String[] args)
  54.     {
  55.         Widget w = new Widget();
  56.         w.setSize(500, 500);
  57.         w.setTitle("ABC Computers Inventory Management System");
  58.         w.setResizable(true);
  59.         w.setLocation(300,300);
  60.         w.setVisible(true);
  61.         w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62.     }
  63.    
  64.     public Widget()
  65.     {
  66.         Container c = getContentPane();
  67.         c.setLayout((new GridLayout(9,2)));
  68.    
  69.         FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT);
  70.             economyPanel.setLayout(rowSetup);
  71.             standardPanel.setLayout(rowSetup);
  72.             advancedPanel.setLayout(rowSetup);
  73.             exceptionalPanel.setLayout(rowSetup);
  74.             redPanel.setLayout(rowSetup);
  75.             greenPanel.setLayout(rowSetup);
  76.             bluePanel.setLayout(rowSetup);
  77.             yellowPanel.setLayout(rowSetup);
  78.        
  79.         economyPanel.add(economyLabel);
  80.         standardPanel.add(standardLabel);
  81.         advancedPanel.add(advancedLabel);
  82.         exceptionalPanel.add(exceptionalLabel);
  83.         redPanel.add(redLabel);
  84.         greenPanel.add(greenLabel);
  85.         bluePanel.add(blueLabel);
  86.         yellowPanel.add(yellowLabel);
  87.        
  88.         c.add(selectStyle);
  89.         c.add(selectColor);
  90.         c.add(styleList);
  91.         c.add(colorList);
  92.         c.add(enterAmount);
  93.         c.add(enterField);
  94.         c.add(submitButton);
  95.         c.add(blankLabel);
  96.         c.add(totalLabel);
  97.         c.add(blankLabel2);
  98.         c.add(economyPanel);
  99.         c.add(redPanel);
  100.         c.add(standardPanel);
  101.         c.add(greenPanel);
  102.         c.add(advancedPanel);
  103.         c.add(bluePanel);
  104.         c.add(exceptionalPanel);
  105.         c.add(yellowPanel);
  106.        
  107.         styleList.setSelectedIndex(1);
  108.         styleList.setEditable(false);
  109.         styleList.addActionListener(this);
  110.        
  111.         colorList.setSelectedIndex(1);
  112.         colorList.setEditable(false);
  113.         colorList.addActionListener(this);
  114.    
  115.         submitButton.addActionListener(this);
  116.        
  117.         try
  118.         {
  119.             output = new DataOutputStream(new FileOutputStream("widgetdata.dat")); //tries to create a new file with the name created above
  120.         }
  121.         catch(IOException io)
  122.         {
  123.             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);
  124.    
  125.             System.exit(1);
  126.         }
  127.        
  128.         try
  129.         {
  130.             input = new DataInputStream(new FileInputStream("widgetdata.dat")); //tries to read the file with the name created above
  131.         }
  132.         catch(IOException io)
  133.         {
  134.             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);
  135.    
  136.             System.exit(1);
  137.         }
  138.        
  139.         addWindowListener(
  140.         new WindowAdapter()
  141.             {
  142.                 public void windowClosing(WindowEvent e)
  143.                 {
  144.                     int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and save the file?", "File Submission", JOptionPane.YES_NO_OPTION);
  145.                     if (answer == JOptionPane.YES_OPTION) //allows the program to exit when the x is hit
  146.                         System.exit(0);
  147.                 }
  148.             }
  149.         );
  150.        
  151.     }
  152.  
  153.     public JMenuBar createMenuBar()
  154.     {
  155.         JMenuBar mnuBar = new JMenuBar();
  156.         setJMenuBar(mnuBar);
  157.  
  158.         JMenu mnuFile = new JMenu("File", true);
  159.             mnuFile.setMnemonic(KeyEvent.VK_F);
  160.             mnuFile.setDisplayedMnemonicIndex(0);
  161.             mnuBar.add(mnuFile);
  162.  
  163.         JMenuItem mnuFileExit = new JMenuItem("Exit");
  164.             mnuFileExit.setMnemonic(KeyEvent.VK_X);
  165.             mnuFileExit.setDisplayedMnemonicIndex(1);
  166.             mnuFile.add(mnuFileExit);
  167.             mnuFileExit.setActionCommand("Exit");
  168.             mnuFileExit.addActionListener(this);
  169.  
  170.         JMenu mnuHelp = new JMenu("Help", true);
  171.             mnuHelp.setMnemonic(KeyEvent.VK_E);
  172.             mnuHelp.setDisplayedMnemonicIndex(0);
  173.             mnuBar.add(mnuHelp);
  174.  
  175.         return mnuBar;
  176.     }
  177.    
  178.     @Override
  179.     public void actionPerformed(ActionEvent e)
  180.     {
  181.         String chosenStyle = (String)styleList.getSelectedItem();
  182.         String chosenColor = (String)colorList.getSelectedItem();
  183.         String entryNumbers = enterField.getText();
  184.         int entryValue = Integer.parseInt(entryNumbers);
  185.        
  186.         try
  187.         {
  188.             int economyCount = input.readInt();
  189.             int redCount = input.readInt();
  190.             int standardCount = input.readInt();
  191.             int greenCount = input.readInt();
  192.         }
  193.         catch(IOException io)
  194.         {
  195.             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);
  196.            
  197.             System.exit(1);
  198.         }
  199.        
  200.         if ("Submit".equals(e.getActionCommand()))
  201.         {
  202.             if (styleList.getSelectedItem().equals("Economy"))
  203.                 {
  204.                     economyCount = economyCount + entryValue;
  205.                     economyLabel.setText("Economy: " + economyCount);
  206.                 }
  207.            
  208.             if (styleList.getSelectedItem().equals("Standard"))
  209.                 {
  210.                     standardCount = standardCount + entryValue;
  211.                     standardLabel.setText("Standard: " + standardCount);
  212.                 }
  213.            
  214.             if (styleList.getSelectedItem().equals("Advanced"))
  215.                 {
  216.                     advancedCount = advancedCount + entryValue;
  217.                     advancedLabel.setText("Advanced: " + advancedCount);
  218.                 }
  219.                
  220.             if (styleList.getSelectedItem().equals("Exceptional"))
  221.                 {
  222.                     exceptionalCount = exceptionalCount + entryValue;
  223.                     exceptionalLabel.setText("Exceptional: " + exceptionalCount);
  224.                 }
  225.                
  226.             if (colorList.getSelectedItem().equals("Red"))
  227.                 {
  228.                     redCount = redCount + entryValue;
  229.                     redLabel.setText("Red: " + redCount);
  230.                 }
  231.                
  232.             if (colorList.getSelectedItem().equals("Green"))
  233.                 {
  234.                     greenCount = greenCount + entryValue;
  235.                     greenLabel.setText("Green: " + greenCount);
  236.                 }
  237.                
  238.             if (colorList.getSelectedItem().equals("Blue"))
  239.                 {
  240.                     blueCount = blueCount + entryValue;
  241.                     blueLabel.setText("Blue: " + blueCount);
  242.                 }
  243.                
  244.             if (colorList.getSelectedItem().equals("Yellow"))
  245.                 {
  246.                     yellowCount = yellowCount + entryValue;
  247.                     yellowLabel.setText("Yellow: " + yellowCount);
  248.                 }
  249.                
  250.             try
  251.                 {
  252.                     output.writeInt(economyCount); //saves the data
  253.                     output.writeInt(standardCount);
  254.                     output.writeInt(advancedCount);
  255.                     output.writeInt(exceptionalCount);
  256.                     output.writeInt(redCount);
  257.                     output.writeInt(greenCount);
  258.                     output.writeInt(blueCount);
  259.                     output.writeInt(yellowCount);
  260.                    
  261.                     JOptionPane.showMessageDialog(null, "The widget information has been saved.", "Submission Successful", JOptionPane.INFORMATION_MESSAGE);
  262.                 }
  263.                 catch(IOException c)
  264.                 {
  265.                 System.exit(1);
  266.                 }
  267.                
  268.         }
  269.     }  
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement