在CardLayout中显示不同的面板大小

时间:2014-08-17 05:28:29

标签: java swing layout-manager cardlayout

我有JPanelcardLayoutPanel),其布局为CardLayout

我还有另外3个不同的面板(firstPagePanelsecondPagePanelthirdPagePanel

  • firstPagePanel大小约为450x400左右,
  • secondPagePanel大小约为800x600
  • thirdPagePanel大小约为1024x768

cardLayoutPanel.add所有3个小组,我首先显示firstPagePanel作为我的观点。

我想让我的程序首先显示firstPagePanel的大小,即450x400 然后,如果显示secondPagePanel,它会将大小更改为800x600,如果显示thirdPagePanel,则会将大小更改为1024,768

我使用frame.getPreferredSize()而不是猜测我的3个面板大小应该是什么,但我的第一个视图总是采用我thirdPagePanel的大小,即1024x768而不是450x400;

我该怎么做才能解决这个问题?

public class MainFrame { 
    private CardLayout cardLayout = new CardLayout();
    private JPanel cardLayoutPanel = new JPanel();
    private FirstPagePanel firstPagePanel = new FirstPagePanel(); 
    private SecondPagePanel secondPagePanel = new SecondPagePanel();    
    private ThirdPagePanel thirdPagePanel = new ThirdPagePanel();
    private JFrame frame = new JFrame("Panel size test");

    public MainFrame() {
        cardLayoutPanel.setLayout(cardLayout);
        cardLayoutPanel.add(firstPagePanel,"1");
        cardLayoutPanel.add(secondPagePanel,"2");
        cardLayoutPanel.add(thirdPagePanel,"3");
        cardLayout.show(cardLayoutPanel,"1");
        frame.add(cardLayoutPanel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.getPreferredSize();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
    }
}

1 个答案:

答案 0 :(得分:2)

最简单的方法是:

  • 删除当前面板。
  • 添加新面板。
  • 打包顶级容器。

E.G。

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ThreeSizeGui {

    public static void swapComponentsAndResizeUI(
            JComponent ui, 
            JComponent current,
            JComponent next) {
        ui.remove(current);
        ui.add(next);
        current = next;
        Component c = ui.getTopLevelAncestor();
        if (c instanceof Window) {
            Window w = (Window) c;
            w.pack();
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                final JPanel ui = new JPanel(new BorderLayout(4, 4));
                ui.setBorder(new EmptyBorder(6, 6, 6, 6));

                JPanel controls = new JPanel(
                        new FlowLayout(FlowLayout.LEADING));
                ui.add(controls, BorderLayout.PAGE_START);
                int s = 100;
                Dimension[] sizes = {
                    new Dimension(s * 4, s * 2),
                    new Dimension(s * 6, s * 3),
                    new Dimension(s * 8, s * 4)
                };
                final JComboBox cb = new JComboBox(sizes);
                controls.add(cb);
                final JPanel[] panels = new JPanel[sizes.length];
                for (int ii = 0; ii < sizes.length; ii++) {
                    Dimension d = sizes[ii];
                    BufferedImage bi = new BufferedImage(
                            d.width, d.height, BufferedImage.TYPE_INT_RGB);
                    JPanel p = new JPanel(new GridLayout());
                    JLabel l = new JLabel(new ImageIcon(bi));
                    p.add(l);
                    panels[ii] = p;
                }
                ItemListener sizeListener = new ItemListener() {

                    JPanel current = panels[0];

                    @Override
                    public void itemStateChanged(ItemEvent e) {
                        JPanel next = panels[cb.getSelectedIndex()];
                        swapComponentsAndResizeUI(ui, current, next);
                        current = next;
                    }
                };
                cb.addItemListener(sizeListener);

                ui.add(panels[0], BorderLayout.CENTER);

                JFrame f = new JFrame("Three Sized Panels");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(ui);
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}