2016-05-08 3 views
-1

おはよう!netbeansを使用してJavaのメインフレームをjframeフォームにリンクする方法

私はNetbeansを使用してコードを作成しており、プロセスは正常に実行されます。 今、私は自分の入力を与え、出力をユーザーインターフェースを通して表示したい。私は2つのJframesを作成しました.1つはユーザーの入力を収集し、もう1つはコードの実行後に結果を表示します。

しかし、私はどのように私がそうすることができるのか分からないので、私はメインクラス(NgramBetaEと呼ばれる)にインターフェイスをリンクすることができません。

私は非常に歓迎する提案。

メインクラスの全体は次のとおりです。

package ngrambetae; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.*; 


/** 
* 
* @author 201102144 
*/ 
public class NgramBetaE { 



static LinkedList<String> allWords = new LinkedList<String>(); 
static LinkedList<String> distinctWords = new LinkedList<String>(); 
static String[] hashmapWord = null; 
static int wordCount; 
public static HashMap<String,HashMap<String, Integer>> hashmap = new HashMap<>(); 
public static HashMap<String,HashMap<String, Integer>> bigramMap = new HashMap<>(); 


/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 


    //prompt user input 
    Scanner input = new Scanner(System.in); 

    //read words from collected corpus; a number of .txt files 
      File directory = new File("Corpus"); 
      File[] listOfFiles = directory.listFiles();//To read from all listed iles in the "directory" 

      int lineNumber = 0; 
      String line; 
      String files; 
      String delimiters = "[()?!:;,.\\s]+"; 

      //reading from a list of text files 
      for (File file : listOfFiles) { 
       if (file.isFile()) { 
        files = file.getName(); 
        try { 
         if (files.endsWith(".txt") || files.endsWith(".TXT")) { //ensures a file being read is a text file 
         BufferedReader br = new BufferedReader(new FileReader(file)); 


         while ((line = br.readLine()) != null) { 
           line = line.toLowerCase(); 

           hashmapWord = line.split(delimiters); 


           //CALCULATING UNIGRAMS         
           for(int s = 0; s < hashmapWord.length; s++){ 

            String read = hashmapWord[s]; 

            allWords.add(read); 

            //count the total number of words in all the text files combined 
            //TEST 
            wordCount = 0;                 
             for (int i = 0; i < allWords.size(); i++){ 
             wordCount ++;           
           } 

           } 

           //CALCULATING BIGRAM FREQUENCIES 
           for(int s = 0; s < hashmapWord.length -1; s++){ 

            String read = hashmapWord[s]; 
            final String read1 = hashmapWord[s + 1]; 

            HashMap<String, Integer> counter = bigramMap.get(read); 
             if (null == counter) { 
              counter = new HashMap<String, Integer>(); 
              bigramMap.put(read, counter); 
             } 
             Integer count = counter.get(read1); 
             counter.put(read1, count == null ? 1 : count + 1); 


           } 

           //CALCULATING TRIGRAM FREQUENCIES 
           for(int s = 0; s < hashmapWord.length - 2; s++){ 

            String read = hashmapWord[s]; 
            String read1 = hashmapWord[s + 1]; 
            final String read2 = hashmapWord[s + 2]; 

            String readTrigrams = read + " " + read1; 

            HashMap<String, Integer> counter = hashmap.get(readTrigrams); 
             if (null == counter) { 
              counter = new HashMap<String, Integer>(); 
              hashmap.put(readTrigrams, counter); 
             } 
             Integer count = counter.get(read2); 
             counter.put(read2, count == null ? 1 : count + 1); 

           } 

         } 
          br.close(); 

         } 
        } catch (NullPointerException | IOException e) { 
         e.printStackTrace(); 
         System.out.println("Unable to read files: " + e); 
        } 

      } 
      } 


      //COMPUTING THE TOTAL NUMBER OF WORDS FROM ALL THE TEXT FILES COMBINED 
      System.out.println("THE TOTAL NUMBER OF WORDS IN COLLECTED CORPUS IS : \t" + wordCount + "\n"); 

       for(int i = 0, size = allWords.size(); i < size; i++){ 
        String distinctWord = allWords.get(i); 

        //adding a word into the 'distinctWords' list if it doesn't already occur     
        if(!distinctWords.contains(distinctWord)){ 
         distinctWords.add(distinctWord); 

        } 
       }      


      //PRINTING THE DISTINCT WORDS 
      System.out.println("THE DISTINCT WORDS IN TOTAL ARE :\t " + distinctWords.size() + "\n"); 


      System.out.println("PRINTING CONTENTS OF THE BIGRAMS HASHMAP... "); 
      System.out.println(bigramMap); 
      System.out.println("================================================================================================================================================================================================================================================================================================================\n"); 



      System.out.println("PRINTING CONTENTS OF THE TRIGRAMS HASHMAP... "); 
      System.out.println(hashmap); 
      System.out.println("================================================================================================================================================================================================================================================================================================================\n"); 


     //QUITTING APPLICATION 

