Java Tutorial/Swing/JScrollBar — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 15:31, 31 мая 2010

Always display scrollbar

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
public class Main extends JFrame {
  JTextArea textArea;
  public Main() {
    super();
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    Container contentPane = this.getContentPane();
    textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    contentPane.add(pane, BorderLayout.CENTER);
  }
  public static void main(String[] args) {
    JFrame f = new Main();
    f.setSize(300, 200);
    f.setVisible(true);
  }
}





Customizing a JScrollBar Look and Feel

Property StringObject TypescrollbarColorScrollBar.actionMapActionMapScrollBar.allowsAbsolutePositioningBooleanScrollBar.ancestorInputMapInputMapScrollBar.ancestorInputMap.RightToLeftInputMapScrollBar.backgroundColorScrollBar.borderBorderScrollBar.darkShadowColorScrollBar.focusInputMapObject[ ]ScrollBar.focusInputMap.RightToLeftInputMapScrollBar.foregroundColorScrollBar.gradientListScrollBar.highlightColorScrollBar.maximumThumbSizeDimensionScrollBar.minimumThumbSizeDimensionScrollBar.shadowColorScrollBar.squareButtonsBooleanScrollBar.thumbColorScrollBar.thumbDarkShadowColorScrollBar.thumbHeightIntegerScrollBar.thumbHighlightColorScrollBar.thumbShadowColorScrollBar.trackColorScrollBar.trackForegroundColorScrollBar.trackHighlightColorScrollBar.trackHighlightForegroundColorScrollBar.widthIntegerScrollBarUIString


JScrollBar and Adjustment event

import java.awt.Adjustable;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
public class Main {
  JScrollBar scrollBarVertical = new JScrollBar();
  JScrollBar scrollbarHorizontal = new JScrollBar(Adjustable.HORIZONTAL);
  Main() {
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.setSize(280, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    scrollBarVertical.setPreferredSize(new Dimension(20, 200));
    scrollbarHorizontal.setPreferredSize(new Dimension(200, 20));
    scrollbarHorizontal.setValue(50);
    scrollBarVertical.addAdjustmentListener(new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent ae) {
        if (scrollBarVertical.getValueIsAdjusting())
          return;
        System.out.println("Value of vertical scroll bar: " + ae.getValue());
      }
    });
    scrollbarHorizontal.addAdjustmentListener(new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent ae) {
        System.out.println("Value of horizontal scroll bar: " + ae.getValue());
      }
    });
    f.add(scrollBarVertical);
    f.add(scrollbarHorizontal);
    f.setVisible(true);
  }
  public static void main(String args[]) {
    new Main();
  }
}





Listening to Scrolling Events with a ChangeListener

import java.awt.BorderLayout;
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
class BoundedChangeListener implements ChangeListener {
  public void stateChanged(ChangeEvent changeEvent) {
    Object source = changeEvent.getSource();
    if (source instanceof BoundedRangeModel) {
      BoundedRangeModel aModel = (BoundedRangeModel) source;
      if (!aModel.getValueIsAdjusting()) {
        System.out.println("Changed: " + aModel.getValue());
      }
    } else {
      System.out.println("Something changed: " + source);
    }
  }
}
public class ScrollBarSample {
  public static void main(String args[]) {
    ChangeListener changeListener = new BoundedChangeListener();
    JScrollBar aJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
    BoundedRangeModel model = aJScrollBar.getModel();
    model.addChangeListener(changeListener);
    JFrame frame = new JFrame("ScrollBars R Us");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(aJScrollBar, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





The simplest of the bounded range components is the JScrollBar

Handling Scrolling Events



import java.awt.BorderLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JFrame;
import javax.swing.JScrollBar;

public class ScrollBarSample {
  public static void main(String args[]) {
    AdjustmentListener adjustmentListener = new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) {
        System.out.println("Adjusted: " + adjustmentEvent.getValue());
      }
    };
    JScrollBar oneJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
    oneJScrollBar.addAdjustmentListener(adjustmentListener);
    JFrame frame = new JFrame("ScrollBars R Us");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(oneJScrollBar, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}