在Java Swings中切换/导航jpanel

时间:2012-10-06 16:39:48

标签: java swing layout jpanel

  • >我在包'abc'中定义了一个jframe。这个jframe也是一个主类。
  • >同一个包'abc'还包含4个不同java类中定义的jpanel(panel1,panel2,panel3,panel4)。

如何在用户点击不同按钮时从主jframe类调用/显示这些不同的jpanel? 如果我有40-50个这样的jpanel,那么从主类中切换这些面板的最有效方法是什么呢?

2 个答案:

答案 0 :(得分:4)

这是一个非常简单的示例,它使用next()和一个按钮来更改面板。

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.CardLayout;

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.border.Border;

public class TestCards extends JFrame {

    private final CardLayout cl = new CardLayout();
    private final JPanel cards = new JPanel(cl);
    private final Border border = BorderFactory.createEmptyBorder(60, 60, 60, 60);

    public TestCards() {

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JPanel panel1 = new JPanel(new GridBagLayout());
        panel1.setBorder(border);
        panel1.setBackground(Color.RED);
        panel1.add(new JLabel("Card 1"));
        cards.add(panel1, "First Panel");

        JPanel panel2 = new JPanel(new GridBagLayout());
        panel2.setBorder(border);
        panel2.setBackground(Color.GREEN);
        panel2.add(new JLabel("Card 2"));
        cards.add(panel2, "Second Panel");

        JPanel panel3 = new JPanel(new GridBagLayout());
        panel3.setBorder(border);
        panel3.setBackground(Color.BLUE);
        panel3.add(new JLabel("Card 3"));
        cards.add(panel3, "Third Panel");

        JButton controlButton = new JButton("Switch");
        controlButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.next(cards);
            }
        });
        JPanel controlPanel = new JPanel();
        controlPanel.setBackground(Color.LIGHT_GRAY);
        controlPanel.add(controlButton);

        contentPane.add(cards);
        contentPane.add(controlPanel);

        cl.show(cards, "First Panel");
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                TestCards frame = new TestCards();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

另一种选择(众多)可以使用3个按钮,每个按钮显示一个特定的面板(使用show代替next)。

有关详细信息,请参阅this

答案 1 :(得分:3)

CardLayout的previousnext方法可用于向前导航&分别通过卡组件返回。为了更容易地连续导航组件,您可以将卡名称放在String数组中:

private static final String[] CARD_NAMES = { "name_2996062106101", ... };

然后,要显示特定的cardIndex,您只需使用:

CardLayout layout = (CardLayout) contentPane.getLayout();
layout.show(contentPane, CARD_NAMES[cardIndex]);

有关详情,请参阅 How to Use CardLayout