Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cardlayout;
- import java.awt.BorderLayout;
- import java.awt.CardLayout;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.DefaultListModel;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- public class CardLayoutDemo {
- JFrame frame = new JFrame("CardLayout demo");
- JPanel panelCont = new JPanel(); // panel cu alte 2 panouri
- JPanel panelFirst = new JPanel();
- JPanel panelSecond = new JPanel();
- JPanel panelThird = new JPanel();
- JButton buttonOne = new JButton("Switch to second panel/workspace");
- JButton buttonSecond = new JButton("Switch to third panel/workspace");
- JButton buttonThird = new JButton("Switch to first panel-EAST");
- CardLayout cl = new CardLayout();
- public CardLayoutDemo() {
- panelCont.setLayout(cl);
- panelFirst.add(buttonOne);
- panelSecond.add(buttonSecond);
- panelFirst.setBackground(Color.BLUE);
- panelSecond.setBackground(Color.GREEN);
- panelThird.setBackground(Color.ORANGE);
- panelThird.setLayout(new BorderLayout());
- panelThird.add(new JButton("Hello Guys -- NORTH!"),BorderLayout.NORTH);
- DefaultListModel dlm = new DefaultListModel();
- dlm.addElement("WEST !");
- dlm.addElement("This is a list !");
- dlm.addElement("On West");
- panelThird.add(new JList(dlm),BorderLayout.WEST);
- panelThird.add(new JLabel("CENTER.Asta e facut cu BorderLayout!"),BorderLayout.CENTER);
- JTextField tf = new JTextField("Write Here! --- SOUTH");
- tf.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){dlm.addElement(e.getActionCommand());}});
- panelThird.add(tf,BorderLayout.SOUTH);
- panelThird.add(buttonThird,BorderLayout.EAST);
- panelSecond.setLayout(new FlowLayout());
- panelSecond.add(new JLabel("Acest layout este de tipul FlowLayout"));
- panelSecond.add(new JButton("1 - 2 - 3"));
- panelSecond.add(new JTextField("TEXTFIELD de lungimea textului initial"));
- panelFirst.setLayout(null);
- JTextField tf2 = new JTextField("Acesta este un Layout NULL ( mai putin recomandat )");
- tf2.setBounds(100,200,370,180);
- panelFirst.add(tf2);
- buttonOne.setBounds(50,50,250,150);
- panelCont.add(panelFirst, "1");
- panelCont.add(panelSecond, "2");
- panelCont.add(panelThird,"3");
- cl.show(panelCont, "1"); // 1 afiseaza firstPanel, 2 te duce pe secondPanel
- buttonOne.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- cl.show(panelCont, "2");
- }
- });
- buttonSecond.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- cl.show(panelCont, "3");
- }
- });
- buttonThird.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- cl.show(panelCont, "1");
- }
- });
- frame.add(panelCont);
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.pack();
- frame.setSize(500,500);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new CardLayoutDemo();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement