java - 我可以从 Action 监听器内部在类中设置数组吗? java

标签 java string comparison textarea textmatching

基本上我需要做的就是从每个按钮监听器中获取 array1 和 array2,但我不知道如何执行此操作。现在 TextMatch 按钮不起作用,因为 array1 和 array2 为空,是有没有办法从按钮监听器类中设置它们? 我需要的只是一种将 array1 和 array2 设置为 ss 和 sstwo 的方法,这样我就可以实现我的 TextTools.match() 方法

主要程序:

    import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
import javax.swing.JComboBox;


public class Lab11 extends JPanel
{
  private SuperString [] array1, array2;
  private StringBuilder string, string2, string3, string4; //loads SuperStrings faster by appending all at once
  private JRadioButton occurrence, alphabetical;
  private JPanel text;
  private JComboBox<Integer> input;
  private JLabel label, file1,file2, unique, unique2,textmatch;
  private JButton load, go,go2, TextMatch;
  private CountLinkedList<SuperString> words, words3; //Change impliments CountList to extends BinaryCountTree
  private OrderedLinkedList<SuperString> words2, words4;//Change impliments CountList to extends BinaryCountTree
  private String filename,filename2;
  private int width = 450;
  private int height = 550;
  private TextArea textarea,textarea2;
  Scanner scan;

  public Lab11()
  {
    TextListener textlistener = new TextListener();
    ButtonListener listener = new ButtonListener();
    Button2Listener listener2 = new Button2Listener();
    Integer [] select = {1,2,3,4};
    input = new JComboBox<Integer>(select);
    text = new JPanel(new GridLayout(1,2));
    go = new JButton("Select Text File 1: ");
    go2 = new JButton("Select Text File 2: ");
    TextMatch = new JButton("TextMatch");
    textmatch = new JLabel("");
    label = new JLabel("N: " );
    unique = new JLabel("");
    unique2 = new JLabel("");
    file1 = new JLabel("");
    file1.setFont(new Font("Helvetica",Font.PLAIN,24));
    unique.setFont(new Font("Helvetica",Font.PLAIN,24));
    file2 = new JLabel("");
    file2.setFont(new Font("Helvetica",Font.PLAIN,24));
    unique2.setFont(new Font("Helvetica",Font.PLAIN,24));

    occurrence= new JRadioButton("Occurrence");
    occurrence.setMnemonic(KeyEvent.VK_O);
    occurrence.addActionListener(listener);
    occurrence.addActionListener(listener2);
    occurrence.setSelected(true);


    alphabetical = new JRadioButton("Alphabetical");
    alphabetical.setMnemonic(KeyEvent.VK_A);
    alphabetical.addActionListener(listener);
    alphabetical.addActionListener(listener2);

    ButtonGroup group = new ButtonGroup();
    group.add(occurrence);
    group.add(alphabetical);

    go.addActionListener(listener);
    go2.addActionListener(listener2);
    input.addActionListener(listener);
    input.addActionListener(listener2);
    TextMatch.addActionListener(textlistener);

    textarea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
    textarea2 = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
    textarea.setFont(new Font("Helvetica",Font.PLAIN,24));
    textarea2.setFont(new Font("Helvetica",Font.PLAIN,24));
    textarea.setPreferredSize(new Dimension(width,height));
    textarea2.setPreferredSize(new Dimension(width,height));
    setPreferredSize(new Dimension(1000,700));
    text.add(textarea);
    text.add(textarea2);
    add(occurrence);
    add(alphabetical);
    add(label);
    add(input);
    add(go);
    add(file1);
    add(unique);
    add(go2);
    add(file2);
    add(unique2);
    add(TextMatch);
    add(textmatch);

    add(text);

    textarea.setText("No File Selected");
    textarea2.setText("No File Selected");


  }
    public class ButtonListener implements ActionListener //makes buttons do things
    {
      JFileChooser chooser = new JFileChooser("../Text");

      public void actionPerformed(ActionEvent event)
      {
       Integer N = input.getSelectedIndex()+1;
       string = new StringBuilder();
       string2 = new StringBuilder();


        if(event.getSource() == go)
      {

        int returnvalue = chooser.showOpenDialog(null);

    if(returnvalue == JFileChooser.APPROVE_OPTION)
    {
      try
      {
        File file = chooser.getSelectedFile();
        String text1= file.getName();
        file1.setText(text1);
        filename = file.getName();
        System.err.println(filename);
        scan = new Scanner(file);

      }
      catch (IOException e)
      {
        System.err.println("IO EXCEPTION");
        return;
      }       
    }
    else
    {
      return;
    }


     String[] storage = new String[N];
    words = new CountLinkedList<SuperString>();
    words2 = new OrderedLinkedList<SuperString>();
   for(int i=1;i<N;i++)
     storage[i] = scan.next().toLowerCase().replaceAll("[^A-Za-z0-9]", "")
                  .replaceAll("[.,':]","");

    while(scan.hasNext())
    {
      for(int i=0;i<=N-2;i++)
        storage[i] = storage[i+1];
      storage[N-1] = scan.next().toLowerCase();
      storage[N-1] = storage[N-1].replace(",","").replace(".","").replaceAll("[^A-Za-z0-9]", "")
                  .replaceAll("[.,':]","");
      SuperString ss = new SuperString(storage);
      SuperString ss2= new SuperString(storage);
      words.add(ss );
      words2.add(ss2 );
    }
        }
    SuperString[] array1 = new SuperString[words.size()];
    SuperString[] ss = new SuperString[words.size()];
    SuperString[] ss2 = new SuperString[words2.size()];
    int i=0;
    int count =0, count2= 0;

    for(SuperString word: words)
    {
      ss[i] = word;
      array1[i] = word;
      i++;
    }
    int j=0;
    for(SuperString word: words2)
    {
      ss2[j] = word;
      j++;
    }


    Arrays.sort(ss, new SuperStringCountOrder());

    for(SuperString word : ss)
    {
      count++;
      string.append(Integer.toString(count)+ "       "+ word+ "\n");
    }
    if(occurrence.isSelected())
    {

      textarea.setText("");
     textarea.append("    "+filename+" has wordcount: "+words.size()+
      "\n-------------------------\n\n");
     textarea.append(string.toString());
      string.replace(0,string.length()*2, "");
    }


     for(SuperString word : ss2)
    {
       count2++;
       string2.append(Integer.toString(count2)+ "       "+ word.toString()+ "\n");  
    }
         if(alphabetical.isSelected())
    {

      textarea.setText("");
      textarea.append("    "+filename+" has wordcount: "+words.size()+
      "\n-------------------------\n\n");
      textarea.append(string2.toString());
      string2.replace(0,string2.length()*2, "");

    }
     unique.setText("Unique Count: "+ Integer.toString(words.size()));

      }
    }


    public class Button2Listener implements ActionListener
    {
       JFileChooser chooser = new JFileChooser("../Text");


