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

 import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class JScrollBarExample implements AdjustmentListener
{
JPanel jPanel = new JPanel();
/*
* 产生一个垂直滚动轴,默认滚动轴位置在10刻度的地方,extent值设10,minimum值为0, maximan值为100,因此滚动轴一开始在刻度10的位置上,
* 可滚动的区域大小为100-10-0=90刻度,滚动范围在0~90中。
*/
JScrollBar scrollBarVerticalDirection = new JScrollBar(JScrollBar.VERTICAL, 10, 10, 0, 100);
JScrollBar scrollBarHorizontalDirection = new JScrollBar();
JLabel jLable = new JLabel("刻度:", JLabel.CENTER);
JLabel jLableForResult = new JLabel("Result:", JLabel.CENTER);
JFrame jFrame = new JFrame("JScrollBarDemo");
JLabel label1 = new JLabel(new ImageIcon(".\\icons\\flower.jpg")); public JScrollBarExample()
{
jbInit();
} private void jbInit()
{
Container contentPane = jFrame.getContentPane();
jPanel.add(jLable);
// 设置拖曳滚动轴时,滚动轴刻度一次的变化量。
scrollBarVerticalDirection.setUnitIncrement(1);
// 设置当鼠标在滚动轴列上按一下是,滚动轴一次所跳的区块大小
scrollBarVerticalDirection.setBlockIncrement(10);
scrollBarVerticalDirection.addAdjustmentListener(this); contentPane.add(jPanel, BorderLayout.CENTER);
contentPane.add(scrollBarVerticalDirection, BorderLayout.EAST);
contentPane.add(jLable, BorderLayout.NORTH);
//contentPane.add(jLableForResult, BorderLayout.NORTH); jFrame.setSize(new Dimension(200, 200));
jFrame.setVisible(true);
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public void adjustmentValueChanged(AdjustmentEvent e) {
if ((JScrollBar) e.getSource() == scrollBarVerticalDirection)
{
jLable.setText("垂直刻度" + e.getValue());
if(e.getValue() >= 90)
{
//jLableForResult.setText("XXXX");
System.out.println("XXXXXXXXXXXXXX ");
}
}
} public static void main(String[] args)
{
new JScrollBarExample();
}
} 效果图:

JScrollBar-LMLPHP

参考资料:

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)

05-06 01:24