showImg.setIcon(imgs[counter]); counter++; showImg.repaint(); } } }
例6-16 TimerAndJProgressBar.java
import javax.swing.*; import java.awt.*;
import java.awt.event.*; import javax.swing.event.*;
public class TimerAndJProgressBar extends JFrame implements ActionListener, ChangeListener { JPanel p = new JPanel(); JLabel msg = new JLabel(\ JProgressBar pgbar = new JProgressBar(); Timer timer = new Timer(100, this); JButton start = new JButton(\开始\ JButton stop = new JButton(\停止\
public TimerAndJProgressBar() { Container c = this.getContentPane(); Box hbox = Box.createHorizontalBox(); p.add(hbox); hbox.add(start); hbox.add(hbox.createHorizontalStrut(20)); hbox.add(stop); c.add(p, BorderLayout.NORTH); c.add(pgbar, BorderLayout.CENTER); c.add(msg, BorderLayout.SOUTH); pgbar.setStringPainted(true); pgbar.setString(\ pgbar.setPreferredSize(new Dimension(200, 30)); pgbar.setBorderPainted(false); pgbar.setBackground(Color.white); pgbar.setForeground(Color.black); pgbar.addChangeListener(this); start.addActionListener(this); stop.addActionListener(this); this.setTitle(\ this.setBounds(200, 200, 300, 140); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);
}
public static void main(String[] args) { new TimerAndJProgressBar(); }
public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { timer.start(); } else if (e.getSource() == stop) { timer.stop(); } else if (e.getSource() == timer) { int value = pgbar.getValue(); if (value < 100) { value++; pgbar.setValue(value); } else { timer.stop(); pgbar.setValue(0); } } }
public void stateChanged(ChangeEvent e) { int value = pgbar.getValue();
if (e.getSource() == pgbar) { msg.setText(\目前已完成:\
}
} }
例6-17 JFileChooserDemo.java
import java.awt.*;
import java.awt.event.*; import javax.swing.*; import java.io.*;
public class JFileChooserDemo extends JFrame implements ActionListener { JLabel msg = new JLabel(\ JTextArea ta = new JTextArea(); JFileChooser fc = new JFileChooser(); JButton openf = new JButton(\ JButton savef = new JButton(\ JPanel p = new JPanel(); JScrollPane sp = new JScrollPane(ta); public JFileChooserDemo() {
Container c = this.getContentPane(); sp.setPreferredSize(new Dimension(350, 300)); openf.addActionListener(this); savef.addActionListener(this); p.add(openf); p.add(savef); c.add(msg, \ c.add(sp, \ c.add(p, \ this.setTitle(\ this.setBounds(200, 200, 300, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public static void main(String[] args) { new JFileChooserDemo(); }
public void actionPerformed(ActionEvent e) { File file = null; int result; FileInputStream fileInStream = null; FileOutputStream fileOutStream = null; int readbyte; if (e.getSource() == openf) { fc.setApproveButtonText(\确定\ fc.setDialogTitle(\打开文件\
result = fc.showOpenDialog(this); ta.setText(\
if (result == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile(); msg.setText(\您选择打开的文件名称为:\} else if (result == JFileChooser.CANCEL_OPTION) { msg.setText(\您没有选择任何文件\}
if (file != null) { try { fileInStream = new FileInputStream(file); } catch (FileNotFoundException fe) { msg.setText(\ return; }
try { while ((readbyte = fileInStream.read()) != -1) {
ta.append(String.valueOf((char) readbyte)); }
} catch (IOException ioe) { msg.setText(\读取文件错误\
} finally {
try { if (fileInStream != null) fileInStream.close(); } catch (IOException ioe2) { } } } }
if (e.getSource() == savef) { result = fc.showSaveDialog(this); file = null; String fileName; if (result == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile(); msg.setText(\您选择存储的文件名称为:\
} else if (result == JFileChooser.CANCEL_OPTION) { msg.setText(\您没有选择任何文件\}
if (file != null) { try { fileOutStream = new FileOutputStream(file); } catch (FileNotFoundException fe) { msg.setText(\ return; }
String content = ta.getText();
try { fileOutStream.write(content.getBytes()); } catch (IOException ioe) { msg.setText(\写入文件错误\} finally { try { if (fileOutStream != null) fileOutStream.close(); } catch (IOException ioe2) { }
} } } } }
例6-18 JTreeDemo.java
import java.awt.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; import java.io.*;
public class JTreeDemo extends JFrame implements TreeSelectionListener { JTextPane tp = new JTextPane(); public JTreeDemo() { Container c = this.getContentPane(); DefaultMutableTreeNode root = new DefaultMutableTreeNode(\例题代码浏览器\
DefaultMutableTreeNode node = new DefaultMutableTreeNode(\ root.add(node); node = new DefaultMutableTreeNode(\ root.add(node); node = new DefaultMutableTreeNode(\ root.add(node); JTree tree = new JTree(root); JScrollPane spTree = new JScrollPane(tree); JScrollPane spShow = new JScrollPane(tp); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, spTree, spShow); tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); tree.addTreeSelectionListener(this); c.add(splitPane); this.setTitle(\ this.setBounds(200, 100, 700, 400); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public static void main(String[] args) { new JTreeDemo(); }
public void valueChanged(TreeSelectionEvent e) {