2011-01-23 3 views
0

メインクラス(クラスファイル)を作成しました。私が心配しているように、それは適切に動作します。今、私はそのクラスファイルを起動するためのボタンを含むGUIを作成しています。 mainProgramクラスを実行するには、どのようにしてactionListenerのコードを記述しますか?GUI Jbuttonからメインクラスを実行するには?

これは、メインクラスです:

import java.io.*; 
import java.util.*; 

public class MainProgram 
{ 
    public static void main (String args[]) throws IOException 
{ 

//Declare variables    
Scanner in = new Scanner (System.in); // Scanner used for user input 
BufferedReader inputQuote; 
BufferedReader inputTheme; 
BufferedReader inputCharacterAnalysis; 
BufferedReader inputSignificance; 
BufferedReader inputPlotEnhancement; 
BufferedReader inputSpeaker; 

String [][] quotes; 
String text; 
int quoteSelection; 
int analysisSelection; 
int howMany=0; 
int count; 

    //Count the number of lines in a text file 
    FileReader fr = new FileReader ("CrucibleQuotations.txt"); 
    LineNumberReader ln = new LineNumberReader (fr); 
    while (ln.readLine() != null) 
    { 
    howMany++; 
    } 

    //import information from the text file 
    inputQuote = new BufferedReader (new FileReader ("CrucibleQuotations.txt")); 
    inputTheme = new BufferedReader (new FileReader("CrucibleTheme.txt")); 
    inputCharacterAnalysis = new BufferedReader (new FileReader("CrucibleCharacterAnalysis.txt")); 
    inputSignificance = new BufferedReader (new FileReader("CrucibleSignificance.txt")); 
    inputPlotEnhancement = new BufferedReader (new FileReader("CruciblePlotEnhancement.txt")); 
    inputSpeaker = new BufferedReader (new FileReader("CrucibleSpeaker.txt")); 

    //Create array based on how many quotes available in the text file 
    quotes = new String [howMany][6]; 

    //Store quote information in the array and display the list of quotations 
    for (int i=0; i<howMany; i++) 
    { 
    quotes [i][0] = inputQuote.readLine(); 
    System.out.println (i+1 + ") " + quotes [i][0]); 
    quotes [i][1] = inputTheme.readLine(); 
    quotes [i][2] = inputCharacterAnalysis.readLine(); 
    quotes [i][3] = inputSignificance.readLine(); 
    quotes [i][4] = inputPlotEnhancement.readLine(); 
    quotes [i][5] = inputSpeaker.readLine(); 
    } 

    //Ask user to choose the quote they want to analyze 
    System.out.println ("Choose a quotation by inputting the number. If the quotation you would like to analyse is unavailable, you may create your own by entering the number 0. ");  
    quoteSelection = in.nextInt(); 

    if (quoteSelection!=0) 
    { 
    //Show user selections to analyze 
    System.out.println ("Choose your analysis"); 
    System.out.println ("1. Theme"); 
    System.out.println ("2. Character Analysis"); 
    System.out.println ("3. Significance"); 
    System.out.println ("4. Plot Enhancement"); 
    System.out.println ("5. Speaker"); 
    analysisSelection = in.nextInt(); 

    //Display the analysis 
    if (analysisSelection <= 5 || analysisSelection > 0) 
    { 
     System.out.println (quotes [quoteSelection-1][analysisSelection]); 
    } 
    } 

これは

import javax.swing.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.BorderLayout; 
import java.awt.GridLayout; 

public class GUI 
{  
    public GUI() 
    {  
    JFrame frame = new JFrame ("Quotes"); 
    frame.setSize(500, 500); 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

    cButton.addActionListener (new ActionListener() { 
     public void actionPerformed (ActionEvent ae) { 
     try{ 
     MainProgram.main (new String[]); 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 
    }); 
    } 

public static void main (String [] args) 
{ 
    new GUI(); 
} 
} 

答えて

1

frame.pack(); 
frame.setVisible(true); 

たりのJFrameを呼び出すことを忘れないでください、私のGUIクラスになるには表示されません。 ActionListenerのコードについては

、次のような文字列配列の次元のために[0]を追加しました:はい、それは動作します

cButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      try { 
       MainProgram.main(new String[0]); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
+0

。ありがとうございました! – Mike