📄 exercise16_6.java
字号:
// Exercise16_6.java: Displays a histogram that shows the occurence// of each letter in a text fileimport java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Exercise16_6 extends JFrame implements ActionListener { int count[] = new int[26]; private JTextField jtf = new JTextField(20); private Histogram display = new Histogram(); public Exercise16_6() { JPanel p = new JPanel(); p.setLayout(new BorderLayout()); p.add(new JLabel("Text File"), BorderLayout.WEST); p.add(jtf, BorderLayout.CENTER); display.setBorder(new LineBorder(Color.red, 1)); getContentPane().setLayout(new BorderLayout()); getContentPane().add(p, BorderLayout.SOUTH); getContentPane().add(display, BorderLayout.CENTER); jtf.addActionListener(this); } public void actionPerformed(ActionEvent e) { FileReader in = null; for (int i = 0; i<26; i++) { count[i] = 0; } try { // Create file input stream in = new FileReader(new File(jtf.getText().trim())); int r; while ((r = in.read()) != -1) { if ((r >= 65) && (r <= 90)) { count[r - 65]++; } else if ((r >= 97) && (r <= 122)) { count[r - 97]++; } } } catch (FileNotFoundException ex) { System.out.println("File not found: " + jtf.getText().trim()); } catch (IOException ex) { System.out.println(ex.getMessage()); } finally { try { if (in != null) in.close(); } catch (IOException ex) {} } display.showHistogram(count); } public static void main (String[] args) { Exercise16_6 frame = new Exercise16_6(); frame.setSize(500, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Exercise16_6"); frame.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -