2013-04-05 56 views
-2

下面的程序應該接收用戶輸入的4位顏色代碼電阻器,然後繼續在JFrame中打印它(目前已將它設置爲BufferedReader和JFrame)然而,我明白我不能簡單地將外部方法中的內容打印到內部方法中。例如,我試圖在輸入完成後在action listener中輸出array3,但是我得到了這個錯誤。不能引用非最終變量(陣列)

我試圖在主要方法中將某些變量設置爲final,在互聯網上搜索了各種頁面,但仍找不到解決我的特定問題的方法。我以前在這裏提過這個問題,然而,它導致更多的混淆,因爲試圖在自己的方法中重寫我的代碼JFrame導致接近混亂,因此我不得不返回到我的原始代碼。我想知道如何接受用戶輸入並將其傳遞給下面的方法在JFrame上,因爲我現在正在嘗試的東西當然不起作用。

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

public class test extends JFrame 
{ 
    public static void main (String [] args) throws IOException 
    { 
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in)); 

    //calling variables 
    String input; 
    int numInput; 

    JLabel l = new JLabel("Hello and welcome to the Program"); 
    l.setAlignmentX(0); 
    l.setAlignmentY(0); 

    //calling arrays 
    int [] array = new int [5]; 
    int [] array2 = new int [3]; 
    String [] array3 = new String [3]; 
    String[] colours = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"}; 
    String[] tollerance = {"red", "gold", "silver", "no band"}; 



    JFrame f = new JFrame("Hello JFrame"); 
    f.setSize(500,500); 
    //f.getContentPane().setBackground(Color.CYAN); 
    f.add(l); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setVisible(true); 
    f.setLocationRelativeTo(null); 
    //JTextField t = new JTextField(16); 

    /*SodaMachine machine = new SodaMachine(); //class name 
    JButton cokeButton = new JButton(); 
    cokeButton.addActionListener(new ActionListener() { 
    public void actionPerformed() { 
    machine.dispenseCoke(); 
    } 
    }); */ 



    JPanel p = new JPanel(); 
    JButton b = new JButton("Press me") ; 
    b.setAlignmentX(0); 
    b.setAlignmentY(0); 

    test x = new test(); 
    b.addActionListener(new ActionListener(){ 
    public void actionPerformed (ActionEvent e) { 
    JOptionPane.showMessageDialog(null,"In the following program you (The user!) will input a number of a resistor value \nThe program will pass the information into methods and then proceed to print out \nThe coorelating colors (Press the button to be asked for input)"); 
    int number = Integer.parseInt(JOptionPane.showInputDialog("Please enter the resistor value")); 

    JOptionPane.showMessageDialog(null, "The Colors are : " + array3[i]); 

    } 

    }); 

    p.add(b); 
    p.add(l); 
    //p.add(t); 
    f.add(p); 


    System.out.println("Hello and welcome to the Program (Press any key to con't)"); 
    input = myInput.readLine(); 

    System.out.println("In the following program you (The user!) will input a number of a resistor value"); 
    System.out.println("The program will pass the information into methods and then proceed to print out"); 
    System.out.println("The coorelating colors (Press any key to be asked for input)"); 
    input = myInput.readLine(); 

    System.out.println("Enter a resistor value (Note that resistors can only acount to 4 decimal places"); 
    input = myInput.readLine(); 
    numInput = Integer.parseInt (input); 


    //colours for values 
    array2 = values(array, input, colours); 
    for(int i = 0 ; i < 3; i++){ 
    array3[i] = digitColours(array2[i], colours); 
    System.out.println(array3[i]);// prints colours for values 
    } 

    //prints 4th colour for multiplier 
    System.out.println(decimalPlaces(input, colours)); 
    //tollerance band 
    System.out.println(tollerance(input, tollerance)); 

    } 

    public static int[] values (int [] digit, String num, String[] colours) 
    { 

    String holder; 
    double numHolder; 
    int lengthOfInput; 
    int holder2; 

    //Rounding of the input 
    lengthOfInput = num.length() - 3; 
    numHolder = Double.parseDouble(num); 
    numHolder = numHolder/(Math.pow(10,lengthOfInput)); 
    numHolder = (int)(Math.round(numHolder)+0.5); 

    // first three digits 
    for(int i = 0; i < 3; i++){ 
    holder = num.substring(i,i+1); 
    digit[i] = Integer.parseInt(holder); 
    } 

    return new int[] {digit[0], digit[1], digit[2]} ;// return 
    } 

    public static String digitColours(int decimalPlace, String[] colours){ 
    //calling additional variables 
    String answer; 
    answer = colours[decimalPlace]; 
    return answer; 
    } 


    //method to find the multiplier 
    public static String decimalPlaces(String input, String[] colours){ 
    //calling additional variables 
    int length = input.length(); 
    String answer; 

    length = length - 3; 
    answer = colours[length]; 

    return answer; 
    } 

    public static String tollerance(String num, String[] tollerance){ 

    String holder; 
    int holder2; 
    double perError; 

    //tollerance 
    holder = num.substring(3,4); 
    perError = Double.parseDouble(holder); 
    holder2 = Integer.parseInt(num); 
    // checks to see if above 5 
    if(perError < 5){ 
    perError = perError/holder2 * 100; 
    } 
    else if(perError > 5){ 
    perError = 10 - perError; 
    perError = perError/holder2 * 100; 
    } 

    //changing for colour 
    if(perError <= 2){ 
    perError = 0; 
    }else if(perError <= 5){ 
    perError = 1; 
    }else if(perError <= 10){ 
    perError = 2; 
    }else if(perError <= 20){ 
    perError = 3; 
    } 

    return tollerance[(int)perError]; 
    } 
    } 
+0

對不起,但你的班級聲稱是重構。 – 2013-04-05 23:08:06

+0

順便問一下,問題是你不能在local-method內部類中使用一個方法變量,至少它被聲明爲'final'。 – 2013-04-05 23:09:18

+3

[Aqua對你以前的問題的回答](http://stackoverflow.com/a/15824306/139010)很好地解釋了編譯錯誤。許多嘗試使用Swing的Java程序員都會遇到此錯誤消息。我強烈建議你將代碼縮減爲[SSCCE](http://sscce.org)。像這樣的基本編譯器錯誤問題,無休止地提出,除非以一般方式提出,有一定的潛力來幫助將來遇到類似問題的程序員,否則這些問題並不是非常有用。 – 2013-04-05 23:11:29

回答

6

您的短期問題是,您正在試圖對內部類中的方法使用非最終變量局部變量。這可以通過使用最終變量或類字段來解決(這裏,因爲它在靜態方法中使用,它必須是靜態的)。

不過話說回來了,你過的所有問題再次是,你已經得到的一切推到靜的主方法(或任何類型爲此事的靜態方法)。您絕對需要創建可用於創建對象的類。我相信我們之前最近討論過這個問題。


編輯

我改變了主意 - 你的「模式」,你的代碼的決定可能顯示可以由無非是靜態方法是什麼顏色的字符串。它可以有一個看起來像這樣的方法:

public class ResistorColorsUtility { 
    // create a String array, ALL_COLORS, as a constant that the methods 
    // of this class can use. 
    private static final String[] ALL_COLORS = { "black", "brown", "red", 
     "orange", "yellow", "green", "blue", "violet", "gray", "white" }; 

    public static String[] getColorsForValue(int value) { 
     // code in here will create an array of Strings for the value given and 
     // return it 

     // here use value int to find all the appropriate colors 
     // create String array called result to hold the color Strings 
     String[] result = ......; // you've got to create this code! 

     return result; // return the result 
    } 

    // it will likely have other public methods that the GUI could use 

    // and other private methods that it will only use internally 
    // and will not be used by other classes 
} 

GUI具有輸入和結果的顯示區域和一個按鈕,找出當用戶希望看到的結果。主要方法不過是讓程序運行,在這裏創建一個JFrame來將主GUI代碼的JPanel放入並顯示該JFrame。

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

// A very simple GUI to illustrate a point 
public class ResistorGui extends JPanel { 
    private JButton findColorButton = new JButton("Find Colors"); 
    private JTextField valueInputField = new JTextField(2); 
    private JTextField resultField = new JTextField(15); 

    public ResistorGui() { 
     // user shouldn't be able to edit result field 
     resultField.setEditable(false); 
     resultField.setFocusable(false); 

     findColorButton.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      // all program logic is in here 

      // get value String from valueInputField 
      // translate to int 
      // call utility method from ResistorColorsUtil class! 
      // display result in resultField 
     } 
     }); 

     add(new JLabel("Value:")); 
     add(valueInputField); 
     add(findColorButton); 
     add(new JLabel("Result:")); 
     add(resultField); 
    } 


    // all main does is to display the GUI, that's it 
    public static void main(String[] args) { 
     ResistorGui mainPanel = new ResistorGui(); 

     JFrame frame = new JFrame("ResistorGui"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 
} 
+0

我不完全確定如何創建這些類,以創建新的對象。 – 2013-04-05 23:10:35

+0

@SD:在嘗試編寫複雜程序(包括GUI)之前,通常事情的順序是學習如何創建類和對象。你可以在這裏瞭解它們:[Lesson:Object-Oriented Programming Concepts](http://docs.oracle.com/javase/tutorial/java/concepts/index.html)。 – 2013-04-05 23:44:51

+0

我實際上只是設法讓程序給我不同的錯誤,通過簡單地爲jframe創建另一個方法,而其他所有方法都轉到主要方法,這似乎更容易解決。錯誤只是聲明a​​rray3 [i]不能解析爲變量,我想我可以將它傳遞給新的非靜態方法並使其工作,除了Jframe不會只啓動常規輸入屏幕。 – 2013-04-05 23:59:58

相關問題