Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame始终为action listener中的变量打印空值_Java_Arrays_Swing_Jframe_Actionlistener - Fatal编程技术网

Java JFrame始终为action listener中的变量打印空值

Java JFrame始终为action listener中的变量打印空值,java,arrays,swing,jframe,actionlistener,Java,Arrays,Swing,Jframe,Actionlistener,以下程序的目标是要求用户输入一个电阻值,然后程序将输出每个数字对应的颜色。因此,这并不包括所有数字。然而,该程序已经完成,我已经尝试将JFrame作为一个额外的东西纳入其中,除了如何在action listener中打印相应的颜色之外 我以前问过这个问题,但由于忘记输入特定的标签,所以得到的答复有限。然而,用户回答使用ArraysToString实际上什么都没有做,因为程序仍然能够打印null JFrame下面的方法用于根据数字收集电阻器每个色带的信息,在action listener中,我尝

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

我以前问过这个问题,但由于忘记输入特定的标签,所以得到的答复有限。然而,用户回答使用ArraysToString实际上什么都没有做,因为程序仍然能够打印null

JFrame下面的方法用于根据数字收集电阻器每个色带的信息,在action listener中,我尝试简单地打印这些颜色,但它总是打印空(3次)

我曾尝试在线查看各种教程,甚至是JavaAPI和指南,但都没有任何帮助。总的来说,我似乎不知道如何将已经编写的代码合并到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;
  }
} 
import java.io.*;
导入javax.swing.*;
//导入javax.swing.JFrame;
//导入javax.swing.JLabel;
//导入javax.swing.JButton;
//导入javax.swing.JPanel;
//导入javax.swing.JTextField;
导入java.awt.event.*;
导入java.util.array;
公共类测试扩展了JFrame
{
公共静态void main(字符串[]args)引发IOException
{
BufferedReader myInput=新的BufferedReader(新的InputStreamReader(System.in));
//调用变量
字符串输入;
int numInput;
JLabel l=新的JLabel(“您好,欢迎来到本程序(按下按钮以启动说明”);
//l、 setAlignmentX(0);
//l.setAlignmentY(0);
//调用数组
int[]数组=新的int[5];
int[]array2=新int[3];
字符串[]数组3=新字符串[3];
字符串[]颜色={“黑色”、“棕色”、“红色”、“橙色”、“黄色”、“绿色”、“蓝色”、“紫色”、“灰色”、“白色”};
JFrame f=新JFrame(“你好JFrame”);
f、 设置大小(500500);
//f、 getContentPane().setBackground(Color.CYAN);
f、 加(l);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 setVisible(真);
//JTextField t=新的JTextField(16);
JPanel p=newjpanel();
JButton b=新JButton(“按我”);
//b.setAlignmentX(0);
//b.setAlignmentY(0);
b、 addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
JOptionPane.showMessageDialog(null,“在以下程序中,您(用户!)将输入一个电阻值的数值\n程序将把信息传递到方法中,然后继续打印出相关颜色(按下按钮要求输入)”;
int number=Integer.parseInt(JOptionPane.showInputDialog(“请输入电阻值”);
最终字符串[]数组3=新字符串[3];
showMessageDialog(null,“颜色为:”+(Arrays.toString(array3)));
} 
});
p、 添加(b);
p、 加(l);
//p、 加(t);
f、 加(p);
System.out.println(“您好,欢迎来到这个程序(按任意键都可以)”;
input=myInput.readLine();
System.out.println(“在下面的程序中,您(用户!)将输入一个电阻值的数字”);
System.out.println(“程序将信息传递到方法中,然后继续打印输出”);
System.out.println(“相关颜色(按任意键要求输入)”;
input=myInput.readLine();
System.out.println(“输入电阻值(注意电阻只能计算到小数点后4位”);
input=myInput.readLine();
numInput=Integer.parseInt(输入);
//价值观的颜色
array2=值(数组、输入、颜色);
对于(int i=0;i<3;i++){
阵列3[i]=数字颜色(阵列2[i],颜色);
System.out.println(array3[i]);//打印值的颜色
}
//打印第四种颜色,用于倍增
系统输出打印LN(小数位数(输入,颜色));
} 
公共静态int[]值(int[]位、字符串num、字符串[]颜色)
{
绳夹;
双纽霍尔德;
int lengthOfInput;
int-holder2;
//耐受性
保持器=子串的数量(3,4);
数字[3]=整数.parseInt(持有者);
holder2=Integer.parseInt(num);
//检查是否高于5
如果(数字[3]<5){
数字[3]=数字[3]/保持器2*100;
}
否则如果(数字[3]>5){
数字[3]=10位数字[3];
数字[3]=数字[3]/保持器2*100;
}
System.out.println(数字[3]);
//输入的四舍五入
lengthOfInput=num.length()-3;
numHolder=Double.parseDouble(num);
numHolder=numHolder/(数学功率(10,lengthOfInput));
numHolder=(int)(数学四舍五入(numHolder)+0.5);
//前三位数
对于(int i=0;i<3;i++){
holder=num.substring(i,i+1);
数字[i]=整数.parseInt(持有者);
}
//打印以供参考
/*System.out.println(“第一位数字四舍五入为:”+(int)位[0]);
System.out.println(“第二个数字是:”+(int)位[1]);
System.out.println(“第三位数字改为:”+(int)位[2])*/
/* */
返回新的int[]{数字[0],数字[1],数字[2],数字[3]};//返回
}
公共静态字符串数字颜色(整数小数点,字符串[]颜色){
//调用附加变量
字符串回答;
答案=颜色[小数点];
返回答案;
}
//方法来查找乘数
公共静态字符串小数点(字符串输入,字符串[]颜色){
//调用附加变量
int length=input.length();
字符串回答;
长度=长度-3;
答案=颜色[长度];
返回答案;
}
} 

数组array3将始终为空,因为它是以这种方式声明的:
        final String[] array3 = new String[3];

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