前幾天講了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();
- }
- }