Guest User

Untitled

a guest
Feb 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. /**
  2. * Write a description of class UI here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import javax.swing.*;
  10. import java.awt.CardLayout;
  11.  
  12. public class UI implements ItemListener
  13. {
  14. JPanel cards;
  15. final static String BUTTONPANEL = "View Inventory";
  16. final static String TEXTPANEL = "Add Inventory";
  17.  
  18. public void addComponentToPane(Container pane)
  19. {
  20. JPanel comboBoxPane = new JPanel();
  21. String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL};
  22. JComboBox cb = new JComboBox(comboBoxItems);
  23. cb.setEditable(false);
  24. cb.addItemListener(this);
  25. comboBoxPane.add(cb);
  26.  
  27. JTextField quantityField = new JTextField(10);
  28. //creating the cards
  29. JPanel card1 = new JPanel();
  30. card1.add(new Label("Item"));
  31. card1.add(new Label("Price"));
  32. card1.add(new Label("Quantity"));
  33.  
  34. JPanel card2 = new JPanel();
  35. card2.add(new JTextField("TextField", 20));
  36.  
  37. //create panel with cards
  38. cards = new JPanel(new CardLayout());
  39. cards.add(card1, BUTTONPANEL);
  40. cards.add(card2, TEXTPANEL);
  41.  
  42. pane.add(comboBoxPane, BorderLayout.PAGE_START);
  43. pane.add(cards, BorderLayout.CENTER);
  44. }
  45.  
  46. public void itemStateChanged(ItemEvent evt) {
  47. CardLayout cl = (CardLayout)(cards.getLayout());
  48. cl.show(cards, (String)evt.getItem());
  49. }
  50.  
  51. /**
  52. * Create the GUI and show it. For thread safety,
  53. * this method should be invoked from the
  54. * event dispatch thread.
  55. */
  56. private static void createAndShowGUI() {
  57. //Create and set up the window.
  58. JFrame frame = new JFrame("CardLayoutDemo");
  59. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60.  
  61. //Create and set up the content pane.
  62. UI demo = new UI();
  63. demo.addComponentToPane(frame.getContentPane());
  64.  
  65. //Display the window.
  66. frame.pack();
  67. frame.setSize(300,300);
  68. frame.setVisible(true);
  69. }
  70.  
  71. public static void main(String[] args) {
  72. /* Use an appropriate Look and Feel */
  73. try {
  74. //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  75. UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  76. } catch (UnsupportedLookAndFeelException ex) {
  77. ex.printStackTrace();
  78. } catch (IllegalAccessException ex) {
  79. ex.printStackTrace();
  80. } catch (InstantiationException ex) {
  81. ex.printStackTrace();
  82. } catch (ClassNotFoundException ex) {
  83. ex.printStackTrace();
  84. }
  85. /* Turn off metal's use of bold fonts */
  86. UIManager.put("swing.boldMetal", Boolean.FALSE);
  87.  
  88. //Schedule a job for the event dispatch thread:
  89. //creating and showing this application's GUI.
  90. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  91. public void run() {
  92. createAndShowGUI();
  93. }
  94. });
  95. }
  96.  
  97.  
  98.  
  99. }
Add Comment
Please, Sign In to add comment