⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multiplewindowsdemo.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// MultipleWindowsDemo.java: Display histogram in a seperate window
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("Show Histogram");
  private Histogram histogram = new Histogram();

  // Create a new frame to hold the histogram panel
  private JFrame histogramFrame = new JFrame();

  // Construct the frame
  public MultipleWindowsDemo()
  {
    // Store text area in a scroll pane
    JScrollPane scrollPane = new JScrollPane(jta = new JTextArea());
    scrollPane.setPreferredSize(new Dimension(300, 200));
    jta.setWrapStyleWord(true);
    jta.setLineWrap(true);

    // Place scroll pane and button in the frame
    getContentPane().add(scrollPane, BorderLayout.CENTER);
    getContentPane().add(jbtShowHistogram, BorderLayout.SOUTH);

    // Register listener
    jbtShowHistogram.addActionListener(this);

    // Create a new frame to hold the histogram panel
    histogramFrame.getContentPane().add(histogram);
    histogramFrame.pack();
    histogramFrame.setTitle("Histogram");
  }

  // Handle the button action
  public void actionPerformed(ActionEvent e)
  {
    // Count the letters in the text area
    int[] count = countLetters();

    // Set the letter count to histogram for display
    histogram.showHistogram(count);

    // Show the frame
    histogramFrame.setVisible(true);
  }

  // Count the letters in the text area
  private int[] countLetters()
  {
    // Count for 26 letters
    int[] count = new int[26];

    // Get contents from the text area
    String text = jta.getText();

    // Count occurence of each letter (case insensitive)
    for (int i=0; i<text.length(); i++)
    {
      char character = text.charAt(i);

      if ((character >= 'A') && (character <= 'Z'))
      {
        count[(int)character-65]++; // The ASCII for 'A' is 65
      }
      else if ((character >= 'a') && (character <= 'z'))
      {
        count[(int)character-97]++; // The ASCII for 'a' is 97
      }
    }

    return count; // Return the count array
  }

  // Main method
  public static void main(String[] args)
  {
    MultipleWindowsDemo frame = new MultipleWindowsDemo();
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Multiple Windows Demo");
    frame.pack();
    frame.setVisible(true);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -