首页 > 代码库 > JScrollBar

JScrollBar

    接到了GUI相关的task,从来没看Java的我只好各种百度加看书了。这里介绍了 JScrollBar 的简单应用。    话不多说,直接上代码和效果图。

 

 1 import java.awt.*;   2 import java.awt.event.*;   3 import javax.swing.*; 4  5 public class JScrollBarExample implements AdjustmentListener 6 { 7     JPanel jPanel = new JPanel(); 8     /*  9     * 产生一个垂直滚动轴,默认滚动轴位置在10刻度的地方,extent值设10,minimum值为0, maximan值为100,因此滚动轴一开始在刻度10的位置上,
* 可滚动的区域大小为100-10-0=90刻度,滚动范围在0~90中。
10 */ 11 JScrollBar scrollBarVerticalDirection = new JScrollBar(JScrollBar.VERTICAL, 10, 10, 0, 100);12 JScrollBar scrollBarHorizontalDirection = new JScrollBar();13 JLabel jLable = new JLabel("刻度:", JLabel.CENTER);14 JLabel jLableForResult = new JLabel("Result:", JLabel.CENTER);15 JFrame jFrame = new JFrame("JScrollBarDemo");16 JLabel label1 = new JLabel(new ImageIcon(".\\icons\\flower.jpg")); 17 18 public JScrollBarExample()19 {20 jbInit();21 }22 23 private void jbInit()24 {25 Container contentPane = jFrame.getContentPane();26 jPanel.add(jLable);27 // 设置拖曳滚动轴时,滚动轴刻度一次的变化量。28 scrollBarVerticalDirection.setUnitIncrement(1);29 // 设置当鼠标在滚动轴列上按一下是,滚动轴一次所跳的区块大小 30 scrollBarVerticalDirection.setBlockIncrement(10);31 scrollBarVerticalDirection.addAdjustmentListener(this);32 33 contentPane.add(jPanel, BorderLayout.CENTER);34 contentPane.add(scrollBarVerticalDirection, BorderLayout.EAST);35 contentPane.add(jLable, BorderLayout.NORTH);36 //contentPane.add(jLableForResult, BorderLayout.NORTH);37 38 jFrame.setSize(new Dimension(200, 200));39 jFrame.setVisible(true);40 jFrame.addWindowListener(new WindowAdapter() { 41 public void windowClosing(WindowEvent e) { 42 System.exit(0); 43 } 44 });45 }46 47 public void adjustmentValueChanged(AdjustmentEvent e) {48 if ((JScrollBar) e.getSource() == scrollBarVerticalDirection)49 {50 jLable.setText("垂直刻度" + e.getValue());51 if(e.getValue() >= 90)52 {53 //jLableForResult.setText("XXXX");54 System.out.println("XXXXXXXXXXXXXX ");55 }56 }57 }58 59 public static void main(String[] args)60 {61 new JScrollBarExample();62 }63 }

效果图:
技术分享

 

参考资料:

erbo2008.iteye.com/blog/834862

http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/javax/swing/JScrollBar.html#JScrollBar(int,%20int,%20int,%20int,%20int)

 

JScrollBar