如何将这些内容添加到我的第一个小程序中

时间:2014-02-26 19:46:37

标签: java applet

对于Java来说,我是初学者,而且我的起步很慢。我创建了一个Applet,允许用户在文本字段中输入某个句子,输出告诉用户特定字符数量的单词数量。

我想在完成之前向applet添加更多内容,但我不太清楚如何做这些事情。

首先,我希望applet能够区分单词和标点符号。因此,如果一个单词的末尾有一个感叹号,那么它将包含一个单词的长度。而且,我希望它能告诉用户applet窗口中的平均字长。

其次,我希望信息以条形图形式显示。意思是x轴告诉用户字长,而y轴告诉用户字频。原因很简单,因为我希望程序整洁。

最后,我不太确定是否有可能,但我希望用户可以选择将文件加载到applet中,applet对其中的文本执行单词计数文件。

到目前为止我的代码是:

    //package name
package org.main;

// Imports
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;


public class Main extends JApplet implements ActionListener, KeyListener {

 private static final long serialVersionUID = -1L;

 // our panel to contain the content
 private JPanel panel;

 // buttons and Text areas
 private JButton analyze, reset;
 private JTextArea input, output;
 private JLabel inputbox;

 // makes the given text areas have scrollbars allowing for large inputs
 private JScrollPane outputscrollpane, inputscrollpane;

 /**
  *  Constructor
  */
 public Main() { 

  // create a new panel to hold our content, and set the text area indent and add a key lister
  panel = new JPanel();
  UIManager.put("TextArea.margin", new Insets(10,10,10,10));
  this.addKeyListener(this);

  // add the panel to the applet, set it to be visible, and set it's size
  this.setContentPane(panel);
  this.setVisible(true);
  this.setSize(800, 600);

  // create the two buttons and add then to the action listener
     analyze = new JButton("Analyze");   analyze.addActionListener(this); 
     reset   = new JButton("Reset");      reset.addActionListener(this);

     // create the input label
     inputbox = new JLabel("Enter a Sentence: ");

     // create the input box, and set text wrap
     input   = new JTextArea(10, 20);
     input.setLineWrap(true);
     input.setWrapStyleWord(true);

     // add the input box to a scroll  panel
     inputscrollpane = new JScrollPane(input);

     // create the input box, and set text wrap and disable input
     output  = new JTextArea(30, 60);
     output.setEditable(false);
     output.setLineWrap(true);
     output.setWrapStyleWord(true);

     // add the output box to a scroll  panel
  outputscrollpane = new JScrollPane(output);

  // create the window layout controller and add it to the panel
  BorderLayout layout = new BorderLayout();
  panel.setLayout(layout);

  // add the buttons and boxes to the panel
  panel.add("North", inputbox);
     panel.add("Center", inputscrollpane);
     panel.add("East", analyze);
     panel.add("West", reset);
     panel.add("South", outputscrollpane);
 }

 /**
  *  Handles button action events
  */
 public void actionPerformed(ActionEvent e) {

  // if the analyse button was pressed clear the output and analyse the text
  if (e.getSource() == analyze) {
   output.setText(null);
   Analyze();
  }

  // if the reset button is press clear both text areas
  if (e.getSource() == reset) {
   output.setText(null);
   input.setText(null);
  }
 }


 /**
  *  private method that will count the word and their length
  */
 private void Analyze() {
  // Lets Begin


  // get the current text inside the input box
  String string = input.getText();

  // split the string where there is a space
  String[] parts = string.split(" ");

  // create an array that will hold the number of words at a set length
  int counter[] = new int[101];

  // loop through separate words and adding one to the place in the array 
  for (String word : parts) {
    counter[word.length()]++;  
  }

  // loop through the counter array where wwe have more then 0, print out the result inside the output box
  for(int i = 0; i < counter.length; i++ ){
            if(counter[i] > 0){
                output.append("There are " + counter[i] + " word(s) " + i + " letter(s) long." + '\n');
            }
        }   

 }

 // Main method, creates an instant of this class
 public static void main(String[] args) {
  new Main();
 }

 @Override
 public void keyPressed(KeyEvent e) {
  // setting keyboard shortcuts

  if (e.getKeyCode() == KeyEvent.VK_V && e.getModifiers() == KeyEvent.CTRL_MASK) {
   input.paste();
  }

  if (e.getKeyCode() == KeyEvent.VK_C && e.getModifiers() == KeyEvent.CTRL_MASK) {
   input.copy();
  }

  if (e.getKeyCode() == KeyEvent.VK_X && e.getModifiers() == KeyEvent.CTRL_MASK) {
   input.cut();
  }
 }

 @Override
 public void keyReleased(KeyEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void keyTyped(KeyEvent e) {
  // TODO Auto-generated method stub

 }


}
*

0 个答案:

没有答案