如何向下滚动JScrollPane上的垂直滚动条?

罗伯托·加西亚(Roberto Garcia)

我将Netbeans与Java / Swing一起使用,并且滚动窗格有问题。首先,JPanel我的滚动面板内部有两个组件。当我初始化所有组件时,第二个面板将不可见。我希望当我单击按钮时,第二个面板设置可见(它起作用),并且垂直滚动条向下移动以显示第二个面板。

我在单击按钮时使用以下代码:

public void eventoAdd() {
      miVista.getJpanelSegundo().setVisible(true);
      miVista.getJScrollPane1().getVerticalScrollBar().setValue(280);
 }

第一次单击时,垂直滚动条从不会移到底部,但是在第二次尝试时,垂直滚动条似乎移到底部。问题是什么?

PS:我看到很多问题,但是没有关于这个特定问题的答案。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author rober
 */
public class View extends javax.swing.JFrame {

    /**
     * Creates new form View
     */
    public View() {
    initComponents();
    jPanelSecond.setVisible(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

    jScrollPaneFirst = new javax.swing.JScrollPane();
    jPanelMain = new javax.swing.JPanel();
    jPanelFirst = new javax.swing.JPanel();
    jPanelSecond = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jScrollPaneFirst.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    jPanelFirst.setBorder(javax.swing.BorderFactory.createTitledBorder("First"));

    javax.swing.GroupLayout jPanelFirstLayout = new javax.swing.GroupLayout(jPanelFirst);
    jPanelFirst.setLayout(jPanelFirstLayout);
    jPanelFirstLayout.setHorizontalGroup(
        jPanelFirstLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    jPanelFirstLayout.setVerticalGroup(
        jPanelFirstLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 61, Short.MAX_VALUE)
    );

    jPanelSecond.setBorder(javax.swing.BorderFactory.createTitledBorder("Second"));

    javax.swing.GroupLayout jPanelSecondLayout = new javax.swing.GroupLayout(jPanelSecond);
    jPanelSecond.setLayout(jPanelSecondLayout);
    jPanelSecondLayout.setHorizontalGroup(
        jPanelSecondLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 294, Short.MAX_VALUE)
    );
    jPanelSecondLayout.setVerticalGroup(
        jPanelSecondLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 63, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout jPanelMainLayout = new javax.swing.GroupLayout(jPanelMain);
    jPanelMain.setLayout(jPanelMainLayout);
    jPanelMainLayout.setHorizontalGroup(
        jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanelFirst, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(jPanelSecond, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    jPanelMainLayout.setVerticalGroup(
        jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanelMainLayout.createSequentialGroup()
            .addComponent(jPanelFirst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanelSecond, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

    jScrollPaneFirst.setViewportView(jPanelMain);

    jButton1.setText("Add");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPaneFirst, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addGap(98, 98, 98)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPaneFirst, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton1))
    );

    pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jPanelSecond.setVisible(true);
    jScrollPaneFirst.getVerticalScrollBar().setValue(280);
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new View().setVisible(true);
        }
    });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanelFirst;
    private javax.swing.JPanel jPanelMain;
    private javax.swing.JPanel jPanelSecond;
    private javax.swing.JScrollPane jScrollPaneFirst;
    // End of variables declaration                   
}
疯狂程序员

作为个人喜好,我发现scrollRectToVisible它是实现此目标的最可靠方法之一,但是您确实需要知道如何使用它

滚动

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jPanelSecond.setVisible(true);
    Rectangle viewBounds = jScrollPaneFirst.getViewportBorderBounds();
    Dimension size = jPanelMain.getPreferredSize();
    jPanelMain.scrollRectToVisible(new Rectangle(0, size.height, viewBounds.width, viewBounds.height));
    //jScrollPaneFirst.getVerticalScrollBar().setValue(280);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何向下滚动JScrollPane上的垂直滚动条?

来自分类Dev

如何向下滚动滚动条

来自分类Dev

Jscrollpane设置垂直滚动条的轨道高度

来自分类Dev

Bootstrap:如何禁用垂直滚动条?

来自分类Dev

如何强制显示垂直滚动条?

来自分类Dev

Bootstrap:如何禁用垂直滚动条?

来自分类Dev

删除垂直滚动条

来自分类Dev

垂直滚动条不滚动

来自分类Dev

QScrollArea:垂直滚动条引起的水平滚动条

来自分类Dev

更改JScrollPane滚动条颜色

来自分类Dev

JScrollPane滚动条不会显示吗?

来自分类Dev

JScrollpane滚动条未显示

来自分类Dev

在DataGridView垂直滚动条上绘制标记

来自分类Dev

在闪亮的仪表板上禁用垂直滚动条

来自分类Dev

在滚动条上移动垂直线

来自分类Dev

如何在UWP上隐藏列表视图垂直滚动条?

来自分类Dev

在Firefox上设置滚动条的样式。如何摆脱滚动条上的黑色边框/轮廓?

来自分类Dev

如何始终在SWT表中显示垂直滚动条?

来自分类Dev

如何加快垂直滚动条标记的渲染

来自分类Dev

如何自定义Apple Watch垂直滚动条

来自分类Dev

如何在extjs中放置垂直滚动条

来自分类Dev

如何在小部件中添加垂直滚动条?

来自分类Dev

如何使Bootstrap下拉菜单的垂直滚动条

来自分类Dev

如何在tabcontrol / tabpages中添加垂直滚动条

来自分类Dev

如何使垂直滚动条不可见?

来自分类Dev

如何自定义Apple Watch垂直滚动条

来自分类Dev

如何显示JList的水平和垂直滚动条?

来自分类Dev

如何为openTK gameWindow实现垂直滚动条

来自分类Dev

如何设置垂直滚动条总是在底部?