JScrollpane//Java Swing 中的自定义滚动条



我制作了自己的ScrollbarUI,它与JScrollbar完美配合。问题是我必须将此滚动条与 JPanel 连接才能滚动它。我已经尝试使用JScrollPane,但是当我尝试接近垂直滚动条时,它就是没有响应。我尝试更改一些简单的东西,例如光标,宽度,高度,背景,以测试UI是否是问题所在,但这也没有奏效。

    JPanel panel_1 = new JPanel();
    JScrollPane scrollpane = new JScrollPane(panel_1);
    scrollpane.setBorder(null);
    scrollpane.getVerticalScrollBar().setUI(new MyScrollbarUI());
    JTextArea txtrLorem = new JTextArea();
    txtrLorem.setPreferredSize(new Dimension(400, 1500));
    txtrLorem.setMaximumSize(new Dimension(400, 2147483647));
    txtrLorem.setText("Lorem ipsum...");
    panel_1.add(txtrLorem);
    scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    GridBagConstraints gbc_panel_1 = new GridBagConstraints();
    gbc_panel_1.gridwidth = 5;
    gbc_panel_1.insets = new Insets(0, 0, 0, 5);
    gbc_panel_1.fill = GridBagConstraints.BOTH;
    gbc_panel_1.gridx = 1;
    gbc_panel_1.gridy = 4;
    add(scrollpane, gbc_panel_1);
    JScrollBar scrollBar = new JScrollBar();
    scrollBar.setPreferredSize(new Dimension(20, 96));
    scrollBar.setMinimumSize(new Dimension(20, 5));
    scrollBar.setMaximumSize(new Dimension(20, 32767));
    scrollBar.setUI(new MyScrollbarUI());
    GridBagConstraints gbc_scrollBar = new GridBagConstraints();
    gbc_scrollBar.fill = GridBagConstraints.VERTICAL;
    gbc_scrollBar.insets = new Insets(0, 0, 0, 5);
    gbc_scrollBar.gridx = 6;
    gbc_scrollBar.gridy = 4;
    add(scrollBar, gbc_scrollBar);

JScrollBar具有正确的设计,但JScrollPane的滚动条则没有。下面是用户界面:

公共类 MyScrollbarUI 扩展了 BasicScrollBarUI {

    @Override
    protected JButton createDecreaseButton(int orientation) {
        JButton btnL = new JButton("");
        btnL.setPressedIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/top_active.png")));
        btnL.setBorderPainted(false);
        btnL.setIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/top_on.png")));
        btnL.setRolloverIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/top.png")));
        btnL.setRolloverEnabled(true);
        return btnL;
    }
    @Override
    protected JButton createIncreaseButton(int orientation) {
        JButton btnL = new JButton("");
        btnL.setPressedIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/down_active.png")));
        btnL.setBorderPainted(false);
        btnL.setIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/down_on.png")));
        btnL.setRolloverIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/down.png")));
        btnL.setRolloverEnabled(true);
        return btnL;
    }
    @Override
    protected void paintDecreaseHighlight(Graphics g)
    {
    Insets insets = scrollbar.getInsets();
    Rectangle thumbR = getThumbBounds();
    g.setColor(new Color(137,144,144));
    if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
        int x = insets.left+decrButton.getWidth()/2-2;
        int y = decrButton.getY() + decrButton.getHeight();
        int w = 4;
        int h = thumbR.y - y;
        g.fillRect(x, y, w, h);
    } 
    else    {
        int x, w;
        if (scrollbar.getComponentOrientation().isLeftToRight()) {
        x = decrButton.getX() + decrButton.getWidth();
        w = thumbR.x - x;
        } else {
        x = thumbR.x + thumbR.width;
        w = decrButton.getX() - x;
        }
        int y = insets.top;
        int h = scrollbar.getHeight() - (insets.top + insets.bottom);
        g.fillRect(x, y, w, h);
    }
    }
    @Override
    protected void paintIncreaseHighlight(Graphics g) {
            Insets insets = scrollbar.getInsets();
            Rectangle thumbR = getThumbBounds();
            g.setColor(new Color(202,207,203));
            if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
                int x = insets.left+decrButton.getWidth()/2-2;
                int y = thumbR.y;
                int w = 4;
                int h = incrButton.getY() - y;
                g.fillRect(x, y, w, h);
            }
        }

    @Override
    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)  
    {
        g.setColor(Color.WHITE);
        g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
        paintDecreaseHighlight(g);
        paintIncreaseHighlight(g);
    }
  @Override
  protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
  {
      if(thumbBounds.isEmpty() || !scrollbar.isEnabled())     {
          return;
      }
      int w = 16;
      int h = 16;
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
          RenderingHints.VALUE_ANTIALIAS_ON);
      g.translate(thumbBounds.x, thumbBounds.y);
      GradientPaint gr = new GradientPaint(2, 0, new Color(158,161,162), 18, 16, new Color(96,99,98));
      g2.setPaint(gr);
      g2.fill(new Ellipse2D.Double(2, 0, w-1, h-1));
      g2.setPaint(new Color(203,207,203));
      g2.fill(new Ellipse2D.Double(6, 4, 7, 7));
      g.translate(-thumbBounds.x, -thumbBounds.y);
  }


} 

试试这个:

JScrollPane table = new JScrollPane(content){ @SuppressWarnings("static-access") @Override public JScrollBar createVerticalScrollBar() { return (new Scroler()).makeUI(); } };

为我工作

相关内容

  • 没有找到相关文章