我正在使用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);
}
}
}
如何避免这种行为,或者我做错了什么?
答案 0 :(得分:0)
更改您发布的代码的这一行:
txtrDetectionZone = new JTextArea();
通过使用constructor来赋予JTextArea
初始大小,该Regex demo采用行数和列数,例如
txtrDetectionZone = new JTextArea(10, 50);
您还可以设置滚动条策略,以便始终显示滚动条,例如
scrollPane_2 = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
那样,滚动条将不会出现或消失。