📄 chapter21n1.java
字号:
/** * * applet to display demonstration Java program source code - Vector version * * Written by: Roger Garside * * First Written: 5/Feb/97 * Last Rewritten: 24/Sept/97 * */import java.applet.* ;import java.awt.* ;import java.awt.event.* ;import java.io.* ;import java.net.* ;import java.util.* ;public class Chapter21n1 extends Applet implements ActionListener { // picture elements private Label label ; private TextArea area ; private Button main, chap, prog ; // list of all programs available private Vector mainIndex ; private String indexString ; // list of programs for current chapter private Vector chapterIndex ; private String indexString1 ; /** * * init * */ public void init() { readIndex() ; setLayout(new BorderLayout()) ; setBackground(Color.green) ; label = new Label("Main Index", Label.CENTER) ; add("North", label) ; // set up the text area area = new TextArea(20, 80) ; area.setText(indexString) ; add("Center", area) ; // set up the buttons Panel p = new Panel() ; p.setLayout(new FlowLayout()) ; main = new Button("Main Index") ; p.add(main) ; main.addActionListener(this) ; main.setEnabled(false) ; chap = new Button("Chapter") ; p.add(chap) ; chap.addActionListener(this) ; chap.setEnabled(true) ; prog = new Button("Program") ; p.add(prog) ; prog.addActionListener(this) ; prog.setEnabled(false) ; add("South", p) ; } // end of method init /** * * readIndex * */ private void readIndex() { try { // access the file "programList" URL index = new URL(getDocumentBase(), "programList") ; BufferedReader s1 = new BufferedReader(new InputStreamReader(index.openStream())) ; mainIndex = new Vector() ; indexString = "" ; String previousFirst = "" ; while (true) { String line = s1.readLine() ; if (line == null) break ; StringTokenizer st = new StringTokenizer(line, "/") ; String first = st.nextToken() ; String second = st.nextToken() ; int start = -1 ; int finish = -1 ; if (!first.equals(previousFirst)) { if (indexString.length() != 0) indexString += '\n' ; start = indexString.length() ; indexString += first ; finish = indexString.length() ; } previousFirst = first ; mainIndex.addElement(new ProgramDetails(first, second, start, finish)) ; } } catch (IOException e) { System.err.println("ERROR: " + e) ; } } // end of method readIndex /** * * actionPerformed * */ public void actionPerformed(ActionEvent event) { // deal with the "Main Index" button if (event.getSource() == main) { label.setText("Main Index") ; area.setText(indexString) ; main.setEnabled(false) ; chap.setEnabled(true) ; prog.setEnabled(false) ; } // deal with the "Chapter" button else if (event.getSource() == chap) { // get the line selected int start = area.getSelectionStart() ; int finish = area.getSelectionEnd() ; // look for the entry in the "mainIndex" vector int i = 0 ; while ((i < mainIndex.size()) && (start > ((ProgramDetails) mainIndex.elementAt(i)).finish)) i++ ; if (i == mainIndex.size()) System.err.println("no chapter selected") ; else { // extract a list of programs in this chapter into the // vector "chapterIndex" and the string "indexString1" String thisChap = ((ProgramDetails) mainIndex.elementAt(i)).first ; chapterIndex = new Vector() ; indexString1 = "" ; String second ; for (int j = 0 ; j < mainIndex.size() ; j++) { ProgramDetails current = (ProgramDetails) mainIndex.elementAt(j) ; if (current.first.equals(thisChap)) { second = current.second ; if (indexString1.length() != 0) indexString1 += '\n' ; start = indexString1.length() ; indexString1 += second ; finish = indexString1.length() ; chapterIndex.addElement(new ProgramDetails(thisChap, second, start, finish)) ; } } label.setText(thisChap) ; area.setText(indexString1) ; main.setEnabled(true) ; chap.setEnabled(false) ; prog.setEnabled(true) ; } } // deal with the "Program" button else if (event.getSource() == prog) { // get the line selected int start = area.getSelectionStart() ; int finish = area.getSelectionEnd() ; // look for the entry in the "chapterIndex" vector int i = 0 ; while ((i < chapterIndex.size()) && (start > ((ProgramDetails) chapterIndex.elementAt(i)).finish)) i++ ; if (i == chapterIndex.size()) System.err.println("no program selected") ; else { // access the appropriate file of source text String thisProg = ((ProgramDetails) chapterIndex.elementAt(i)).second ; try { URL t = new URL(getDocumentBase(), thisProg) ; BufferedReader s1 = new BufferedReader(new InputStreamReader(t.openStream())) ; String textString = "" ; while (true) { String line = s1.readLine() ; if (line == null) break ; if (textString.length() != 0) textString += '\n' ; textString += line ; } label.setText(thisProg) ; area.setText(textString) ; main.setEnabled(true) ; chap.setEnabled(false) ; prog.setEnabled(false) ; } catch (IOException e1) { System.err.println("ERROR: " + e1) ; } } } } // end of method actionPerformed } // end of class Chapter21n1class ProgramDetails { String first, second ; int start, finish ; /** * * constructor * */ public ProgramDetails(String f, String s, int n1, int n2) { first = f ; second = s ; start = n1 ; finish = n2 ; } // end of constructor method } // end of class ProgramDetails
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -