如何使用CardLayout在面板之间切换?

时间:2019-04-25 08:49:49

标签: java swing nullpointerexception layout-manager cardlayout

我制作了一个parentPanel,上面有一个CardLayout,在此之下,我又制作了四个JPanel容器。

在左侧,我有4个按钮,当我按"Forside"(按钮)时,我想切换到卡布局上的面板(Forside),依此类推...

我尝试了不同的youtube教程,并试图在这里查看而未获得任何成功。 我尝试过的一切都以NullPointerException

结尾
public class Main extends JFrame {

    private JPanel contentPane;

    int xx, xy;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setUndecorated(true); // Hides the jframe top bar
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Main() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 735, 506);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(102, 102, 102));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panelLeft = new JPanel();
        panelLeft.setBackground(new Color(51, 51, 51));
        panelLeft.setForeground(Color.DARK_GRAY);
        panelLeft.setBounds(0, 54, 150, 459);
        contentPane.add(panelLeft);
        panelLeft.setLayout(null);

        JButton btnForside = new JButton("Forside");
        btnForside.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

            }
        });
        btnForside.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnForside.setForeground(Color.WHITE);
        btnForside.setFont(new Font("Tahoma", Font.PLAIN, 17));
        btnForside.setIcon(new ImageIcon(Main.class.getResource("/Images/icons8_Home_32px_1.png")));
        btnForside.setContentAreaFilled(false);
        btnForside.setBorderPainted(false);
        btnForside.setBorder(null);
        btnForside.setBounds(16, 60, 112, 30);
        panelLeft.add(btnForside);

        JButton btnDagbog = new JButton("Dagbog");
        btnDagbog.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });
        btnDagbog.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnDagbog.setContentAreaFilled(false);
        btnDagbog.setBorderPainted(false);
        btnDagbog.setBorder(null);
        btnDagbog.setFont(new Font("Tahoma", Font.PLAIN, 17));
        btnDagbog.setForeground(Color.WHITE);
        btnDagbog.setIcon(new ImageIcon(Main.class.getResource("/Images/icons8_Book_32px.png")));
        btnDagbog.setBounds(16, 116, 112, 30);
        panelLeft.add(btnDagbog);

        JButton btnAftaler = new JButton("Aftaler");
        btnAftaler.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnAftaler.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnAftaler.setContentAreaFilled(false);
        btnAftaler.setBorderPainted(false);
        btnAftaler.setBorder(null);
        btnAftaler.setForeground(Color.WHITE);
        btnAftaler.setFont(new Font("Tahoma", Font.PLAIN, 17));
        btnAftaler.setIcon(new ImageIcon(Main.class.getResource("/Images/icons8_Planner_32px.png")));
        btnAftaler.setBounds(16, 173, 112, 30);
        panelLeft.add(btnAftaler);

        JButton btnKontakt = new JButton("Kontakt");
        btnKontakt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnKontakt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnKontakt.setContentAreaFilled(false);
        btnKontakt.setBorder(null);
        btnKontakt.setBorderPainted(false);
        btnKontakt.setFont(new Font("Tahoma", Font.PLAIN, 17));
        btnKontakt.setForeground(Color.WHITE);
        btnKontakt.setIcon(new ImageIcon(Main.class.getResource("/Images/icons8_Phone_32px.png")));
        btnKontakt.setBounds(16, 231, 112, 30);
        panelLeft.add(btnKontakt);

        JPanel panelTop = new JPanel();
        panelTop.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent arg0) {
                int x = arg0.getXOnScreen(); // makes uggerhøj picture dragable
                int y = arg0.getYOnScreen(); // makes uggerhøj picture dragable
                Main.this.setLocation(x - xx, y - xy);  // makes uggerhøj picture dragable
            }
        });
        panelTop.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                 xx = e.getX(); // makes uggerhøj picture dragable
                 xy = e.getY(); // makes uggerhøj picture dragable
            }
        });
        panelTop.setBackground(new Color(51, 51, 51));
        panelTop.setBounds(0, 0, 737, 60);
        contentPane.add(panelTop);
        panelTop.setLayout(null);

        JButton btnX = new JButton("X");
        btnX.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnX.setRolloverIcon(null);
        btnX.setFont(new Font("Tahoma", Font.BOLD, 18));
        btnX.setFocusTraversalKeysEnabled(false);
        btnX.setFocusPainted(false);
        btnX.setBorderPainted(false);
        btnX.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        btnX.setContentAreaFilled(false);
        btnX.setForeground(SystemColor.activeCaption);
        btnX.setBorder(null);
        btnX.setBounds(615, 13, 97, 25);
        panelTop.add(btnX);

        JPanel parentPanel = new JPanel();
        parentPanel.setBackground(Color.GRAY);
        parentPanel.setBounds(148, 54, 569, 405);
        contentPane.add(parentPanel);
        parentPanel.setLayout(new CardLayout(0, 0));

        JPanel Forside = new JPanel();
        parentPanel.add(Forside, "name_1472174211097300");
        Forside.setFocusable(false);

        JButton btnTest = new JButton("test");
        Forside.add(btnTest);

        JPanel Dagbog = new JPanel();
        parentPanel.add(Dagbog, "name_1472176236196000");

        JLabel lblTest = new JLabel("dagbog");
        Dagbog.add(lblTest);


        JPanel Aftaler = new JPanel();
        parentPanel.add(Aftaler, "name_1472177885026100");

        JPanel Kontakt = new JPanel();
        parentPanel.add(Kontakt, "name_1472179607862700");
    }
}

我只想要它,以便正确的按钮指向正确的卡片。

1 个答案:

答案 0 :(得分:0)

首先,下一次请@AndrewThompson建议您对代码进行MCVE,@ camickr建议在进行实际代码之前这一步进行测试和调试(这是专家编程101)。

CardLayout对象通过方法

在容器的内容之间切换
CardLayout.show(Container parent, String name);

在摆动代码中导航时请切记不要在方法中将Components字段改为局部变量(至少在您的情况下,parentPanel和{{1} })

因此,为了使您的CardLayout在4个面板之间切换(很抱歉,您的语言不熟悉),我做了一个相当重构的代码版本。

parentPanel

和快乐的编码^-^。