【问题标题】:JComboBox in a JScrollPaneJScrollPane 中的 JComboBox
【发布时间】:2013-12-17 05:12:23
【问题描述】:

我有许多 JComboBoxes,我将它们添加到具有 BoxLayout 的面板中。 所有作品都很好,大文本将被包裹在两三行中,这就是我使用 html 标签的原因。

我的问题是,当复选框太多时,我将添加一个 ScrollPane。 ScrollPane 应该只使用“VERTICAL_SCROLLBAR_​​AS_NEEDED”,但这会将格式从文本中断为仅一行。

编辑:
问题是,我如何在 JScrollPane 中添加面板,并在 Combobox 文本中添加换行符。使用滚动窗格不起作用。

这是一个简单的例子:

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 中添加面板,并在 Combobox 文本中添加换行符。使用滚动窗格它不起作用。只有一行,“其他”文字消失了。
  • 在 CheckBox 文本中间应用
  • 如果您发布图像应该是什么样子就可以了。
  • @Masud 你的意思是

标签: java swing layout jscrollpane jcombobox


【解决方案1】:

如果我理解正确的话,您真正想要的是一个永远不会比包含它的 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));
    }
}

【讨论】:

    【解决方案2】:

    我会添加一些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;
        }
    }
    

    这不是你想要的理想,而是方向。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2012-05-03
      • 1970-01-01
      • 2012-02-02
      • 2015-11-19
      • 2011-05-11
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多