簡體   English   中英

如何使用NetBeans在Java中將主類鏈接到jframe表單

[英]how to link a main class to a jframe form in java using netbeans

美好的一天!

我使用Netbeans創建了一個代碼,它可以很好地執行進程。 現在,我希望給定輸入並通過用戶界面顯示輸出。 然后,我創建了2個Jframe,其中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有一個文本框和一個按鈕。

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

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

}   

我希望用戶在文本框中輸入數據,然后單擊“預測下一個單詞”按鈕,然后代碼執行的輸出將顯示在第二個jframe上,它具有3個標簽和相對文本區域。

注意; 我無法粘貼屏幕截圖,但是如果您運行NgramBetaE類,您將對接口的外觀有所了解,因為我試圖對其進行解釋。

謝謝

甚至不要嘗試將您的GUI代碼鏈接到您的NgramBetaE代碼,因為您需要做更多的工作,因為NgramBetaE僅僅是一種巨大的靜態主方法,該方法通過掃描儀從控制台獲取用戶輸入,並通過printlns。 融合這兩個就像試圖將一個方形的釘子插入一個圓孔。

而是着眼於面向對象的編碼來重寫整個過程,包括創建具有實例字段和方法的,與OOP兼容的模型類,以及用於獲取輸入並顯示它的單個GUI,其中包含模型的實例類,並在此實例上調用實例方法。


考慮為以下項目創建非GUI類和方法:

  • 從文本文件中讀取數據
  • 分析和散列文本文件中包含的數據,包括計算單詞頻率等...
  • 分析后以任何可能的數據形式返回所需的數據。
  • 一種允許輸入字符串/短語進行測試並返回其預測概率的方法

然后為以下內容創建GUI代碼:

  • 從用戶獲取選定的文本文件。 JFileChooser和支持的代碼在這里效果很好。
  • 按鈕開始分析
  • JTextField允許輸入短語
  • JTextArea或JTable可以顯示分析結果

請注意,您應該避免在GUI中使用多個JFrame。 有關更多信息,請查看“使用多個JFrame,良好/不良實踐?”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM