代码之家  ›  专栏  ›  技术社区  ›  Zeliax

如何在启动程序时将包含JPanel的JScrollPane滚动到顶部?

  •  2
  • Zeliax  · 技术社区  · 10 年前

    我有一个JScrollPane的问题,当程序启动时,我无法从顶部开始。我希望这里的某个巫师能给出答案:P

    下面是一个工作代码示例:

    public class JScrollPaneExample extends JPanel{
    
    public JScrollPaneExample(int rowCount){
    
        setLayout(new BorderLayout());
    
        JScrollPane scrollPane = makeScrollPane(rowCount);
        add(scrollPane, BorderLayout.PAGE_START);
        setPreferredSize(new Dimension(300,300));
    }
    
    private JScrollPane makeScrollPane(int rowCount) {
        JScrollPane scrollPane = new JScrollPane();
    
        JPanel pane = new JPanel();
        pane.setLayout(new GridLayout(rowCount, 0));
    
        for(int i = 0; i < rowCount; i++){
            if(i != 4)
                pane.add(makeTestField("Test " + (i+1), "Test description: " + (i+1), i));
            else {
                pane.add(makeTestField("This is a test", 
                                           "This is also a test to see if this field will do a line break. "
                                           + "This is also a test to see if this field will do a line break. "
                                           + "Something else could be entirely different...", 
                                           i));
            }
        }
    
        scrollPane.getViewport().add(pane);
        scrollPane.setPreferredSize(new Dimension(200, 400));   
        return scrollPane;
    }
    
    protected static JPanel makeTestField(String header, String description, int rowCount){ 
        JPanel testField = new JPanel();
    
        testField.setLayout(new BorderLayout());
        testField.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    
        JLabel label = new JLabel(header);
        Font font = new Font("Serif", Font.BOLD, 18);
        label.setFont(font);
    
        JButton btn = new JButton("Start");
    
        JTextArea textArea = new JTextArea(description);
        textArea.setEditable(false);
        textArea.setBackground(label.getBackground());
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
    
        testField.add(label, BorderLayout.PAGE_START);
        testField.add(textArea, BorderLayout.CENTER);
        testField.add(btn, BorderLayout.LINE_END);
    
        return testField;
    }
    
    public static void main(String[] args) {
    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);
    
                JFrame frame = new JFrame();
                JPanel scrollPane = new JScrollPaneExample(8);
                frame.add(scrollPane);
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setPreferredSize(new Dimension(500,500));
                frame.pack();
            }
        });
    }
    }
    

    我尝试过退出在浏览interwebz时发现的许多不同的方法,例如设置scrollPanel的视口和许多其他东西,但都没有成功。

    2 回复  |  直到 10 年前
        1
  •  4
  •   Sergiy Medvynskyy    10 年前

    为此,您需要设置垂直滚动条的值。这是代码。

    public class JScrollPaneExample extends JPanel{
    
        public JScrollPaneExample(int rowCount){
    
            setLayout(new BorderLayout());
    
            final JScrollPane scrollPane = makeScrollPane(rowCount);
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    scrollPane.getVerticalScrollBar().setValue(0);
    
                }
            });
            add(scrollPane, BorderLayout.PAGE_START);
            setPreferredSize(new Dimension(300,300));
        }
    
        private JScrollPane makeScrollPane(int rowCount) {
            final JScrollPane scrollPane = new JScrollPane();
    
            final JPanel pane = new JPanel();
            pane.setLayout(new GridLayout(rowCount, 0));
    
            for(int i = 0; i < rowCount; i++){
                if(i != 4)
                    pane.add(makeTestField("Test " + (i+1), "Test description: " + (i+1), i));
                else {
                    pane.add(makeTestField("This is a test", 
                            "This is also a test to see if this field will do a line break. "
                                    + "This is also a test to see if this field will do a line break. "
                                    + "Something else could be entirely different...", 
                                    i));
                }
            }
    
            scrollPane.getViewport().add(pane);
            scrollPane.setPreferredSize(new Dimension(200, 400));   
            return scrollPane;
        }
    
        protected static JPanel makeTestField(String header, String description, int rowCount){ 
            final JPanel testField = new JPanel();
    
            testField.setLayout(new BorderLayout());
            testField.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    
            final JLabel label = new JLabel(header);
            final Font font = new Font("Serif", Font.BOLD, 18);
            label.setFont(font);
    
            final JButton btn = new JButton("Start");
    
            final JTextArea textArea = new JTextArea(description);
            textArea.setEditable(false);
            textArea.setBackground(label.getBackground());
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            textArea.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
    
            testField.add(label, BorderLayout.PAGE_START);
            testField.add(textArea, BorderLayout.CENTER);
            testField.add(btn, BorderLayout.LINE_END);
    
            return testField;
        }
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    // Turn off metal's use of bold fonts
                    UIManager.put("swing.boldMetal", Boolean.FALSE);
    
                    final JFrame frame = new JFrame();
                    final JPanel scrollPane = new JScrollPaneExample(8);
                    frame.add(scrollPane);
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setPreferredSize(new Dimension(500,500));
                    frame.pack();
                }
            });
        }
    }
    
        2
  •  1
  •   MadProgrammer    10 年前

    另一个解决方案可能是使用 JComponent#scrollRectToVisible 并通过它 0, 0, 1, 1 (或类似的东西)

    您需要在添加到 JScrollPane