所以我不知道它是否有用,或者我是否希望它以不同的方式工作,但无论如何。
我有一个org.openide.windows.TopComponent,它有三个包含不同项目的列表。可以通过单击其名称来隐藏此列表,以使高度可变,具体取决于哪些可见。如果列表总高度大于TopComponent高度,则应显示滚动条。如果宽度发生更改,则列表中的元素应重新排列,以便列表使用可用的整个宽度。所以我采用的方法是:
的TopComponent
在JScrollPane里面(给我一个我需要的滚动)。仅限垂直滚动。
在JPanel内部。
在三个列表(带有JList的JScrollPanes)中,使用各自的JLabel单击并隐藏内容。
因此,如果我遵循此模式,一切看起来都很好,当我更改宽度使其变大时,列表中的元素将重新排列以使用新宽度。但是当我尝试将它再次缩小到原始大小时,列表保持原样并且一些元素隐藏(没有足够的空间供所有人使用),就好像有一个滚动(水平)。但没有!但是如果我删除外部滚动条并且我确实改变了宽度,那么列表会很好地调整大小(当我使它更宽更窄时)但是我没有我需要的垂直滚动...
public class ViewPalettePanel extends TopComponent {
fcp = new PalettePanel();
scroll = new JScrollPane(fcp,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
add(scroll, gbc);
}
public class PalettePanel extends javax.swing.JPanel {
public PalettePanel() {
initComponents();
}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
lbl1 = new javax.swing.JLabel();
scPnl1 = new javax.swing.JScrollPane();
lbl2 = new javax.swing.JLabel();
scPnl2 = new javax.swing.JScrollPane();
lbl3 = new javax.swing.JLabel();
scPnl3 = new javax.swing.JScrollPane();
lblPadding = new javax.swing.JLabel();
setLayout(new java.awt.GridBagLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
add(lbl1, gridBagConstraints);
scPnl1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scPnl1.setMinimumSize(new java.awt.Dimension(60, 60));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
add(scPnl1, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
add(lbl2, gridBagConstraints);
scPnl2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scPnl2.setMinimumSize(new java.awt.Dimension(60, 60));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
add(scPnl2, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
add(lbl3, gridBagConstraints);
scPnl3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scPnl3.setMinimumSize(new java.awt.Dimension(60, 60));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 3);
add(scPnl3, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 6;
gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
gridBagConstraints.weighty = 1.0;
add(lblPadding, gridBagConstraints);
}
}
这是我正在使用(或多或少完整)视觉部分的代码。如果我更改添加(滚动,gbc)要添加(fcp,gbc)我想要调整大小但不是滚动。
有什么想法吗?
答案 0 :(得分:0)
我发现将它添加到JScrollPane中的JPanel它可以工作,但我不确定是否会有任何副作用......(但对我来说工作正常)
@Override
public java.awt.Dimension getPreferredSize() {
int h = super.getPreferredSize().height;
int w = getParent().getSize().width;
return new java.awt.Dimension(w, h);
}