Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 做一个Swing徽标测试_Java_Arrays_Swing - Fatal编程技术网

Java 做一个Swing徽标测试

Java 做一个Swing徽标测试,java,arrays,swing,Java,Arrays,Swing,首先,我是一个新手程序员,所以我对代码没有最好的了解,也不知道如何很好地使用stackoverflow import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class Logoquiz extends JPanel { private JPanel box; private JPanel sprite; p

首先,我是一个新手程序员,所以我对代码没有最好的了解,也不知道如何很好地使用stackoverflow

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.*;
import java.io.*;

public class Logoquiz extends JPanel
{
   private JPanel box;
   private JPanel sprite;
   private JPanel subtitle; 
   private JPanel opt;  
   private JButton rBOX;
   private JButton q;  
   private JButton sub;
   private JButton oA;
   private JButton oB;
   private int qNum;
   private int []numbers;
   private String []names;
   private int currentN;
   private int mysteryN;

   public Character()
   {

      Scanner inFile = new Scanner(new File("logos.txt"));
      int []numbers = new int[9];
      String []names = new String[9];
      for(int w = 0; w < 9; w++)
      {
         numbers[w]=inFile.nextInt();
         names[w]=inFile.next();
      }  

      currentN=pickNum();
      mysteryN=pickNum();

      setLayout(new BorderLayout()); 

      box = new JPanel();
      box.setLayout(new FlowLayout()); 
      box.setPreferredSize(new Dimension(1200,490));

      rBOX = new JButton("Click Next to Begin!");
      rBOX.setPreferredSize(new Dimension(1170,480));
      Color bg = new Color(25,179,230);
      rBOX.setBackground(Color.WHITE);
      rBOX.setFont(new Font("Arial", Font.PLAIN, 18));
      rBOX.setForeground(Color.BLACK);  
      box.add(rBOX);

      add(box, BorderLayout.NORTH);
      box.setBackground(Color.GRAY);

    //----------------------------------------------------------

      subtitle = new JPanel();
      subtitle.setLayout(new FlowLayout()); 
      subtitle.setPreferredSize(new Dimension(1200,170));

      q = new JButton(statement());
      q.setPreferredSize(new Dimension(1170,80));
      q.setBackground(bg);
      q.setFont(new Font("Arial", Font.PLAIN, 22));
      q.setForeground(Color.white);
      q.addActionListener(new qListener());
      subtitle.add(q);

      sub = new JButton("NEXT");
      sub.setPreferredSize(new Dimension(1170,80));
      sub.setBackground(bg);
      sub.setFont(new Font("Arial", Font.PLAIN, 22));
      sub.setForeground(Color.white);
      sub.addActionListener(new subListener());
      subtitle.add(sub);     

      add(subtitle, BorderLayout.CENTER);
      subtitle.setBackground(Color.GRAY);

    //----------------------------------------------------------

      opt = new JPanel();
      opt.setLayout(new FlowLayout()); 
      opt.setPreferredSize(new Dimension(1200,100));

      oA = new JButton("True");
      oA.setPreferredSize(new Dimension(580,90));
      oA.setBackground(Color.GREEN);
      oA.setFont(new Font("Arial", Font.PLAIN, 22));
      oA.setForeground(Color.white);
      opt.add(oA);

      oB = new JButton("False");
      oB.setPreferredSize(new Dimension(580,90));
      oB.setBackground(Color.RED);
      oB.setFont(new Font("Arial", Font.PLAIN, 22));
      oB.setForeground(Color.white);
      opt.add(oB);

      add(opt, BorderLayout.SOUTH);
      opt.setBackground(Color.GRAY);

 }
      //---------------------------------------------------------

   private class subListener implements ActionListener
   {
      public void actionPerformed(ActionEvent click)
      {
         rBOX.setText("");
         rBOX.setIcon(new ImageIcon(numbers[currentN]+".jpg"));
         currentN=pickNum();
      }

   }

   private class qListener implements ActionListener
   {
      public void actionPerformed(ActionEvent click)
      {
         rBOX.setText("");
         rBOX.setIcon(new ImageIcon(numbers[currentN]+".jpg"));
      }

   }

      //---------------------------------------------------------

   public int pickNum()
   {

      qNum = (int)(Math.random()*9+1);
      return qNum;

   }

   public String statement()
   {

      return "This is the logo of"+names[mysteryN]+" .";

   }



}
我甚至不知道这是什么意思

所以我已经做了所有人建议的更改,并编译了它! 然而,当我运行它时,我得到了这个

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Logoquiz.<init>(Logoquiz.java:31)
    at TERA.main(TERA.java:17)
线程“main”java.util.NoSuchElementException中的异常 位于java.util.Scanner.throwFor(Scanner.java:862) 下一步(Scanner.java:1485) 位于java.util.Scanner.nextInt(Scanner.java:2117) 位于java.util.Scanner.nextInt(Scanner.java:2076) 在logoquick上。(logoquick.java:31) 在TERA.main(TERA.java:17)
顺便说一下,TERA是驱动程序。

您的for循环写错了,永远不会结束:

for(int w = 1; 1 <= 9; w++)  
为此:

public Character() throws FileNotFoundException {
  Scanner inFile = new Scanner(new File("logos.txt"));
  numbers = new int[9];
  names = new String[9];


接下来,我们需要研究布局管理器的使用…

那么它现在做什么,您希望它做什么?请参阅。此外,最好不要在回答问题后删除部分内容。保持它的完整性,以便其他有相同问题或遇到相同错误的人可以找到它并从中得到使用。
“所以我已经做了所有人建议的更改,并且它可以编译!但是,当我运行它时,我得到了这个;”
——这是一个新的和不相关的问题,因此值得提出一个新的和不相关的问题。您需要做的是询问您的小程序,但为此,请编写一个小型非GUI程序,该程序只将您试图提取的文本提取到数组中,然后打印出结果。然后发布小程序、小数据文件和您看到的错误消息。
FileNotFoundException
是选中的异常。需要处理它。@Braj:我以为我解决了那个错误。如果构造函数抛出异常,这就是这段代码需要做的全部工作。+1也用于处理数组索引越界异常。
for(int w = 0; w < 9; w++)
for (int w = 0; w < numbers.length; w++)
public Character() throws FileNotFoundException {
  Scanner inFile = new Scanner(new File("logos.txt"));
  int[] numbers = new int[9];
  String[] names = new String[9];
public Character() throws FileNotFoundException {
  Scanner inFile = new Scanner(new File("logos.txt"));
  numbers = new int[9];
  names = new String[9];