/*MultipleWindowsDemo.java*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MultipleWindowsDemo extends JFrame implements ActionListener
{ private JTextArea jta;
  private JButton jbtShowHistogram=new JButton("显示直方图");
  private Histogram histogram=new Histogram();
  private JFrame histogramFrame=new JFrame();

  public MultipleWindowsDemo()
  { JScrollPane scrollPane=new JScrollPane(jta=new JTextArea());
    scrollPane.setPreferredSize(new Dimension(300,200));
    jta.setWrapStyleWord(true);
    jta.setLineWrap(true);
    getContentPane().add(scrollPane,BorderLayout.CENTER);
    getContentPane().add(jbtShowHistogram,BorderLayout.SOUTH);
    jbtShowHistogram.addActionListener(this);
    histogramFrame.getContentPane().add(histogram);
    histogramFrame.pack();
    histogramFrame.setTitle("统计字母个数的直方图");
  }
  
  public void actionPerformed(ActionEvent e)
  { int[] count=countLetters();
    histogram.showHistogram(count);
    histogramFrame.setVisible(true);
  }

  private int[] countLetters()
  { int[] count=new int[26];
    String text=jta.getText();
    for(int i=0;i<text.length();i++)
    { char character=text.charAt(i);
      if((character>='A') && (character<='Z'))
      { count[(int)character-65]++;
      }
      else if ((character>='a') && (character<='z'))
      { count[(int)character-97]++;
      }
    }
    return count;
  }

  public static void main(String[] args)
  { MultipleWindowsDemo frame=new MultipleWindowsDemo();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("多窗口应用实例");
    frame.pack();
    frame.setVisible(true);
  }
}