合同卡布局如何

时间:2012-09-30 08:16:04

标签: java swing jframe jpanel cardlayout

我使用VerticalLayout (org.jdesktop.swingx.VerticalLayout)建立了 mainPanel 。主面板有几个子面板。其中之一是根据用户选择动态变化的面板。所以,我已将它的布局设置为CardLayout,我认为这是最简单的(也许是最好的?)方式。

我会调用该面板 elasticPanel 。顾名思义,它应该是有弹性的。这意味着它应该能够扩展和收缩。让我们说它表现得像这样。如果用户选择1,则 elasticPanel 应显示一个,例如JComboBox。如果用户选择2,则选择两个JComboBox ...

好的,到目前为止它完美无缺。现在,当1显示两个elasticPanel时,用户再次选择JComboBox。我现在需要做的是elasticPanel应该显示一个JComboBox的正常大小。但由于elasticPanel已经展开,所以会发生的事情是显示JComboBox拉伸以适应它的大小。所以它给人一种奇怪的外观。


以下屏幕截图显示了我的界面问题。

选择之前。 NONE已被选中。

enter image description here

选择了一个元素

enter image description here

再次选择

NONE

enter image description here

我需要在最后一个屏幕截图中显示elasticPanel错误的位置),如第一个屏幕截图所示。这只是一个简单的案例。想象一下在显示大约5个,6个子组件后回到NONE时的外观。

我尝试了setSize()方法。它没有做任何事情......那么如何解决这个问题呢?

感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:4)

很难说你指的是CardLayout什么东西。由于CardLayout以不同的方式工作。您可以做的只是将一个JPanel basePanel 放在GridLayout(0, 1),并将此JPanel置于另一个JPanel之上,说 contentPanel ,现在将其设置为JFrame的内容窗格,并在视图中添加或删除元素时调用pack()。这是一个显示我的意思的例子。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ElasticPanel
{
    private JFrame frame;
    private JPanel contentPane;
    private JPanel basePanel;
    /*
     * Array to hold the JComboBox
     * elements.
     */
    private JComboBox[] prodCombo;  
    private JComboBox[] temp;
    /* 
     * Counter to keep track
     * of the number of JComboBox 
     * present.
     */
    private int counter;  
    /*
     * Data for each JComboBox
     */
    private String[] data = {
                                "None",
                                "Sub Category"
                            };

    private ActionListener comboAction =
            new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            JComboBox cbox = (JComboBox) ae.getSource();
            String command = (String) ae.getActionCommand();
            int index = Integer.parseInt(command);
            String selection = (String) cbox.getSelectedItem();

            if (selection.equals("None"))
            {
                /*
                 * i = index + 1, because, we want to
                 * remove all JComboBox after this one.
                 */
                for (int i = (index + 1); i < prodCombo.length; i++)
                {
                    temp = new JComboBox[prodCombo.length];
                    for (int j = 0; j < prodCombo.length; j++)
                        temp[j] = prodCombo[j];
                    basePanel.remove(prodCombo[i]); 
                }
                prodCombo = new JComboBox[index + 1];
                for (int i = 0; i <= index; i++)
                {                   
                    prodCombo[i] = temp[i];
                }
                counter = prodCombo.length;
                System.out.println("Item Removed\nCounter : " + counter);
            }
            else if (selection.equals("Sub Category"))
            {
                counter++;
                temp = new JComboBox[counter];
                for (int i = 0; i < prodCombo.length; i++)
                {
                    temp[i] = prodCombo[i];
                }
                temp[counter - 1] = new JComboBox(data);
                temp[counter - 1].setActionCommand("" + (counter - 1));
                temp[counter - 1].addActionListener(this);
                prodCombo = new JComboBox[counter];
                for (int i = 0; i < counter; i++)
                    prodCombo[i] = temp[i];
                basePanel.add(prodCombo[counter - 1]);  
                System.out.println("Item Added\nCounter : " + counter);
            }

            //basePanel.revalidate();
            //basePanel.repaint();
            frame.pack();
        }
    };

    public ElasticPanel()
    {
        prodCombo = new JComboBox[1];
        counter = 1;
    }

    private void displayGUI()
    {
        frame = new JFrame("Elastic Panel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel();

        basePanel = new JPanel(new GridLayout(0, 1, 5, 5));

        prodCombo[counter - 1] = new JComboBox(data);
        prodCombo[counter - 1].setActionCommand("" + (counter - 1));
        prodCombo[counter - 1].addActionListener(comboAction);

        basePanel.add(prodCombo[counter - 1]);
        contentPane.add(basePanel);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ElasticPanel().displayGUI();
            }
        });
    }
}

* 最新更新:*

通过添加更多组件并将弹性面板放置在其他位置而非内容窗格的顶部,可以获得更多洞察力。

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;

public class VirtualViewGUI extends JFrame
{
    private JPanel rightPanel;
    private ElasticPanel elasticPanel;

    public VirtualViewGUI()
    {
        super("Virtual View");

        JMenuBar jmenuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");
        JMenu feel = new JMenu("Look & Feel");

        JMenu layOutMenu = new JMenu("ConfigureCells");
        JMenuItem add_files = new JMenuItem("Select Directory.."); 
        JMenuItem minCellSize = new JMenuItem("height 260 X  width 260"); 
        JMenuItem moderateCellSize = new JMenuItem("height 320 X  width 320"); 
        JMenuItem maxCellSize = new JMenuItem("height 360 X  width 360"); 
        JMenuItem exit = new JMenuItem("Exit");
        JMenuItem help = new JMenuItem("Help Content");

        fileMenu.add(add_files);
        fileMenu.add(exit);
        layOutMenu.add(minCellSize);
        layOutMenu.add(moderateCellSize);
        layOutMenu.add(maxCellSize);
        helpMenu.add(help);

        jmenuBar.add(fileMenu);
        jmenuBar.add(layOutMenu);
        jmenuBar.add(helpMenu);

        ImageIcon myImage = null;
        try
        {
            myImage = new ImageIcon(
                new URL("http://gagandeepbali.uk.to/" + 
                        "gaganisonline/images/swing/" + 
                        "stackoverflow/cow-cartoon.jpg"));
        }
        catch(MalformedURLException mue)    
        {
            mue.printStackTrace();
        }

        JLabel icon = new JLabel(myImage);
        icon.setIcon(myImage);
        setJMenuBar(jmenuBar); 

        rightPanel = new JPanel();
        elasticPanel = new ElasticPanel(this);
        rightPanel.add(elasticPanel);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));
        contentPane.add(icon, BorderLayout.CENTER);
        contentPane.add(rightPanel, BorderLayout.LINE_END);

        setContentPane(contentPane);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);    
        setVisible(true);
        System.out.println("File Separator is : " + System.getProperty("file.separator"));
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new VirtualViewGUI();
            }
        });
    }
}

class ElasticPanel extends JPanel
{
    private JFrame frame;
    private JPanel contentPane;
    /*
     * Array to hold the JComboBox
     * elements.
     */
    private JComboBox[] prodCombo;  
    private JComboBox[] temp;
    /* 
     * Counter to keep track
     * of the number of JComboBox 
     * present.
     */
    private int counter;  
    /*
     * Data for each JComboBox
     */
    private String[] data = {
                                "None",
                                "Sub Category"
                            };

    private ActionListener comboAction =
            new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            JComboBox cbox = (JComboBox) ae.getSource();
            String command = (String) ae.getActionCommand();
            int index = Integer.parseInt(command);
            String selection = (String) cbox.getSelectedItem();

            if (selection.equals("None"))
            {
                /*
                 * i = index + 1, because, we want to
                 * remove all JComboBox after this one.
                 */
                for (int i = (index + 1); i < prodCombo.length; i++)
                {
                    temp = new JComboBox[prodCombo.length];
                    for (int j = 0; j < prodCombo.length; j++)
                        temp[j] = prodCombo[j];
                    remove(prodCombo[i]);   
                }
                prodCombo = new JComboBox[index + 1];
                for (int i = 0; i <= index; i++)
                {                   
                    prodCombo[i] = temp[i];
                }
                counter = prodCombo.length;
                System.out.println("Item Removed\nCounter : " + counter);
            }
            else if (selection.equals("Sub Category"))
            {
                counter++;
                temp = new JComboBox[counter];
                for (int i = 0; i < prodCombo.length; i++)
                {
                    temp[i] = prodCombo[i];
                }
                temp[counter - 1] = new JComboBox(data);
                temp[counter - 1].setActionCommand("" + (counter - 1));
                temp[counter - 1].addActionListener(this);
                prodCombo = new JComboBox[counter];
                for (int i = 0; i < counter; i++)
                    prodCombo[i] = temp[i];
                add(prodCombo[counter - 1]);    
                System.out.println("Item Added\nCounter : " + counter);
            }

            //revalidate();
            //repaint();
            frame.pack();
        }
    };

    public ElasticPanel(JFrame frame)
    {
        this.frame = frame;
        prodCombo = new JComboBox[1];
        counter = 1;

        setLayout(new GridLayout(0, 1, 5, 5));

        prodCombo[counter - 1] = new JComboBox(data);
        prodCombo[counter - 1].setActionCommand("" + (counter - 1));
        prodCombo[counter - 1].addActionListener(comboAction);

        add(prodCombo[counter - 1]);        
    }
}

答案 1 :(得分:2)

如果我理解正确,您有2个不同的面板,并使用CardLayout在这2个面板之间切换。 CardLayout的问题在于它占用了最大面板的大小。

因此,对于CardLayout,您将无法缩小容器的大小,但是您可以通过将面板包裹在具有BorderLayout的另一个面板中来避免组合框被拉伸,并且将面板放在BorderLayout.NORTH