Полоса прокрутки исчезнет, ​​и панель станет больше - PullRequest
0 голосов
/ 28 мая 2019

Я проектирую окно с GridBagLayout.Эта основная панель представляет собой панель прокрутки и состоит из нескольких панелей GridBagLayout.На одной из панелей есть две панели прокрутки, содержащие текстовое поле.

Проблема заключается в том, что при записи в одно из этого текстового поля его размер одинаков и появляется полоса прокрутки, но когда я перестаю писатьполоса прокрутки исчезает, а текстовое поле становится больше, и мои соседние ячейки становятся больше.

В коде, который я публикую после, полосы прокрутки называются scrollPane_1 и scrollPane_2:

public class PruebaQ extends JPanel {
    public class SignalingMessagePane extends JPanel {

        // Components
        private JCheckBox singMessChckBx1;
        private JLabel familyLabel1;
        private JComboBox<ComboItem> familyCmbBx1;
        private JTextArea textTField1;


        public SignalingMessagePane() {
            super();
        }

        public void initialize() {
            this.setBorder(new LineBorder(Color.LIGHT_GRAY));

            GridBagLayout gbl_nst5Panel1 = new GridBagLayout();
            gbl_nst5Panel1.columnWidths = new int[] { 0, 0, 0, 0, 0, 0 };
            gbl_nst5Panel1.rowHeights = new int[] { 0, 0, 0 };
            gbl_nst5Panel1.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            gbl_nst5Panel1.rowWeights = new double[] { 0.0, 0.0, 0.0 };
            this.setLayout(gbl_nst5Panel1);

            singMessChckBx1 = new JCheckBox("");
            singMessChckBx1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    familyCmbBx1.setEnabled(singMessChckBx1.isSelected());
                    textTField1.setEnabled(singMessChckBx1.isSelected());
                }
            });
            GridBagConstraints gbc_singMessChckBx1 = new GridBagConstraints();
            gbc_singMessChckBx1.anchor = GridBagConstraints.NORTHWEST;
            gbc_singMessChckBx1.insets = new Insets(5, 5, 0, 0);
            gbc_singMessChckBx1.gridx = 0;
            gbc_singMessChckBx1.gridy = 0;
            this.add(singMessChckBx1, gbc_singMessChckBx1);

            familyLabel1 = new JLabel("Label");
            GridBagConstraints gbc_familyLabel1 = new GridBagConstraints();
            gbc_familyLabel1.insets = new Insets(0, 40, 0, 5);
            gbc_familyLabel1.anchor = GridBagConstraints.EAST;
            gbc_familyLabel1.gridx = 0;
            gbc_familyLabel1.gridy = 1;
            this.add(familyLabel1, gbc_familyLabel1);

            familyCmbBx1 = new JComboBox();
            GridBagConstraints gbc_familyCmbBx1 = new GridBagConstraints();
            gbc_familyCmbBx1.weightx = 0.6;
            gbc_familyCmbBx1.insets = new Insets(0, 0, 0, 5);
            gbc_familyCmbBx1.fill = GridBagConstraints.HORIZONTAL;
            gbc_familyCmbBx1.gridx = 1;
            gbc_familyCmbBx1.gridy = 1;
            this.add(familyCmbBx1, gbc_familyCmbBx1);
            familyCmbBx1.setEnabled(false);

            textTField1 = new JTextArea();
            GridBagConstraints gbc_textTField1 = new GridBagConstraints();
            gbc_textTField1.weighty = 1.0;
            gbc_textTField1.weightx = 1.0;
            gbc_textTField1.gridwidth = 3;
            gbc_textTField1.insets = new Insets(10, 5, 5, 0);
            gbc_textTField1.fill = GridBagConstraints.BOTH;
            gbc_textTField1.gridx = 0;
            gbc_textTField1.gridy = 2;
            this.add(textTField1, gbc_textTField1);
            textTField1.setEnabled(false);
        }
    }


    private JScrollPane scrollPane_2;
    private JTextArea txtrDetectionZone;
    private JScrollPane scrollPane_1;
    private JTextArea txtrRelevanceZone;
    private JPanel panel;
    private JPanel nestedPanel2;
    private JPanel nst2RefPositionPanel;
    private JScrollPane scrollPane;
    private JLabel latitudeLabel;
    private JTextField latitudeTxt;
    private JLabel longitudeLabel;
    private JTextField longitudeTxt;
    private JLabel zoomLabel;
    private JTextField zoomTxt;
    private JPanel nestedPanel3;
    private JLabel detectionZoneLabel;
    private JLabel relevanceZoneLabel;
    private JPanel nestedPanel5;
    private SignalingMessagePane[] nstPanel;

    /**
     * Create the panel.
     */
    public PruebaQ() {      


        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0};
        gridBagLayout.rowHeights = new int[]{0};
        gridBagLayout.columnWeights = new double[]{1.0};
        gridBagLayout.rowWeights = new double[]{1.0};
        setLayout(gridBagLayout);

        panel = new JPanel();
        scrollPane = new JScrollPane(panel);
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.weighty = 1.0;
        gbc_scrollPane.weightx = 1.0;
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 0;
        add(scrollPane, gbc_scrollPane);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0, 0};
        gbl_panel.rowHeights = new int[]{0, 0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0};
        gbl_panel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

                nestedPanel2 = new JPanel();
                GridBagConstraints gbc_nestedPanel2 = new GridBagConstraints();
                gbc_nestedPanel2.weighty = 1.0;
                gbc_nestedPanel2.weightx = 1.0;
                gbc_nestedPanel2.fill = GridBagConstraints.BOTH;
                gbc_nestedPanel2.insets = new Insets(20, 10, 10, 10);
                gbc_nestedPanel2.gridx = 1;
                gbc_nestedPanel2.gridy = 1;
                panel.add(nestedPanel2, gbc_nestedPanel2);
                GridBagLayout gbl_nestedPanel2 = new GridBagLayout();
                gbl_nestedPanel2.columnWidths = new int[]{0};
                gbl_nestedPanel2.rowHeights = new int[]{0, 0};
                gbl_nestedPanel2.columnWeights = new double[]{1.0};
                gbl_nestedPanel2.rowWeights = new double[]{0.0, 1.0};
                nestedPanel2.setLayout(gbl_nestedPanel2);

                nst2RefPositionPanel = new JPanel();
                nst2RefPositionPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Position", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
                GridBagConstraints gbc_nst2RefPositionPanel = new GridBagConstraints();
                gbc_nst2RefPositionPanel.weighty = 1.0;
                gbc_nst2RefPositionPanel.weightx = 1.0;
                gbc_nst2RefPositionPanel.insets = new Insets(0, 30, 0, 30);
                gbc_nst2RefPositionPanel.fill = GridBagConstraints.BOTH;
                gbc_nst2RefPositionPanel.gridx = 0;
                gbc_nst2RefPositionPanel.gridy = 1;
                nestedPanel2.add(nst2RefPositionPanel, gbc_nst2RefPositionPanel);
                GridBagLayout gbl_nst2RefPositionPanel = new GridBagLayout();
                gbl_nst2RefPositionPanel.columnWidths = new int[]{0, 0};
                gbl_nst2RefPositionPanel.rowHeights = new int[]{0, 0, 0};
                gbl_nst2RefPositionPanel.columnWeights = new double[]{0.0, 0.0};
                gbl_nst2RefPositionPanel.rowWeights = new double[]{0.0, 0.0, 0.0};
                nst2RefPositionPanel.setLayout(gbl_nst2RefPositionPanel);

                latitudeLabel = new JLabel("label");
                GridBagConstraints gbc_latitudeLabel = new GridBagConstraints();
                gbc_latitudeLabel.insets = new Insets(10, 20, 0, 10);
                gbc_latitudeLabel.anchor = GridBagConstraints.WEST;
                gbc_latitudeLabel.gridx = 0;
                gbc_latitudeLabel.gridy = 0;
                nst2RefPositionPanel.add(latitudeLabel, gbc_latitudeLabel);

                latitudeTxt = new JTextField();
                GridBagConstraints gbc_latitudeTxt = new GridBagConstraints();
                gbc_latitudeTxt.insets = new Insets(10, 0, 0, 20);
                gbc_latitudeTxt.fill = GridBagConstraints.HORIZONTAL;
                gbc_latitudeTxt.gridx = 1;
                gbc_latitudeTxt.gridy = 0;
                nst2RefPositionPanel.add(latitudeTxt, gbc_latitudeTxt);
                latitudeTxt.setColumns(10);

                longitudeLabel = new JLabel("label");
                GridBagConstraints gbc_longitudeLabel = new GridBagConstraints();
                gbc_longitudeLabel.anchor = GridBagConstraints.WEST;
                gbc_longitudeLabel.insets = new Insets(0, 20, 0, 10);
                gbc_longitudeLabel.gridx = 0;
                gbc_longitudeLabel.gridy = 1;
                nst2RefPositionPanel.add(longitudeLabel, gbc_longitudeLabel);

                longitudeTxt = new JTextField();
                GridBagConstraints gbc_longitudeTxt = new GridBagConstraints();
                gbc_longitudeTxt.insets = new Insets(0, 0, 0, 20);
                gbc_longitudeTxt.fill = GridBagConstraints.HORIZONTAL;
                gbc_longitudeTxt.gridx = 1;
                gbc_longitudeTxt.gridy = 1;
                nst2RefPositionPanel.add(longitudeTxt, gbc_longitudeTxt);
                longitudeTxt.setColumns(10);

                zoomLabel = new JLabel("label");
                GridBagConstraints gbc_zoomLabel = new GridBagConstraints();
                gbc_zoomLabel.anchor = GridBagConstraints.WEST;
                gbc_zoomLabel.insets = new Insets(0, 20, 15, 10);
                gbc_zoomLabel.gridx = 0;
                gbc_zoomLabel.gridy = 2;
                nst2RefPositionPanel.add(zoomLabel, gbc_zoomLabel);

                zoomTxt = new JTextField();
                GridBagConstraints gbc_zoomTxt = new GridBagConstraints();
                gbc_zoomTxt.insets = new Insets(0, 0, 15, 20);
                gbc_zoomTxt.fill = GridBagConstraints.HORIZONTAL;
                gbc_zoomTxt.gridx = 1;
                gbc_zoomTxt.gridy = 2;
                nst2RefPositionPanel.add(zoomTxt, gbc_zoomTxt);
                zoomTxt.setColumns(10);

                nestedPanel3 = new JPanel();
                GridBagConstraints gbc_nestedPanel3 = new GridBagConstraints();
                gbc_nestedPanel3.weighty = 1.0;
                gbc_nestedPanel3.weightx = 1.0;
                gbc_nestedPanel3.fill = GridBagConstraints.BOTH;
                gbc_nestedPanel3.insets = new Insets(20, 10, 15, 20);
                gbc_nestedPanel3.gridx = 2;
                gbc_nestedPanel3.gridy = 1;
                panel.add(nestedPanel3, gbc_nestedPanel3);
                GridBagLayout gbl_nestedPanel3 = new GridBagLayout();
                gbl_nestedPanel3.columnWidths = new int[]{0};
                gbl_nestedPanel3.rowHeights = new int[]{0, 0, 0, 0};
                gbl_nestedPanel3.columnWeights = new double[]{0.0};
                gbl_nestedPanel3.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0};
                nestedPanel3.setLayout(gbl_nestedPanel3);

                detectionZoneLabel = new JLabel("label");
                GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
                gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
                gbc_lblNewLabel.insets = new Insets(0, 0, 1, 0);
                gbc_lblNewLabel.gridx = 0;
                gbc_lblNewLabel.gridy = 0;
                nestedPanel3.add(detectionZoneLabel, gbc_lblNewLabel);

                scrollPane_2 = new JScrollPane();
                GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
                gbc_scrollPane_2.weighty = 1.0;
                gbc_scrollPane_2.weightx = 1.0;
                gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
                gbc_scrollPane_2.gridx = 0;
                gbc_scrollPane_2.gridy = 1;
                nestedPanel3.add(scrollPane_2, gbc_scrollPane_2);

                txtrDetectionZone = new JTextArea();
                scrollPane_2.setViewportView(txtrDetectionZone);

                relevanceZoneLabel = new JLabel("label");
                GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
                gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
                gbc_lblNewLabel_1.insets = new Insets(5, 0, 1, 0);
                gbc_lblNewLabel_1.gridx = 0;
                gbc_lblNewLabel_1.gridy = 2;
                nestedPanel3.add(relevanceZoneLabel, gbc_lblNewLabel_1);

                scrollPane_1 = new JScrollPane();
                GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
                gbc_scrollPane_1.weighty = 1.0;
                gbc_scrollPane_1.weightx = 1.0;
                gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
                gbc_scrollPane_1.gridx = 0;
                gbc_scrollPane_1.gridy = 3;
                nestedPanel3.add(scrollPane_1, gbc_scrollPane_1);

                txtrRelevanceZone = new JTextArea();
                scrollPane_1.setViewportView(txtrRelevanceZone);

                nestedPanel5 = new JPanel();
                nestedPanel5.setBorder(new TitledBorder(null, "Messages", TitledBorder.LEADING, TitledBorder.TOP, null, null));
                GridBagConstraints gbc_nestedPanel5 = new GridBagConstraints();
                gbc_nestedPanel5.weighty = 1.0;
                gbc_nestedPanel5.weightx = 1.0;
                gbc_nestedPanel5.fill = GridBagConstraints.BOTH;
                gbc_nestedPanel5.gridwidth = 2;
                gbc_nestedPanel5.insets = new Insets(10, 10, 10, 10);
                gbc_nestedPanel5.gridx = 1;
                gbc_nestedPanel5.gridy = 2;
                panel.add(nestedPanel5, gbc_nestedPanel5);
                GridBagLayout gbl_nestedPanel5 = new GridBagLayout();
                gbl_nestedPanel5.columnWidths = new int[]{0, 0};
                gbl_nestedPanel5.rowHeights = new int[]{0, 0};
                gbl_nestedPanel5.columnWeights = new double[]{0.0, 0.0};
                gbl_nestedPanel5.rowWeights = new double[]{0.0, 0.0};
                nestedPanel5.setLayout(gbl_nestedPanel5);

                for(int index = 0 ; index < 4; index++) {
                    nstPanel[index] = new SignalingMessagePane();
                    nstPanel[index].initialize();
                    GridBagConstraints gbc_nst5Panel1 = new GridBagConstraints();
                    gbc_nst5Panel1.weighty = 0.5;
                    gbc_nst5Panel1.weightx = 0.5;
                    gbc_nst5Panel1.insets = new Insets(5, 5, 5, 10);
                    gbc_nst5Panel1.fill = GridBagConstraints.BOTH;
                    gbc_nst5Panel1.gridx = index % 2;
                    gbc_nst5Panel1.gridy = index > 1? 1:0;
                    nestedPanel5.add(nstPanel[index], gbc_nst5Panel1);
                }
    }

}

Как мне избежать этого поведения или что я делаю не так?

1 Ответ

0 голосов
/ 01 июня 2019

Измените эту строку кода, который вы разместили:

txtrDetectionZone = new JTextArea();

Присвойте JTextArea начальный размер с помощью конструктора , который принимает количество строк и столбцов, например,

txtrDetectionZone = new JTextArea(10, 50);

Также вы можете установить политику полосы прокрутки так, чтобы полосы прокрутки всегда появлялись, например,

scrollPane_2 = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

Таким образом, полосы прокрутки не будут появляться и исчезать.

...