1

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

我将调用该面板elasticPanel。顾名思义,它应该是有弹性的。这意味着,它应该能够扩展和收缩。假设它的行为是这样的。如果用户选择1,则elasticPanel应该显示一个,例如JComboBox。如果用户选择2然后,两个JComboBox...

好的,到目前为止它工作得很好。现在,当显示两个s时,用户1再次选择。我现在需要做的是应该显示一个正常大小的。但由于已经扩展,发生的情况是它显示拉伸以适应它的大小。所以它给人一种奇怪的感觉。elasticPanelJComboBoxelasticPanelJComboBoxelasticPanelJComboBox


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

在选择之前。NONE被选中。

在此处输入图像描述

选择了一个元素

在此处输入图像描述

NONE再次被选中

在此处输入图像描述

我需要最后一个屏幕截图中的elasticPanel故障位置)与第一个屏幕截图中的一样。这只是一个简单的案例。想象一下NONE显示大约 5、6 个子组件后返回时的样子。

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

任何帮助表示赞赏。谢谢!

4

2 回答 2

4

很难说你指CardLayout的是什么东西。由于CardLayout以不同的方式工作。您可以做的就是简单地放置一个JPanelbasePanel 并将GridLayout(0, 1)其放在JPanel另一个JPanelcontentPanelJFrame的顶部,然后在您从视图中添加或删除元素时将其设置为并调用 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]);        
    }
}
于 2012-09-30T14:43:58.127 回答
2

如果我理解正确,您有 2 个不同的面板,您可以使用CardLayout. 的问题CardLayout是它占用了最大面板的大小。

因此,CardLayout您将无法缩小容器的大小,但您可以避免组合框被拉伸,方法是使用 a 将面板包裹在另一个面板中BorderLayout,并将面板放入BorderLayout.NORTH

于 2012-09-30T09:07:37.643 回答