有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中JScrollBar的swing问题

我对JScrollBar有个问题

在我的应用程序中,我有两个面板(一个固定面板,另一个根据用户在菜单中的选择而变化)

在给定的面板中,我有一个带表的JScrollPane。由于标准java滚动条不属于应用程序的主题,我决定尝试创建另一个滚动条

我基本上创建了一个JScrollPane,并将其滚动条更改为我创建的自定义滚动条(scrollPane.setVerticalScrollBar(newCustomScrollbar())

我第一次展示面板时,滚动条非常完美。但是,当我切换到另一个面板并返回到它时,滚动条模型已重置

这可能是什么/我如何解决

创建JScrollPane代码:

JScrollPane tablePanel = new JScrollPane(table);
tablePanel.setBounds(10, 148, 495, 200);
tablePanel.setBorder(new EmptyBorder(0, 0, 0, 0));
tablePanel.setBackground(Color.WHITE);
tablePanel.setVerticalScrollBar(new CustomScrollBar());
add(tablePanel);

自定义条形码:

public class CustomScrollBar extends JScrollBar {

    public CustomScrollBar() {
        setOpaque(false);
        
        setUI(new BasicScrollBarUI() {
            private final Dimension d = new Dimension();

            @Override
            protected JButton createDecreaseButton(int orientation) {
                return new JButton() {
                    @Override
                    public Dimension getPreferredSize() {
                        return d;
                    }
                };
            }

            @Override
            protected JButton createIncreaseButton(int orientation) {
                return new JButton() {
                    @Override
                    public Dimension getPreferredSize() {
                        return d;
                    }
                };
            }

            @Override
            protected void paintTrack(Graphics g, JComponent c, Rectangle r) {
            }

            @Override
            protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Color color = null;
                JScrollBar sb = (JScrollBar) c;
                if (!sb.isEnabled() || r.width > r.height) {
                    return;
                } else if (isDragging) {
                    color = Colors.superdarkPurple;
                } else if (isThumbRollover()) {
                    color = Colors.lightPurple;
                } else {
                    color = Colors.darkPurple;
                }
                g2.setPaint(color);
                g2.fillRoundRect(r.x, r.y, r.width, r.height, 10, 10);
                g2.setPaint(Color.WHITE);
                g2.drawRoundRect(r.x, r.y, r.width, r.height, 10, 10);
                g2.dispose();
            }

            @Override
            protected void setThumbBounds(int x, int y, int width, int height) {
                super.setThumbBounds(x, y, width, height);
                scrollbar.repaint();
            }
        });
    }
    
    public JScrollBar geCustomScrollBar() {
        return this;
    }

编辑1

这将转到一个面板以添加问题

public void gotoAddQuestionsPanel(Question q) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                currentQuestionsPanel = "add";
                updateFrameComponentTreeUI();
                remove(subPanel);
                if (q != null) {
                    addQuestionsPanel.setQuestion(q);
                }
                subPanel = addQuestionsPanel.getPanel();
                subPanel.setBackground(Color.WHITE);
                subPanel.setBounds(304, 0, 515, 415);
                getContentPane().add(subPanel);
                subPanel.setLayout(null);
            }
        });
    }

这可以追溯到定制的滚动条

public void gotoQueryQuestionsPanel() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                currentQuestionsPanel = "query";
                updateFrameComponentTreeUI();
                remove(subPanel);
                subPanel = queryQuestionsPanel.getPanel();
                subPanel.setBackground(Color.WHITE);
                subPanel.setBounds(304, 0, 515, 415);
                getContentPane().add(subPanel);
                subPanel.setLayout(null);
            }
        });
    }

public void updateFrameComponentTreeUI() {
    SwingUtilities.updateComponentTreeUI(this);
}

共 (0) 个答案