Implementing back/forward buttons in Swing

后端 未结 3 658
生来不讨喜
生来不讨喜 2020-11-22 01:00

I have a quick question.

I\'m getting a little bit of experience with Swing and the easiest way to do this was to draw up a reasonably big GUI.

As part of t

3条回答
  •  时光说笑
    2020-11-22 01:57

    Here's an example using CardLayout.

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.util.Random;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    /** @see http://stackoverflow.com/questions/5654926 */
    public class CardPanel extends JPanel {
    
        private static final Random random = new Random();
        private static final JPanel cards = new JPanel(new CardLayout());
        private final String name;
    
        public CardPanel(String name) {
            this.name = name;
            this.setPreferredSize(new Dimension(320, 240));
            this.setBackground(new Color(random.nextInt()));
            this.add(new JLabel(name));
        }
    
        @Override
        public String toString() {
            return name;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    create();
                }
            });
        }
    
        private static void create() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            for (int i = 1; i < 9; i++) {
                CardPanel p = new CardPanel("Panel " + String.valueOf(i));
                cards.add(p, p.toString());
            }
            JPanel control = new JPanel();
            control.add(new JButton(new AbstractAction("\u22b2Prev") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    CardLayout cl = (CardLayout) cards.getLayout();
                    cl.previous(cards);
                }
            }));
            control.add(new JButton(new AbstractAction("Next\u22b3") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    CardLayout cl = (CardLayout) cards.getLayout();
                    cl.next(cards);
                }
            }));
            f.add(cards, BorderLayout.CENTER);
            f.add(control, BorderLayout.SOUTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
    

提交回复
热议问题