2013-04-05 26 views
1

以下程序的目標是要求用戶輸入一個電阻值,然後程序將爲每個數字輸出相應的顏色。因此這不包括所有數字。然而,該程序完成後,我試圖將JFrame作爲一個額外的東西進行整合,除非我掛斷瞭如何在動作偵聽器中打印相應的顏色。JFrame總是在動作監聽器中輸出變量爲空

但之前我問過這個問題,由於忘記輸入特定標籤而得到了有限的回覆。但是,用戶使用ArraysToString的答案實際上什麼也沒做,因爲程序仍然設法打印出null。

Cannot refer to non variable inside Action listener (Jframe) JFrame中下面的方法sopposed收集用於取決於位數,其中在動作監聽我試着簡單地打印出這些顏色的電阻器的每個顏色段信息,而是始終打印null(3次)

我試過查看各種在線教程,甚至是Java API和指南,其中沒有一個可以提供幫助。總的來說,我似乎沒有意識到如何將已經寫入JFrame的代碼合併到一起,無論這是一個乏味的過程,我願意公司化,並且非常感謝有關如何解決此困境的一些見解。

import java.io.*; 
import javax.swing.*; 
//import javax.swing.JFrame; 
//import javax.swing.JLabel; 
//import javax.swing.JButton; 
//import javax.swing.JPanel; 
//import javax.swing.JTextField; 
import java.awt.event.*; 
import java.util.Arrays; 

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 (Press the button to start the instructions"); 
    //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"}; 


    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); 

    //JTextField t = new JTextField(16); 


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

    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")); 

     final String [] array3 = new String [3]; 

     JOptionPane.showMessageDialog(null, "The Colors are : " + (Arrays.toString(array3))); 



     } 

    }); 

    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)); 

    } 

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

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

    //tollerance 
    holder = num.substring(3,4); 
    digit[3] = Integer.parseInt(holder); 
    holder2 = Integer.parseInt(num); 
    // checks to see if above 5 
    if(digit[3] < 5){ 
     digit[3] = digit[3]/holder2 * 100; 
    } 
     else if(digit[3] > 5){ 
     digit[3] = 10 - digit[3]; 
     digit[3] = digit[3]/holder2 * 100; 
    } 
    System.out.println(digit[3]); 

    //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); 
    } 

    //print out for information 
    /*System.out.println("The first digit is rounded to:" + (int)digit[0]); 
    System.out.println("The second digit is roudned to:" + (int)digit[1]);     
    System.out.println("The third digit is roudned to:" + (int)digit[2]); */ 
    /* */ 
    return new int[] {digit[0], digit[1],digit[2],digit[3]} ;// 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; 
    } 
} 

回答

2

的陣列,ARRAY3,永遠是零,因爲它已經宣佈道:

 final String[] array3 = new String[3]; 

     JOptionPane.showMessageDialog(null, 
       "The Colors are : " + (Arrays.toString(array3))); 

正如你可以從上面的代碼中看到的,它從來沒有被賦予任何類型的String對象所以默認爲null。您必須先填充它才能保存非空值。想象一個類似於蛋箱的物體陣列:除非你用雞蛋填充它,否則它總是空的。

我建議你讓任何想要傳入內部類的變量final。但必須是最終在類,而不是在類:

更重要的是,我會得到大多數所有代碼你的主要方法,走出靜態地和進入它所屬的實例地。然後你可以使用類字段,而不必擔心本地最終變量。這就是我認爲當前程序的主要弱點 - 太多的靜態代碼和沒有足夠的符合OOP的類和對象。

您的其他問題是,您似乎試圖將Swing GUI與控制檯程序混合,該控制檯程序通過GUI和標準通過BufferedReader獲取用戶輸入,這可能會導致災難。我強烈建議你選擇其中一個,在我看來,使用Swing GUI可能會更好,而不是嘗試混合這些不可混合的東西。

+0

把我的代碼從主方法中拿出來,就像把jframe放在它自己的方法中一樣,然後爲自己創建一個構造函數? – 2013-04-05 02:31:57

+0

@SD:類似的東西,但它不會那麼簡單或直接,我不認爲。你可能需要做很多重寫。 – 2013-04-05 02:33:03

+0

我會盡我所能,謝謝你的迴應,雖然這個JFrame是一個獎勵標記的東西,但它肯定值得通過做的麻煩。 – 2013-04-05 02:35:10