      public void actionPerformed(ActionEvent event)
      {
       Integer N = input.getSelectedIndex()+1;
        string3 = new StringBuilder();
        string4 = new StringBuilder();

         if(event.getSource() == go2)
      {

        int returnvalue = chooser.showOpenDialog(null);

    if(returnvalue == JFileChooser.APPROVE_OPTION)
    {
      try
      {
        File file = chooser.getSelectedFile();
        String text2= file.getName();
        file2.setText(text2);
        filename2 = file.getName();
        System.err.println(filename);
        scan = new Scanner(file);

      }
      catch (IOException e)
      {
        System.err.println("IO EXCEPTION");
        return;
      }       
    }
    else
    {
      return;
    }

     String[] storage = new String[N];
    words3 = new CountLinkedList<SuperString>();
    words4 = new OrderedLinkedList<SuperString>();
   for(int i=1;i<N;i++)
     storage[i] = scan.next().toLowerCase().replace(",","").replace(".","");

    while(scan.hasNext())
    {
      for(int i=0;i<=N-2;i++)
        storage[i] = storage[i+1];
      storage[N-1] = scan.next().toLowerCase();
      storage[N-1] = storage[N-1].replace(",","").replace(".","").replaceAll("[^A-Za-z0-9]", "")
                  .replaceAll("[.,':]","");
      SuperString ss = new SuperString(storage);
      SuperString ss2= new SuperString(storage);
      words3.add(ss );
      words4.add(ss2 );
    }
    textarea2.setText("");
         }
    SuperString[] array2 = new SuperString[words3.size()];
    SuperString[] sstwo = new SuperString[words3.size()];
    SuperString[] ss2two = new SuperString[words4.size()];
    int i=0;
    int count =0, count2= 0;

    for(SuperString word2: words3)
    {
      sstwo[i] = word2;
      array2[i] = word2;
      i++;
    }

    int j=0;
    for(SuperString word2: words4)
    {
      ss2two[j] = word2;
      j++;
    }  

    Arrays.sort(sstwo, new SuperStringCountOrder());

        for(SuperString word2 : sstwo)
    {
      count++;
      string3.append(Integer.toString(count)+ "       "+ word2+ "\n");
    }
    if(occurrence.isSelected())
    {
     textarea2.setText("");
     textarea2.append("    "+filename2+" has wordcount: "+words3.size()+
      "\n-------------------------\n\n");
     textarea2.append(string3.toString());
    }

     for(SuperString word2 : ss2two)
    {
       count2++;
        string4.append(count2+"   "+ "  "+word2+"\n");     
    }
     if(alphabetical.isSelected())
          {
       textarea2.setText("");
       textarea2.append("    "+filename2+" has wordcount: "+words3.size()+
      "\n-------------------------\n\n");
       textarea2.append(string4.toString());
       }

     unique2.setText("Unique Count: "+ Integer.toString(words3.size()));

      }
    }

    public class TextListener implements ActionListener
    {
      public void actionPerformed(ActionEvent event)
      {
       if(event.getSource() ==TextMatch)
    {
      textmatch.setText("The match is: "+ TextTools.match(array1,array2));
    }
      }
    }



   public static void main(String[] arg)
  {
    JFrame frame = new JFrame("Lab 11");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame.getContentPane().add(new Lab11());
    frame.setResizable(false);
    frame.pack();
    frame.setVisible(true);
  }
}

文本匹配程序:

import java.util.Arrays;

public class TextTools
{
   public static double  match(SuperString[] X, SuperString[]  Y)
  {
    double length1 = 0, length2 = 0;

     Arrays.sort(X);
     Arrays.sort(Y);
     for(SuperString token : X)
     { 
       double value = token.count();
       length1 += value*value;
     }
     length1 = Math.sqrt(length1);

     for(SuperString token : Y)
     { 
       double value = token.count();
       length2 += value*value;
     }
     length2 = Math.sqrt(length2);
     double total = 0;
     int j=0;
     for(int i=0;i<X.length;i++)
     {
       while(j < Y.length && X[i].compareTo(Y[j])>0 )
         j++;
       if(j == Y.length)
         return total/(length1*length2);
       if(X[i].compareTo(Y[j]) == 0)
          total += X[i].count()*Y[j].count();
     }
      return total/(length1*length2);
   }
}

最佳答案

事实证明,我通过在 Action 监听器中重新声明 SuperString 来删除 array1 和 array2

关于java - 我可以从 Action 监听器内部在类中设置数组吗? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27363717/

相关文章:

java - 为什么如果比较在 Java 中不起作用

java - 尝试使用 Task 更新 JavaFX 场景图,在调用任务对象外部的事件处理程序时出现错误

Java - 错误非默认 namespace 无法映射到 XML 1.0 文档中的空 URI

java - 检查文件是否已完全写入

c - 在 C 中打印字符串的所有排列

java - 字符串比较时的错误结果

java - 如何使用Jzy3d java 3d图表库?

C 字符串作为链表?

c++ - 如何 strcpy 并返回复制的字符数?

javascript - 从 jQuery 对象派生 HTMLElement 对象