📅  最后修改于: 2020-09-30 06:00:36             🧑  作者: Mango
JScrollbar类的对象用于添加水平和垂直滚动条。它是滚动条的实现。它继承了JComponent类。
我们来看一下javax.swing.JScrollBar类的声明。
public class JScrollBar extends JComponent implements Adjustable, Accessible
Constructor | Description |
---|---|
JScrollBar() | Creates a vertical scrollbar with the initial values. |
JScrollBar(int orientation) | Creates a scrollbar with the specified orientation and the initial values. |
JScrollBar(int orientation, int value, int extent, int min, int max) | Creates a scrollbar with the specified orientation, value, extent, minimum, and maximum. |
import javax.swing.*;
class ScrollBarExample
{
ScrollBarExample(){
JFrame f= new JFrame("Scrollbar Example");
JScrollBar s=new JScrollBar();
s.setBounds(100,100, 50,100);
f.add(s);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ScrollBarExample();
}}
输出:
import javax.swing.*;
import java.awt.event.*;
class ScrollBarExample
{
ScrollBarExample(){
JFrame f= new JFrame("Scrollbar Example");
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400,100);
final JScrollBar s=new JScrollBar();
s.setBounds(100,100, 50,100);
f.add(s); f.add(label);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
s.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
label.setText("Vertical Scrollbar value is:"+ s.getValue());
}
});
}
public static void main(String args[])
{
new ScrollBarExample();
}}
输出: