滚动面板上的面板不允许滚动按钮。

huangapple go评论75阅读模式
英文:

Scroll pane on panel doesn't allow scrolling buttons

问题

我有一个JPanel,它可能包含很少或很多按钮,如果按钮多于可显示的数量,我需要能够通过垂直滚动查看这些按钮。当我添加一个JScrollPane时,即使按钮明显重叠JPanel的可用空间,也无法滚动。

滚动窗格的创建方式:

scrollPane = new ScrollPane(this);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBorder(BorderFactory.createLineBorder(Color.black, 2));

按钮的添加方式:

for (int i = 0; i < 20; ++i)
{
    panel.add(new TestButton("example." + i, "example"));
}

该面板添加到另一个使用FlowLayout的面板上。如果重要的话,这些按钮是自定义绘制的按钮(它们扩展了JButton),可以更改自己的大小(使用setPreferredSize)并执行自己的绘制。

英文:

I have a JPanel that will contain either very few or a lot of buttons and if there are more buttons than can be displayed, I need to be able to scroll through the vertically displayed buttons. When I add a JScrollPane it doesn't let you scroll even when the buttons clearly overlap the available space of the JPanel.

How the scroll pane is created:

scrollPane = new ScrollPane(this);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBorder(BorderFactory.createLineBorder(Color.black, 2));

How the buttons are added:

for (int i = 0; i &lt; 20; ++i)
{
    panel.add(new TestButton(&quot;example.&quot; + i, &quot;example&quot;));
}

The panel is added onto another panel that is using FlowLayout. If it matters, the buttons are custom drawn buttons (they extend JButton) that change their own size (using setPreferredSize) and do their own drawing.

答案1

得分: 1

流布局最初从左到右添加组件。由于您禁用了水平滚动条,按钮变得不可见。我的建议是使用框布局将按钮放置在从上到下的布局中。

panel.setLayout(new javax.swing.BoxLayout(panel, javax.swing.BoxLayout.Y_AXIS));
英文:

Flow layout adds the components from left to right initially. As you disabled horizontal scrollbar, buttons are getting invisible. My suggestion is to have a box layout to place the buttons to be laid out top to bottom.

panel.setLayout(new javax.swing.BoxLayout(panel, javax.swing.BoxLayout.Y_AXIS));

huangapple
  • 本文由 发表于 2020年7月30日 13:20:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63166645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定