如何获取滚动条中栏的当前位置?

How do I get the current location of bar in scroll bar?

概览:

考虑下图:

有垂直滚动条和水平滚动条。单击 OK 后,框架将消失。

在其他一些工作之后,我想让用户保持与现在相同的位置,这可能在具有相同宽度和高度的不同图像中。所以,我想跟踪滚动条中的条。

问题:

代码:

MultipleProcess.javaclass,其实就是开框

// imports and package description were here

public class MultipleProcess extends javax.swing.JDialog {
    SinglePaperEvaluation upperClass;
    BatchPaperEvaluation upperClass2;
    File imageFilePath;
    BufferedImage image;

    public MultipleProcess(boolean modal,File imageFile,SinglePaperEvaluation upper, BatchPaperEvaluation upper2) {
        super(upper==null?upper2:upper,"Co-ordinate Detector", modal);
        initComponents();
        upperClass = upper;
        upperClass2 = upper2;
        imageFilePath = imageFile;

        //adjust screen and windows listener were here

        loadImage();//custom function, it loads image in the jScrollPaneImageScreen

        if(upperClass2!=null){
            upperClass2.setVisible(false);

            //scrollbars states loading part
            jScrollPaneImageScreen.getVerticalScrollBar().setValue(upperClass2.verticalBar);
            jScrollPaneImageScreen.getHorizontalScrollBar().setValue(upperClass2.horizontalBar);
//            if(upperClass2.react!=null)jScrollPaneImageScreen.scrollRectToVisible(upperClass2.react);
        }

        setVisible(true);
    }

    // you can skip this function but have a look if you eager to see how am I loaded image in the pane
     private void loadImage() {
        try {
            image = ImageIO.read(imageFilePath);
            ImageIcon ii = new ImageIcon(imageFilePath.toString());
            JLabel jLabel = new JLabel(ii);
            jLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {

                    int x = e.getX();
                    int y = e.getY();

                    jTextFieldX.setText(String.format("%d",x));
                    jTextFieldY.setText(String.format("%d",y));

                }
            });
            jScrollPaneImageScreen.setViewportView(jLabel);
        } catch (IOException ex) {
            Logger.getLogger(SkeletonPreparing.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   //init Component function was here

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {                                          

        if(upperClass2!=null){
            //other irrelevant processing deleted 

            //scrollbars states storing part
            upperClass2.verticalBar = jScrollPaneImageScreen.getVerticalScrollBar().getValue();
            upperClass2.horizontalBar = jScrollPaneImageScreen.getHorizontalScrollBar().getValue();
//            upperClass2.react = jScrollPaneImageScreen.getViewport().getViewRect();
            System.out.println("Stored: "+upperClass2.verticalBar+","+upperClass2.horizontalBar);
            upperClass2.setVisible(true);
        }
        dispose();
    }                                         
    //variable initializations were here
}

BatchPaperEvaluation.java class,从我创建上述实例的地方 class:

private void jButtonSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
    File[] filesToBeProcessed = new File(jTextFieldOMRFolderPath.getText()).listFiles(filter);
    for (File inputFile : filesToBeProcessed) {
        MultipleProcess dialog = new MultipleProcess(true, inputFile, null, this);
        //other processing
    }

    //further processing
} 

注:

如果您使用 scrollPane.getVerticalScrollBar().getValue(),您可以在关闭窗格时保存该值。

然后,当您重新加载窗格时,只需使用 scrollPane.getVerticalScrollBar.setValue(savedValue)

您也可以对水平滚动条执行相同的操作。

我查看了您在评论中提出的 MCVE,我强烈怀疑问题出在您给我的代码中,可能是在调用 new MultipleProcess.[=15= 的 class 中]

尝试JScrollPane.getViewport().getViewRect()获取可见矩形。您可以通过执行 JScrollPane.getViewport().scrollRectToVisible(<saved rect>).

来恢复它