我有许多JComboBoxes,我用BoxLayout添加到面板中。所有作品都很好,大文本将被包裹在两三行中,这就是我使用html标签的原因。
我的问题是,当有太多的复选框时,我会添加一个滚动窗格。ScrollPane应该只使用"VERTICAL_SCROLLBAR_AS_NEEDED",但这将使格式从文本变为只有一行。
编辑:
问题是,如何在JScrollPane中添加面板,并在组合框文本中添加换行符。有了滚动窗格,它就不起作用了。
这里有一个简单的例子:
public static void main(String[] args) {
new MySwingTest();
}
private MySwingTest(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createCenterPanel(), BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
}
private Component createCenterPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
for(int i = 0 ; i < 10;i++){
panel.add(new JCheckBox("<html>das das das das das da d da fdfsdf dfsd fsdfsdf sdfsd fsdfsd fsdf fsd fss fs </html>"));
}
JScrollPane pane = new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
return pane;
}
如果我理解正确的话,你真正想要的是一个永远不会比包含它的JScrollPane更宽的面板。Swing有一个专门用于此目的的界面Scrollable:
class CheckboxPanel
extends JPanel
implements Scrollable {
private final int checkBoxHeight;
public CheckboxPanel() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
checkBoxHeight = new JCheckBox("Example").getPreferredSize().height;
}
@Override
public boolean getScrollableTracksViewportWidth() {
// This prevents a horizontal scrollbar from appearing.
return true;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction) {
if (orientation == SwingConstants.HORIZONTAL) {
return 1;
}
return Math.min(checkBoxHeight, direction < 0 ?
visibleRect.y : getHeight() - (visibleRect.y + visibleRect.height));
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction) {
if (orientation == SwingConstants.HORIZONTAL) {
return 10;
}
return Math.min(visibleRect.height, direction < 0 ?
visibleRect.y : getHeight() - (visibleRect.y + visibleRect.height));
}
}
我会添加一些addComponentListener
并为面板设置setPreferredSize
:
public class MySwingTest {
JScrollPane scrollPane;
JPanel panel;
public static void main(String[] args) {
new MySwingTest();
}
private MySwingTest(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createCenterPanel(), BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
}
private Component createCenterPanel() {
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
for(int i = 0 ; i < 10;i++){
panel.add(new JCheckBox("<html>das das das das das da d da fdfsdf dfsd fsdfsdf sdfsd fsdfsd fsdf fsd fss fs </html>"));
}
scrollPane = new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
scrollPane.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Rectangle rect = scrollPane.getViewportBorderBounds();
panel.setPreferredSize(new Dimension((int)rect.getWidth()-10, (int)panel.getPreferredSize().getHeight()+100)); // dummy offset
}
});
return scrollPane;
}
}
你想要什么并不理想,而是方向。
希望它能帮助