首先,我是一名新手程序员,所以我没有最好的代码知识,我也不知道如何使用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]+" .";
}
}
现在我收到了这个编译错误,
Character.java:28: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
Scanner inFile = new Scanner(new File("logos.txt"));
^
1 error
我不确定它甚至意味着什么。
所以我已经完成了所有人建议的所有更改并编译了! 但是,当我跑它时,我得到了它;
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)
顺便提一下,TERA是司机。
答案 0 :(得分:3)
你的for循环写错了,永远不会结束:
for(int w = 1; 1 <= 9; w++)
条件1 <= 9
将始终为真,因此循环将永远不会结束。
取而代之的是:
for(int w = 0; w < 9; w++)
请注意,数组是基于0的。如果你的w变量变为9,你将耗尽数组空间并获得一个超出范围的数组索引异常。
更好的是避免幻数:
for (int w = 0; w < numbers.length; w++)
关于:
按资源类我的意思是它不是驱动程序,所以它不包含main方法,而这个类Character是由另一个包含main方法和JFrame的类调用的JPanel。
然后让你的构造函数抛出异常,让另一个类通过调用try和catch块中的构造函数来处理异常。
作为一个侧面推荐,我改变了这个类的名称,因为它与核心Java类的名称冲突。
另一个问题是你的代码正在遮蔽它的一些变量,即名称和数字。通过在构造函数中重新声明变量,您将类字段保留为null。解决方案是不重新声明变量。
即改变这个:
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];
接下来我们需要处理布局管理器的使用....