不能引用非变量动作监听器里(JFrame的) [英] Cannot refer to non variable inside Action listener (Jframe)

查看:152
本文介绍了不能引用非变量动作监听器里(JFrame的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这行调用的具体方法,为3位然后继续打印颜色,除了我怎么结合这一点,我的code的其余部分进入我的JFrame,然后或ActionListener的。该生产线的System.out.println(ARRAY3 [I]); /

我收到错误试图只需在操作侦听器打印出的颜色值时,
不能指非final的变量......

我已经尝试了在线观看各种教程,甚至的Java API和准则,其中没有一个能有所帮助。总的来说,我似乎不知道如何把一个已经写入的JFrame,无论它是一个繁琐的过程,我愿意为企业和将是如何解决这个predicament一些有识之士很感激code。

 进口java.io. *;
进口的javax.swing *。
//进口javax.swing.JFrame中;
//进口javax.swing.JLabel中;
//进口javax.swing.JButton中;
//进口javax.swing.JPanel中;
//进口javax.swing.JTextField中;
java.awt.event中导入*。
公共类测试扩展的JFrame
{
  公共静态无效的主要(字串[] args)抛出IOException异常
  {
    的BufferedReader myInput =新的BufferedReader(新的InputStreamReader(System.in));
    //调用变量
    字符串输入;
    INT numInput;    JLabel的L =新的JLabel(您好,欢迎光临本程序(preSS按钮启动的说明);
    //l.setAlignmentX(0);
    // l.setAlignmentY(0);    //调用数组
    INT []数组=新INT [5];
    INT []数组2 =新INT [3];
    的String [] = ARRAY3新的String [3];
    的String [] =颜色{黑色,棕色,红,橙色,黄,绿,蓝,紫罗兰,灰色,白};
    JFrame的F =新的JFrame(你好JFrame的);
    f.setSize(500,500);
    //f.getContentPane().setBackground(Color.CYAN);
    f.add(升);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(真);    // JTextField的T =新的JTextField(16);
    JPanel的P =新JPanel();
    JButton的B =的新的JButton(preSS我);
    // b.setAlignmentX(0);
    // b.setAlignmentY(0);    b.addActionListener(新的ActionListener(){
      公共无效的actionPerformed(ActionEvent的五){
        JOptionPane.showMessageDialog(NULL,在下面的程序你(用户!)将输入数字的电阻值,\\ n该程序将信息传递到方法,然后继续打印出\\ n此coorelating颜色($ P $的PSS按钮被要求输入));
        INT数=的Integer.parseInt(JOptionPane.showInputDialog(请输入电阻值));
        JOptionPane.showMessageDialog(NULL,颜色是:+(ARRAY3 [I] +\\ n));      }    });    p.add(二);
    p.add(升);
    //p.add(t);
    f.add(P);
    的System.out.println(您好,欢迎光临本程序(preSS任意键续));
    输入= myInput.readLine();    的System.out.println(在下面的程序你(用户)将输入一个号码的电阻值的!);
    的System.out.println(该方案将信息传递到方法,然后继续打印);
    的System.out.println(以下简称coorelating颜色(preSS任意键被要求输入));
    输入= myInput.readLine();    的System.out.println(输入的电阻值(注意,电阻器只能acount到4位小数);
    输入= myInput.readLine();
    numInput =的Integer.parseInt(输入);    //颜色值
    数组2 =值(数组,输入,颜色);
    的for(int i = 0;我3;;我++){
      ARRAY3 [I] = digitColours(数组2 [I]中,颜色);
      的System.out.println(ARRAY3 [I]); //输出颜色值
    }
    //输出4色倍增
    的System.out.println(小数位数(输入,颜色));  }  公共静态INT []值(INT []数字,字符串NUM,字符串[]颜色)
  {    串持有人;
    双numHolder;
    INT lengthOfInput;
    INT holder2;    // tollerance
    支架= num.substring(3,4);
    位[3] =的Integer.parseInt(支架);
    holder2 =的Integer.parseInt(NUM);
    //检查是否超过5
    如果(位[3]小于5){
      位[3] =位[3] / holder2 * 100;
    }
    否则如果(位[3]→5){
      位[3] = 10 - 位[3];
      位[3] =位[3] / holder2 * 100;
    }
    的System.out.println(位[3]);    输入的//四舍五入
    lengthOfInput = num.length() - 3;
    numHolder = Double.parseDouble(NUM);
    numHolder = numHolder /(Math.pow(10,lengthOfInput));
    numHolder =(INT)(Math.round(numHolder)+0.5);    //前三位
    的for(int i = 0;我3;;我++){
      支架= num.substring(I,I + 1);
      位[I] =的Integer.parseInt(支架);
    }    //打印出的信息
    /*System.out.println(\"The第一数字被四舍五入为:+(int)的数字[0]);
     的System.out.println(第二位数字roudned为:[1] +(int)的数字);
     的System.out.println(第三数位roudned到:+(int)的数字[2]); * /
    / * * /
    返回新INT [] {位[0],位[1],位[2],位[3]}; //返回
  }
  公共静态字符串digitColours(INT decimalPlace,字符串[]颜色){
    //调用额外的变量
    字符串的答案;
    答案=颜色[decimalPlace]
    返回的答案;
  }
  //方法找到乘数
  公共静态字符串小数位数(输入字符串,字符串[]颜色){
    //调用额外的变量
    INT长度= input.length();
    字符串的答案;    长度=长度 - 3;
    答案=颜色[长度]    返回的答案;
  }
}


解决方案

在Java中,你不能引用匿名内部类中不是最终的变量。您尝试访问 ARRAY3 匿名执行的ActionListener 内部。你可以简单地更改 ARRAY3 来最终决定,即:

 最终的String [] = ARRAY3新的String [3];

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

  JOptionPane.showMessageDialog(NULL,颜色是:+(Arrays.toString(ARRAY3)));

The following program's goal is to ask the user to input a resistor value, then of which the program will output the corresponding colors for each digit. This thus not include all the digits. However, that program is done, I've made an attempt to incorporate JFrame as an extra thing, except I am hung up on how to print the corresponding colors in the action listener.

This line calls the specific methods, for the 3 digits then proceeds to print the colors, except how do I incorporate that and the rest of my code into my JFrame, and or ActionListener. The line System.out.println(array3[i]);/

I do receive the errors when trying to simply print the color values in the action listener "Cannot refer to a non-final variable"....

I have tried viewing various tutorials online, and even the Java API and guidelines, none of which could help. In general I seem unaware of how to incorporate code that is already written into JFrame, whether it's a tedious process, I am willing to corporate and would be very grateful for some insight on how to tackle this predicament.

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

解决方案

In Java you cannot refer to not final variables inside anonymous inner class. You attempt to access array3 inside anonymous implementation of ActionListener. You can simply change array3 to final, ie:

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

Also, to print a content of array you can use Arrays.toString():

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

这篇关于不能引用非变量动作监听器里(JFrame的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