2013-04-05 27 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