     String userInput = null; 
     while(true) { 

       System.out.println("\n**********************************************************************************************************************************************************************************************************************************"); 
       System.out.println("\n\n\t\tPLEASE ENTER A WORD OR PHRASE YOU WOULD LIKE A PREDICTION OF THE NEXT WORD FROM:"); 
       System.out.println("\t\t\t\t(OR TYPE IN 'Q' OR 'q' TO QUIT)"); 

       userInput = input.nextLine();      
        if (userInput.equalsIgnoreCase("Q")) break; 

        //FORMAT USER INPUT 
        String[] users = userInput.toLowerCase().split("[?!,.\\s]+"); 
        if (users.length < 2) { 
         userInput = users[0]; 

         //System.out.println("\nENTRY '" + userInput + "' IS TOO SHORT TO PREDICT NEXT WORD. PLEASE ENTER 2 OR MORE WORDS"); 

         //CALCULATING BIGRAM PROBABILITY 

         int sum = 0; 
         try { 
          for(String s : bigramMap.get(userInput).keySet()) { 
           sum += bigramMap.get(userInput).get(s); 
          } 


          String stringHolder = null; 
          double numHolder = 0.0; 
          for(String s : bigramMap.get(userInput).keySet()) { 
           //System.out.println("TWO"); 
           double x = Math.round(bigramMap.get(userInput).put(s, bigramMap.get(userInput).get(s))/ (double)sum *100); 

           if(s != null){ 
            if(numHolder < x){ 

             stringHolder = s; 
             numHolder = x; 

            } 

           } 

          } 
          System.out.println("\nNEXT WORD PREDICTED IS '" + stringHolder + "'"); 
          System.out.println("ITS PROBABILITY OF OCCURRENCE IS " + numHolder + "%"); 

         } catch (Exception NullPointerException) { 
          System.out.println("\nSORRY. MATCH NOT FOUND."); 
         } 

        } else { 
         userInput = users[users.length - 2] + " " + users[users.length - 1]; 



//      System.out.println("FROM USER WE GET...."); 
//      System.out.println(bigrams.get(userInput).keySet()); 

        /* CALCULATING TRIGRAM PROBABILITY*/ 
         int sum = 0; 
         try { 
          for(String s : hashmap.get(userInput).keySet()) { 
           sum += hashmap.get(userInput).get(s); 
          } 

          String stringHolder = null; 
          double numHolder = 0.0; 
          for(String s : hashmap.get(userInput).keySet()) { 
           //System.out.println("TWO"); 
           double x = Math.round(hashmap.get(userInput).put(s, hashmap.get(userInput).get(s))/ (double)sum *100); 

           if(s != null){ 
            if(numHolder < x){ 

             stringHolder = s; 
             numHolder = x; 

            } 

           } 

          } 
          System.out.println("\nNEXT WORD PREDICTED IS '" + stringHolder + "'"); 
          System.out.println("ITS PROBABILITY OF OCCURRENCE IS " + numHolder + "%"); 

         } catch (Exception NullPointerException) { 
          System.out.println("\nSORRY. MATCH NOT FOUND."); 
         } 
        } 
      } 

     input.close(); 
} 

    } 

プロジェクトの実行時に最初に表示するJframeには、1つのテキストボックスと1つのボタンがあります。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    String usersInput = jTextField1.getText(); 

    Interface1 s = new Interface1(); 
    s.setVisible(true); 
    dispose(); 

} 

ユーザーがテキストボックスにデータを入力し、彼らはボタンをクリックすると、コードの実行からの出力は3つのラベルを持っていた第2 JFrameの上に表示される「次の単語を予測」するために私が希望相対テキスト領域。

注記:私はスクリーンショットを貼り付けることができませんでしたが、NgramBetaEクラスを実行すると、それらを説明しようとしたときにインターフェイスがどのようになるかを知ることができます。

はありがとう

+0

私の答えについてご質問がある場合は、コメントを投稿してください。 –

答えて

2

も、あなたはNgramBetaEからユーザー入力を取得する少しつ以上の巨大な静的なmainメソッドであるため、実行するより多くの仕事をしたとして、あなたのNgramBetaEコードにあなたのGUIのコードをリンクしようとしないでくださいScannerでコンソールを開き、printlns経由でコンソールに出力します。これら2つを溶かすことは丸い穴に四角いペグを入れようとするようなものです。

代わりに、インスタンス指向のフィールドとメソッドを持つOOP準拠のモデルクラスと、入力を取得して表示する単一のGUIを含む、オブジェクト指向のコーディングに目を向けるように全体を書き直します。モデルクラスのインスタンスであり、このインスタンスのインスタンスメソッドを呼び出します。単語の出現頻度などを計算することを含むテキスト・ファイルに保持されているデータを

  • 分析してテキストファイルからのデータに

    • 読書およびハッシュ -


      ための非GUIクラスとメソッドを作成することを検討して..

    • 必要なデータ形式で分析後に必要なデータを返す。

    はその後のためにGUIのコードを作成し、その予測確率リターンで、テスト用文字列/語句の入力を可能にするための方法であって

    • は、ユーザからテキストファイルを選択しましょう。 JFileChooserとサポートコードはここでうまくいきます。
    • フレーズ
    • JTextAreaのか分析
    • の結果、あなたのGUIで複数のJFrameを避けるべきであると

    ノートを表示するために、おそらくJTableのの入力が可能

  • のJTextFieldを解析を開始するためのボタン。詳細については、こちらをご覧くださいThe Use of Multiple JFrames, Good/Bad Practice?

  • 関連する問題