Java 滚动条消失,面板变大
我正在设计一个带有Java 滚动条消失,面板变大,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我正在设计一个带有GridBagLayout的窗口。此主面板是一个滚动面板,由多个面板组成,所有面板均GridBagLayout。其中一个面板中有两个滚动面板,其中包含一个文本字段 问题是,当在其中一个文本字段中写入时,其大小相同,并出现滚动条,但当我停止写入时,滚动条消失,文本字段变大,相邻单元格也变大 在我发布之后的代码中,滚动窗格被称为scrollPane_1和scrollPane_2: public class PruebaQ extends JPanel { public cl
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);
}
}
}
公共类PruebaQ扩展了JPanel{
公共类SignalingMessagePane扩展了JPanel{
//组成部分
私有JCheckBox singMessChckBx1;
私人JLabel familyLabel1;
私人JComboBox家庭CMBBX1;
私人JTextArea textTField1;
公共信号消息窗格(){
超级();
}
公共无效初始化(){
此.setboorder(新线条边框(颜色.浅灰色));
GridBagLayout gbl_nst5Panel1=新的GridBagLayout();
gbl_nst5Panel1.columnWidths=newint[]{0,0,0,0,0};
gbl_nst5Panel1.rowHeights=newint[]{0,0,0};
gbl_nst5Panel1.columnWeights=新的双[]{0.0,0.0,0.0,0.0,0.0};
gbl_nst5Panel1.rowWeights=新的双[]{0.0,0.0,0.0};
此.setLayout(gbl_nst5Panel1);
singMessChckBx1=新的JCheckBox(“”);
singMessChckBx1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
familyCmbBx1.setEnabled(singMessChckBx1.isSelected());
textTField1.setEnabled(singMessChckBx1.isSelected());
}
});
GridBagConstraints gbc_singMessChckBx1=新的GridBagConstraints();
gbc_singMessChckBx1.anchor=GridBagConstraints.NORTHWEST;
gbc_singMessChckBx1.insets=新的insets(5,5,0,0);
gbc_singMessChckBx1.gridx=0;
gbc_singMessChckBx1.gridy=0;
添加(singMessChckBx1,gbc_singMessChckBx1);
FamilyLabel 1=新的JLabel(“标签”);
GridBagConstraints gbc_familyLabel1=新的GridBagConstraints();
gbc_familyLabel1.插图=新插图(0,40,0,5);
gbc_familyLabel1.anchor=GridBagConstraints.EAST;
gbc_familyLabel1.gridx=0;
gbc_familyLabel1.gridy=1;
添加(家庭标签1,gbc_家庭标签1);
familyCmbBx1=新JComboBox();
GridBagConstraints gbc_familyCmbBx1=新的GridBagConstraints();
gbc_家族MBBx1.weightx=0.6;
gbc_familyCmbBx1.insets=新的insets(0,0,0,5);
gbc_familyCmbBx1.fill=GridBagConstraints.HORIZONTAL;
gbc_familyCmbBx1.gridx=1;
gbc_familyCmbBx1.gridy=1;
添加(家族式CMBBX1,gbc_家族式CMBBX1);
familyCmbBx1.setEnabled(假);
textTField1=新的JTextArea();
GridBagConstraints gbc_textTField1=新的GridBagConstraints();
gbc_textTField1.weighty=1.0;
gbc_textTField1.weightx=1.0;
gbc_textTField1.gridwidth=3;
gbc_textTField1.insets=新的insets(10,5,5,0);
gbc_textTField1.fill=GridBagConstraints.BOTH;
gbc_textTField1.gridx=0;
gbc_textTField1.gridy=2;
添加(文本字段1、gbc_文本字段1);
textTField1.setEnabled(false);
}
}
私有JScrollPane滚动窗格2;
私人JTextArea txtrDetectionZone;
私有JScrollPane滚动窗格_1;
私人住宅区;
私人JPanel小组;
私人JPanel nestedPanel2;
私人JPanel NST2参考位置面板;
私有JScrollPane滚动窗格;
私人JLabel-latitudeLabel;
私有JTextField-latitudeTxt;
私人JLabel longitudeLabel;
私有JTextField longitudeText;
私人JLabel zoomLabel;
私有JTextField zoomTxt;
私人JPanel nestedPanel3;
专用JLabel检测Zonelabel;
私人JLabel relevanceZoneLabel;
私人JPanel nestedPanel5;
专用信号消息窗格[]显示面板;
/**
*创建面板。
*/
公共PruebaQ(){
GridBagLayout GridBagLayout=新的GridBagLayout();
gridBagLayout.columnWidths=新的int[]{0};
gridBagLayout.rowHeights=new int[]{0};
gridBagLayout.columnWeights=新的双精度[]{1.0};
gridBagLayout.rowWeights=新的双精度[]{1.0};
setLayout(gridBagLayout);
panel=新的JPanel();
scrollPane=新的JScrollPane(面板);
GridBagConstraints gbc_scrollPane=新的GridBagConstraints();
gbc_scrollPane.weighty=1.0;
gbc_scrollPane.weightx=1.0;
gbc_scrollPane.fill=GridBagConstraints.BOTH;
gbc_scrollPane.gridx=0;
gbc_scrollPane.gridy=0;
添加(滚动窗格、gbc_滚动窗格);
GridBagLayout gbl_面板=新建GridBagLayout();
gbl_panel.columnWidths=新的int[]{0,0,0};
gbl_panel.rowHeights=newint[]{0,0,0};
gbl_panel.columnWeights=新的双[]{0.0,0.0,0.0};
gbl_panel.rowWeights=新的double[]{0.0,0.0,double.MIN_值};
面板。设置布局(gbl_面板);
nestedPanel2=新的JPanel();
GridBagConstraints gbc_nestedPanel2=新的GridBagConstraints();
gbc_嵌套面板2.weighty=1.0;
gbc_嵌套面板2.weights x=1.0;
gbc_nestedPanel2.fill=GridBagConstraints.BOTH;
gbc_嵌套面板2.插图=新插图(20,10,10,10);
gbc_嵌套面板2.gridx=1;
gbc_嵌套面板2.gridy=1;
面板。添加(嵌套面板2,gbc_嵌套面板2);
GridBagLayout gbl_nestedPanel2=新的GridBagLayout()
txtrDetectionZone = new JTextArea();
txtrDetectionZone = new JTextArea(10, 50);
scrollPane_2 = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);