2009-12-02 69 views
-1

我想了解java的事件處理程序,並不斷收到我創建的類型類型(靜態/非靜態)方法的錯誤。一些代碼,我想寫的樣子:java靜態與非靜態使用此和事件處理程序

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 

public class Main extends JFrame implements ActionListener{ 

    static private int[] intArray = new int[10000]; 
    static private int numOfInts = 0; 
    static private int avg = 0; 

    public static void main(String[] args) { 

    //create main frame 
    JFrame frame = new JFrame(); 
    frame.setTitle("Section V, question 2"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(350, 250); 
    frame.setLayout(new GridLayout(4, 1)); 
    frame.setVisible(true); 

    //create instruction label and add to frame 
    Label instructions = new Label("Follow the instructions on the exam to use this program"); 
    frame.add(instructions); 

    //create textfield for index entry and add to frame 
    JTextField indexEntry = new JTextField(); 
    frame.add(indexEntry); 

    //create button for average and add to frame 
    JButton avgBtn = new JButton("Click for Average"); 
    frame.add(avgBtn); 
    avgBtn.addActionListener(avgBtn); 

    //create panel to display results and add to frame 
    JPanel resultsPanel = new JPanel(); 
    resultsPanel.setBackground(Color.BLUE); 
    frame.add(resultsPanel); 

    //read in from file 
    readFromFile(); 

    //compute average 
    computeAverage(); 

    System.out.println(avg); 

} 

static private void readFromFile(){ 
    try{ 
    // Open the file that is the first 
    // command line parameter 
    FileInputStream fstream = new FileInputStream("numbers.dat"); 
    // Get the object of DataInputStream 
    DataInputStream in = new DataInputStream(fstream); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 
    //Read File Line By Line 
    int i = 0; 
    while ((strLine = br.readLine()) != null) { 
     // Print the content on the console 
     System.out.println (strLine); 
     intArray[i] = Integer.parseInt(strLine); 
     numOfInts++; 
     i++; 
    } 
    //Close the input stream 
    in.close(); 
    System.out.println ("numOfInts = " + numOfInts); 
    } 
    catch (Exception e){//Catch exception if any 
    System.err.println("Error: " + e.getMessage()); 
    } 
} 

static private void computeAverage(){ 
    int sum = 0; 

    for(int i = 0; i < numOfInts; i++) 
    sum += intArray[i]; 

    avg = sum/numOfInts; 

    //return avg; 
} 

public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == avgBtn){ 
     computeAverage(); 
    } 
} 

} 

這應該建立在從文件中的一些整數讀GUI,然後當按下一個按鈕,計算它們的平均值。但是,我一直在遇到靜態/非靜態的東西和事件處理者的問題。我當前的錯誤是:
Main.java:35:javax.swing.AbstractButton中的addActionListener(java.awt.event.ActionListener)不能應用於(javax.swing.JButton)
avgBtn.addActionListener(avgBtn);

Main.java:91:找不到符號
符號:變量avgBtn
位置:類主要
如果(e.getSource()== avgBtn){

我明白,編譯器可以」 t找到avgBtn是因爲它在另一個函數(Main())中定義,但任何人都可以闡明如何將事件處理程序附加到它上面?試過'這個'也無濟於事......先謝謝了,如果你看到其他的錯誤,我很想聽聽我怎樣才能讓它變得更好。

回答

1

你的代碼有點混亂,編譯後會出現更多的語法錯誤。 您不應該混用swing/awt組件,例如:而不是使用Label在swing中使用JLabel,對於Panel使用JPanel。

注意swing的「J」前綴,如果您想了解更多關於Java(Swing)或甚至閱讀一些基本教程,您應該閱讀書籍。

除非您瞭解其用途,否則請勿使用靜態方法。

總之這裏是你想要的最接近代碼:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
public class Main extends JFrame implements ActionListener { 
    private int[] intArray = new int[10000]; 
    private int numOfInts = 0; 
    private int avg = 0; 

    protected JButton avgBtn; 
    protected JTextField indexEntry; 
    protected JLabel instructions; 
    protected JPanel resultsPanel; 

    //constructor - construct the components here and do the initializations 
    public Main(){ 
     //create main frame  
     this.setTitle("Section V, question 2"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setSize(350, 250); 
     this.setLayout(new GridLayout(4, 1)); 
     //this.setVisible(true); 

     //create instruction label and add to frame 
     instructions = new JLabel("Follow the instructions on the exam to use this program"); 
     this.add(instructions); 

     //create textfield for index entry and add to frame 
     indexEntry = new JTextField(); 
     this.add(indexEntry); 

     //create button for average and add to frame 
     avgBtn = new JButton("Click for Average"); 
     this.add(avgBtn); 
     avgBtn.addActionListener(this); 

     //create panel to display results and add to frame 
     resultsPanel = new JPanel(); 
     resultsPanel.setBackground(Color.BLUE); 
     this.add(resultsPanel); 

     //read in from file 
     readFromFile(); 

     //compute average 
     computeAverage(); 
     System.out.println(avg); 
    } 

    private void readFromFile() { 
     try { 
      // Open the file that is the first 
      // command line parameter 
      FileInputStream fstream = new FileInputStream("numbers.dat"); 
      // Get the object of DataInputStream 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      String strLine; 
      //Read File Line By Line 
      int i = 0; 
      while ((strLine = br.readLine()) != null) { 
       // Print the content on the console 
       System.out.println (strLine); 
       intArray[i] = Integer.parseInt(strLine); 
       numOfInts++; 
       i++; 
      } 
      //Close the input stream 
      in.close(); 
      System.out.println ("numOfInts = " + numOfInts); 
     } 
     catch (Exception e) { 
      //Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
     } 
    } 
    private void computeAverage() { 
     int sum = 0; 
     for (int i = 0; i < numOfInts; i++) 
     sum += intArray[i]; 
     avg = sum/numOfInts; 
     //return avg; 
    } 

    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == avgBtn) { 
      computeAverage(); 
     } 
    } 

    public static void main(String[] args) { 
     Main m = new Main(); 
     m.setVisible(true); 
    } 
} 
+0

這完美的作品。我想一個解決方案是創建一個靜態的main(),它調用你真正想要執行的東西?非常感謝... – danwoods 2009-12-02 03:15:27