前几天讲了Swing基本的控件,今天开始讲特殊控件。一天讲2个吧,首先讲用JProgressBar,ProgressMonitor和BoundedRangeModel来实现进度条。然后讲用JSlider和BoundedRangeModel来实现滑动条。
进度条,eg
- public class TestJProgressBar
- {
- JFrame frame = new JFrame("测试进度条");
- //创建一条垂直进度条
- JProgressBar bar = new JProgressBar(JProgressBar.VERTICAL );
- JCheckBox indeterminate = new JCheckBox("不确定进度");
- JCheckBox noBorder = new JCheckBox("不绘制边框");
- public void init()
- {
- Box box = new Box(BoxLayout.Y_AXIS);
- box.add(indeterminate);
- box.add(noBorder);
- frame.setLayout(new FlowLayout());
- frame.add(box);
- //把进度条添加到JFrame窗口中
- frame.add(bar);
- //设置在进度条中绘制完成百分比
- bar.setStringPainted(true);
- noBorder.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- //根据该选择框决定是否绘制进度条的边框
- bar.setBorderPainted(!noBorder.isSelected());
- }
- });
- final SimulatedTarget target = new SimulatedTarget(1000);
- //以启动一条线程的方式来执行一个耗时的任务
- new Thread(target).start();
- //设置进度条的最大值和最小值,
- bar.setMinimum(0);
- //以总任务量作为进度条的最大值
- bar.setMaximum(target.getAmount());
- Timer timer = new Timer(300 , new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- //以任务的当前完成量设置进度条的value
- bar.setValue(target.getCurrent());
- }
- });
- timer.start();
- indeterminate.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- //设置该进度条的进度是否确定
- bar.setIndeterminate(indeterminate.isSelected());
- bar.setStringPainted(!indeterminate.isSelected());
- }
- });
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args)
- {
- new TestJProgressBar().init();
- }
- }
- //模拟一个耗时的任务
- class SimulatedTarget implements Runnable
- {
- //任务的当前完成量
- private volatile int current;
- //总任务量
- private int amount;
- public SimulatedTarget(int amount)
- {
- current = 0;
- this.amount = amount;
- }
- public int getAmount()
- {
- return amount;
- }
- public int getCurrent()
- {
- return current;
- }
- //run方法代表不断完成任务的过程
- public void run()
- {
- while (current < amount)
- {
- try
- {
- Thread.sleep(50);
- }
- catch(InterruptedException e)
- {
- }
- current++;
- }
- }
- }
SimulatedTarget模拟了一个耗时程序,JProgressBar存在BoundedRangeModel用于保存数据对象。
ProgressMonitor是进度对话框,用起来很方便,但就缺乏了自主性,eg
- public class TestProgressMonitor
- {
- Timer timer;
- public void init()
- {
- final SimulatedTarget target = new SimulatedTarget(1000);
- //以启动一条线程的方式来执行一个耗时的任务
- final Thread targetThread = new Thread(target);
- targetThread.start();
- //创建进度对话框
- final ProgressMonitor dialog = new ProgressMonitor(null ,
- "等待任务完成" , "已完成:" , 0 , target.getAmount());
- //创建一个计时器
- timer = new Timer(300 , new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- //以任务的当前完成量设置进度对话框的完成比例
- dialog.setProgress(target.getCurrent());
- //如果用户单击了进度对话框的”取消“按钮
- if (dialog.isCanceled())
- {
- //停止计时器
- timer.stop();
- //中断任务的执行线程
- targetThread.interrupt();
- //系统退出
- System.exit(0);
- }
- }
- });
- timer.start();
- }
- public static void main(String[] args)
- {
- new TestProgressMonitor().init();
- }
- }
这个程序利用了上面的耗时程序。进度条就是那么简单。
滑动条和进度条差不多,他们共用一个模型BoundedRangeModel。下面是个滑动条的各样式的演示例子,eg
- public class TestJSlider
- {
- JFrame mainWin = new JFrame("滑动条示范");
- Box sliderBox = new Box(BoxLayout.Y_AXIS);
- JTextField showVal = new JTextField();
- ChangeListener listener;
- public void init()
- {
- //定义一个监听器,用于监听所有滑动条
- listener = new ChangeListener()
- {
- public void stateChanged(ChangeEvent event)
- {
- //取出滑动条的值,并在文本中显示出来
- JSlider source = (JSlider) event.getSource();
- showVal.setText("当前滑动条的值为:" + source.getValue());
- }
- };
- //-----------添加一个普通滑动条-----------
- JSlider slider = new JSlider();
- addSlider(slider, "普通滑动条");
- //-----------添加保留区为30的滑动条-----------
- slider = new JSlider();
- slider.setExtent(30);
- addSlider(slider, "保留区为30");
- //-----------添加带主、次刻度的滑动条,并设置其最大值,最小值-----------
- slider = new JSlider(30 , 200);
- //设置绘制刻度
- slider.setPaintTicks(true);
- //设置主、次刻度的间距
- slider.setMajorTickSpacing(20);
- slider.setMinorTickSpacing(5);
- addSlider(slider, "有刻度");
- //-----------添加滑块必须停在刻度处滑动条-----------
- slider = new JSlider();
- //设置滑块必须停在刻度处
- slider.setSnapToTicks(true);
- //设置绘制刻度
- slider.setPaintTicks(true);
- //设置主、次刻度的间距
- slider.setMajorTickSpacing(20);
- slider.setMinorTickSpacing(5);
- addSlider(slider, "滑块停在刻度处");
- //-----------添加没有滑轨的滑动条-----------
- slider = new JSlider();
- //设置绘制刻度
- slider.setPaintTicks(true);
- //设置主、次刻度的间距
- slider.setMajorTickSpacing(20);
- slider.setMinorTickSpacing(5);
- //设置不绘制滑轨
- slider.setPaintTrack(false);
- addSlider(slider, "无滑轨");
- //-----------添加方向反转的滑动条-----------
- slider = new JSlider();
- //设置绘制刻度
- slider.setPaintTicks(true);
- //设置主、次刻度的间距
- slider.setMajorTickSpacing(20);
- slider.setMinorTickSpacing(5);
- //设置方向反转
- slider.setInverted(true);
- addSlider(slider, "方向反转");
- //-----------添加绘制默认刻度标签的滑动条-----------
- slider = new JSlider();
- //设置绘制刻度
- slider.setPaintTicks(true);
- //设置主、次刻度的间距
- slider.setMajorTickSpacing(20);
- slider.setMinorTickSpacing(5);
- //设置绘制刻度标签,默认绘制数值刻度标签
- slider.setPaintLabels(true);
- addSlider(slider, "数值刻度标签");
- //-----------添加绘制Label类型的刻度标签的滑动条-----------
- slider = new JSlider();
- //设置绘制刻度
- slider.setPaintTicks(true);
- //设置主、次刻度的间距
- slider.setMajorTickSpacing(20);
- slider.setMinorTickSpacing(5);
- //设置绘制刻度标签
- slider.setPaintLabels(true);
- Dictionary<Integer, Component> labelTable = new Hashtable<Integer, Component>();
- labelTable.put(0, new JLabel("A"));
- labelTable.put(20, new JLabel("B"));
- labelTable.put(40, new JLabel("C"));
- labelTable.put(60, new JLabel("D"));
- labelTable.put(80, new JLabel("E"));
- labelTable.put(100, new JLabel("F"));
- //指定刻度标签,标签是JLabel
- slider.setLabelTable(labelTable);
- addSlider(slider, "JLable标签");
- //-----------添加绘制Label类型的刻度标签的滑动条-----------
- slider = new JSlider();
- //设置绘制刻度
- slider.setPaintTicks(true);
- //设置主、次刻度的间距
- slider.setMajorTickSpacing(20);
- slider.setMinorTickSpacing(5);
- //设置绘制刻度标签
- slider.setPaintLabels(true);
- labelTable = new Hashtable<Integer, Component>();
- labelTable.put(0, new JLabel(new ImageIcon("ico/0.GIF")));
- labelTable.put(20, new JLabel(new ImageIcon("ico/2.GIF")));
- labelTable.put(40, new JLabel(new ImageIcon("ico/4.GIF")));
- labelTable.put(60, new JLabel(new ImageIcon("ico/6.GIF")));
- labelTable.put(80, new JLabel(new ImageIcon("ico/8.GIF")));
- //指定刻度标签,标签是ImageIcon
- slider.setLabelTable(labelTable);
- addSlider(slider, "Icon标签");
- mainWin.add(sliderBox, BorderLayout.CENTER);
- mainWin.add(showVal, BorderLayout.SOUTH);
- mainWin.pack();
- mainWin.setVisible(true);
- }
- //定义一个方法,用于将滑动条添加到容器中
- public void addSlider(JSlider slider, String description)
- {
- slider.addChangeListener(listener);
- Box box = new Box(BoxLayout.X_AXIS);
- box.add(new JLabel(description + ":"));
- box.add(slider);
- sliderBox.add(box);
- }
- public static void main(String[] args)
- {
- new TestJSlider().init();
- }
- }
原文链接:http://blog.csdn.net/terryzero/article/details/3797782
【编辑推荐】