无法在动作侦听器(Jframe)中引用非变量

Posted

技术标签:

【中文标题】无法在动作侦听器(Jframe)中引用非变量【英文标题】:Cannot refer to non variable inside Action listener (Jframe) 【发布时间】:2013-03-27 07:00:14 【问题描述】:

以下程序的目标是要求用户输入一个电阻值,然后程序将输出每个数字对应的颜色。因此,这不包括所有数字。但是,该程序已经完成,我尝试将 JFrame 合并为一个额外的东西,除了我对如何在动作侦听器中打印相应的颜色感到困惑。

这一行调用了特定的方法,对于 3 位数字然后继续打印颜色,除了我如何将它和我的其余代码合并到我的 JFrame 和/或 ActionListener 中。行 System.out.println(array3[i]);/

当我尝试在动作监听器中简单地打印颜色值时,我确实收到了错误 “不能引用非最终变量”....

我曾尝试查看各种在线教程,甚至是 Java API 和指南,但都无济于事。总的来说,我似乎不知道如何将已经写入 JFrame 的代码合并到 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.*;


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


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



       

    );

    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;
  
 

【问题讨论】:

【参考方案1】:

在 Java 中,您不能在匿名内部类中引用非 final 变量。您尝试在ActionListener 的匿名实现中访问array3。您可以简单地将array3 更改为final,即:

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

另外,要打印数组的内容,您可以使用Arrays.toString()

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

【讨论】:

这会打印出 3 种颜色,除了打印出 (null,null,null) @SD 您实际上并没有在按钮操作处理程序中执行任何计算。你需要调用values()digitColours,就像你从BufferedReader 读取输入一样。我不知道你为什么把那段代码留在那里。 我怎么称呼他们,如果你能帮助我,我愿意给你打勾?

以上是关于无法在动作侦听器(Jframe)中引用非变量的主要内容,如果未能解决你的问题,请参考以下文章

java在动作监听器中引用类

TestNG 中的非静态驱动程序和屏幕截图侦听器

NetBeans 在设计视图中删除自动生成的动作侦听器

如何将侦听器添加到 osx 上的 java 停靠图标

无法添加键盘事件监听器动作脚本

如何在关键侦听器范围之外使用布尔值