Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class UI here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.CardLayout;
- public class UI implements ItemListener
- {
- JPanel cards;
- final static String BUTTONPANEL = "View Inventory";
- final static String TEXTPANEL = "Add Inventory";
- public void addComponentToPane(Container pane)
- {
- JPanel comboBoxPane = new JPanel();
- String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL};
- JComboBox cb = new JComboBox(comboBoxItems);
- cb.setEditable(false);
- cb.addItemListener(this);
- comboBoxPane.add(cb);
- JTextField quantityField = new JTextField(10);
- //creating the cards
- JPanel card1 = new JPanel();
- card1.add(new Label("Item"));
- card1.add(new Label("Price"));
- card1.add(new Label("Quantity"));
- JPanel card2 = new JPanel();
- card2.add(new JTextField("TextField", 20));
- //create panel with cards
- cards = new JPanel(new CardLayout());
- cards.add(card1, BUTTONPANEL);
- cards.add(card2, TEXTPANEL);
- pane.add(comboBoxPane, BorderLayout.PAGE_START);
- pane.add(cards, BorderLayout.CENTER);
- }
- public void itemStateChanged(ItemEvent evt) {
- CardLayout cl = (CardLayout)(cards.getLayout());
- cl.show(cards, (String)evt.getItem());
- }
- /**
- * Create the GUI and show it. For thread safety,
- * this method should be invoked from the
- * event dispatch thread.
- */
- private static void createAndShowGUI() {
- //Create and set up the window.
- JFrame frame = new JFrame("CardLayoutDemo");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //Create and set up the content pane.
- UI demo = new UI();
- demo.addComponentToPane(frame.getContentPane());
- //Display the window.
- frame.pack();
- frame.setSize(300,300);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- /* Use an appropriate Look and Feel */
- try {
- //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
- UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
- } catch (UnsupportedLookAndFeelException ex) {
- ex.printStackTrace();
- } catch (IllegalAccessException ex) {
- ex.printStackTrace();
- } catch (InstantiationException ex) {
- ex.printStackTrace();
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- }
- /* Turn off metal's use of bold fonts */
- UIManager.put("swing.boldMetal", Boolean.FALSE);
- //Schedule a job for the event dispatch thread:
- //creating and showing this application's GUI.
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment