Java Swing JScrollPane

2018-01-09 19:23 更新

Java Swing教程 - Java Swing JScrollPane


JScrollPane为其他元素提供滚动功能。

以下代码演示如何使用不同的选项创建JScrollPane:

创建没有组件作为其视口的JScrollPane和  使用默认滚动条策略

JScrollPane sp1  = new JScrollPane();

使用JTextArea作为其视口和创建JScrollPane  默认滚动条策略为“根据需要"

JTextArea description = new JTextArea(10, 60); 
JScrollPane sp2  = new JScrollPane(description);

使用JTextArea作为其视口和创建JScrollPane  两个滚动条策略设置为“始终显示"

JTextArea comments = new JTextArea(10, 60); 
JScrollPane sp3  = new JScrollPane(comments, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

JScrollPane的视口保留对我们添加到JScrollPane的组件的引用。 您可以通过查询其视口来获取JScrollPane中组件的引用,如图所示:

JViewport vp  = sp3.getViewport();
JTextArea comments1 = (JTextArea)vp.getView();

要将组件添加到JScrollPane的视口:

sp3.setViewportView(new JTextPane());


JScrollPane标题和角

要将组件放置在JScrollPane的其中一个角上,请调用setCorner(String key,Component corner)键是

  • JScrollPane.LOWER_LEFT_CORNER
  • JScrollPane.LOWER_RIGHT_CORNER
  • JScrollPane.UPPER_LEFT_CORNER
  • JScrollPane.UPPER_RIGHT_CORNER
import java.awt.BorderLayout;
import java.awt.GridLayout;
/*from   ww w  .j  a v  a 2s .  co  m*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;

public class Main {

  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPaneDemo());
    f.setSize(500, 500);
    f.setVisible(true);
  }
}

class JScrollPaneDemo extends JPanel {

  public void init() {
    try {
      SwingUtilities.invokeAndWait(new Runnable() {
        public void run() {
          makeGUI();
        }
      });
    } catch (Exception exc) {
      System.out.println("Can"t create because of " + exc);
    }
  }

  private void makeGUI() {

    setLayout(new BorderLayout());

    JPanel jp = new JPanel();
    jp.setLayout(new GridLayout(20, 20));
    int b = 0;
    for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
        jp.add(new JButton("Button " + b));
        ++b;
      }
    }

    int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
    JScrollPane jsp = new JScrollPane(jp, v, h);

    add(jsp, BorderLayout.CENTER);
  }
}


ScrollBar策略

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
/*from  w  ww  .  j a va  2  s .c o  m*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

public class Main {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = new JLabel("Label");
    label.setPreferredSize(new Dimension(1000, 1000));
    JScrollPane jScrollPane = new JScrollPane(label);

    JButton jButton1 = new JButton();

    jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jScrollPane.setViewportBorder(new LineBorder(Color.RED));
    jScrollPane.getViewport().add(jButton1, null);

    frame.add(jScrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}

在JScrollPane中移动ScrollBar

import java.awt.event.*;
import javax.swing.*;
/*from w  w w. j  a v  a2s .c  om*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

class JScrollPaneToTopAction implements ActionListener {
  JScrollPane scrollPane;
  public JScrollPaneToTopAction(JScrollPane scrollPane) {
    if (scrollPane == null) {
      throw new IllegalArgumentException(
        "JScrollPaneToTopAction: null JScrollPane");
    }
    this.scrollPane = scrollPane;
  }
  public void actionPerformed(ActionEvent actionEvent) {
    JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
    JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
    verticalScrollBar.setValue(verticalScrollBar.getMinimum());
    horizontalScrollBar.setValue(horizontalScrollBar.getMinimum());
  }
}


public class Main {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = new JLabel("Label");
    label.setPreferredSize(new Dimension(1000, 1000));
    JScrollPane jScrollPane = new JScrollPane(label);

    JButton bn = new JButton("Move");
    
    bn.addActionListener(new JScrollPaneToTopAction(jScrollPane));
    
    frame.add(bn, BorderLayout.SOUTH);
    frame.add(jScrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}

滚动条值更改

在JScrollPane容器中侦听滚动条值更改

import java.awt.Adjustable;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
// www  . j ava2 s  .co m
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTextArea textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea);

    // Listen for value changes in the scroll pane"s scrollbars
    AdjustmentListener listener = new MyAdjustmentListener();
    pane.getHorizontalScrollBar().addAdjustmentListener(listener);
    pane.getVerticalScrollBar().addAdjustmentListener(listener);
  }
}

class MyAdjustmentListener implements AdjustmentListener {
  public void adjustmentValueChanged(AdjustmentEvent evt) {
    Adjustable source = evt.getAdjustable();
    if (evt.getValueIsAdjusting()) {
      return;
    }
    int orient = source.getOrientation();
    if (orient == Adjustable.HORIZONTAL) {
      System.out.println("from horizontal scrollbar"); 
    } else {
      System.out.println("from vertical scrollbar");
    }
    int type = evt.getAdjustmentType();
    switch (type) {
    case AdjustmentEvent.UNIT_INCREMENT:
      System.out.println("Scrollbar was increased by one unit");
      break;
    case AdjustmentEvent.UNIT_DECREMENT:
      System.out.println("Scrollbar was decreased by one unit");
      break;
    case AdjustmentEvent.BLOCK_INCREMENT:
      System.out.println("Scrollbar was increased by one block");
      break;
    case AdjustmentEvent.BLOCK_DECREMENT:
      System.out.println("Scrollbar was decreased by one block");
      break;
    case AdjustmentEvent.TRACK:
      System.out.println("The knob on the scrollbar was dragged");
      break;
    }
    int value = evt.getValue();
  }
}
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号