簡體   English   中英

調整JList的大小問題?

[英]resize problem with JList?

我正在調整JList的大小...下面是我的代碼...我的要求是將一個固定大小的JList添加到Panel中,當我最大化/最小化Frame時,JList應該相應地增加/減少。請幫助我..如何實現?

以下是設計:

嗨,卡爾,它在簡單的情況下有效,但在上述情況下不起作用。 所以我現在解釋上面的代碼..

我有一個名為“ TestListControl”的GB布局框架。 添加具有GB布局的selectDevicesPanel。 在selectDevicesPanel中:添加具有GB布局的selectNamesInnerPanel在selectNamesInnerPanel中:添加具有GB布局的selectNameAddPanel添加具有流布局的selectTeamPanel在selectNameAddPanel:selectDeviceScrollPane clickToAddDeletePanel selectedNameScrollPane在selectTeamPanel中:selectTeamLabel selectTeamComboBox

public class TestListControl extends javax.swing.JFrame {

    public TestListControl() {
        initComponents();
    }

    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        selectDevicesPanel = new javax.swing.JPanel();
        selectNamesInnerPanel = new javax.swing.JPanel();
        selectNameAddPanel = new javax.swing.JPanel();
        chooseDeviceScrollPane = new javax.swing.JScrollPane();
        chooseNamesList = new javax.swing.JList();
        clickToAddDeletePanel = new javax.swing.JPanel();
        clickToAddButton = new javax.swing.JButton();
        clickToRemoveButton = new javax.swing.JButton();
        selectedNameScrollPane = new javax.swing.JScrollPane();
        selectedNamesList = new javax.swing.JList();
        selectNameLabel = new javax.swing.JLabel();
        selectTeamPanel = new javax.swing.JPanel();
        selectTeamLabel = new javax.swing.JLabel();
        selectTeamComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        selectDevicesPanel.setBorder(javax.swing.BorderFactory
                .createTitledBorder("Member Selection"));
        selectDevicesPanel.setLayout(new java.awt.GridBagLayout());

        selectNamesInnerPanel.setLayout(new java.awt.GridBagLayout());

        selectNameAddPanel.setLayout(new java.awt.GridBagLayout());

        chooseNamesList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "XXXX", "YYYY", "ZZZZ" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        chooseNamesList.setMaximumSize(new java.awt.Dimension(46, 88));
        chooseNamesList.setMinimumSize(new java.awt.Dimension(94, 98));
        chooseDeviceScrollPane.setViewportView(chooseNamesList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 7, 0);
        selectNameAddPanel.add(chooseDeviceScrollPane, gridBagConstraints);

        clickToAddDeletePanel.setLayout(new java.awt.GridBagLayout());

        clickToAddButton.setText(">>");
        clickToAddDeletePanel.add(clickToAddButton,
                new java.awt.GridBagConstraints());

        clickToRemoveButton.setText("<<");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        clickToAddDeletePanel.add(clickToRemoveButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        selectNameAddPanel.add(clickToAddDeletePanel, gridBagConstraints);

        selectedNamesList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "XXXX", "YYYY" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        selectedNamesList.setMinimumSize(new java.awt.Dimension(94, 98));
        selectedNameScrollPane.setViewportView(selectedNamesList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
        selectNameAddPanel.add(selectedNameScrollPane, gridBagConstraints);

        selectNameLabel.setText("Select Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 8, 0);
        selectNameAddPanel.add(selectNameLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        selectNamesInnerPanel.add(selectNameAddPanel, gridBagConstraints);

        selectTeamLabel.setText("Select Team");
        selectTeamPanel.add(selectTeamLabel);

        selectTeamComboBox.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "TEAM X", "TEAM Y", "TEAM Z" }));
        selectTeamComboBox
                .addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        selectTeamComboBoxActionPerformed(evt);
                    }
                });
        selectTeamPanel.add(selectTeamComboBox);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        selectNamesInnerPanel.add(selectTeamPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        selectDevicesPanel.add(selectNamesInnerPanel, gridBagConstraints);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(
                org.jdesktop.layout.GroupLayout.LEADING).add(0, 400,
                Short.MAX_VALUE).add(
                layout.createParallelGroup(
                        org.jdesktop.layout.GroupLayout.LEADING).add(
                        layout.createSequentialGroup().add(0, 107,
                                Short.MAX_VALUE).add(selectDevicesPanel,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                185,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(0, 108, Short.MAX_VALUE))));
        layout.setVerticalGroup(layout.createParallelGroup(
                org.jdesktop.layout.GroupLayout.LEADING).add(0, 300,
                Short.MAX_VALUE).add(
                layout.createParallelGroup(
                        org.jdesktop.layout.GroupLayout.LEADING).add(
                        layout.createSequentialGroup().add(0, 39,
                                Short.MAX_VALUE).add(selectDevicesPanel,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                221,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(0, 40, Short.MAX_VALUE))));

        pack();
    }

    private void selectTeamComboBoxActionPerformed(
            java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestListControl().setVisible(true);
            }
        });
    }

    private javax.swing.JScrollPane chooseDeviceScrollPane;
    private javax.swing.JList chooseNamesList;
    private javax.swing.JButton clickToAddButton;
    private javax.swing.JPanel clickToAddDeletePanel;
    private javax.swing.JButton clickToRemoveButton;
    private javax.swing.JPanel selectDevicesPanel;
    private javax.swing.JPanel selectNameAddPanel;
    private javax.swing.JLabel selectNameLabel;
    private javax.swing.JPanel selectNamesInnerPanel;
    private javax.swing.JComboBox selectTeamComboBox;
    private javax.swing.JLabel selectTeamLabel;
    private javax.swing.JPanel selectTeamPanel;
    private javax.swing.JScrollPane selectedNameScrollPane;
    private javax.swing.JList selectedNamesList;

}

如果您這樣做,您的列表將不會變得更大:

chooseNamesList.setMaximumSize(new java.awt.Dimension(46, 88));

如果設置原型單元格值,則更改數據時JList不會調整大小。

有關setPrototypeCellValue(Object)方法的信息,請參見JList Javadoc頁面

伙計們,我自己解決了這個問題..謝謝您的協調..實際上我犯了一個錯誤,即給擁有Jlist和JEditor窗格的父面板賦予weightx和weighty……簡而言之,我們需要設置包含要調整大小的任何組件的所有父容器的重量參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM