我正在尝试做谁想成为一家磨坊。 Java中的应用程序和Java的大一新生。我为此写了gui。我有方法ınQuestions类,它从file读取数据。该代码的工作方式与我编译它们时一样,它显示问题和选项,当ı选择正确时,它显示其他问题和选项。问题是,如果我再次尝试选择选项,它将显示
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 42
at Questions.mechanism(Questions.java:91)
at Interface2.actionPerformed(Interface2.java:430)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
//////QUESTİONCLASS
public class Questions {
String [] QuestionArray = new String [42];
String [] AArray = new String [42];
String [] BArray = new String [42];
String [] CArray = new String [42];
String [] DArray = new String [42];
String [] HighArray = new String[10];
public int QFlag =0;
int i=0;
int a=0;
int x=0;
String g = null;
// ANSWER OPTİONS
File AFile = new File ("C:\\optionA.txt");
File BFile = new File ("C:\\optionB.txt");
File CFile = new File ("C:\\optionC.txt");
File DFile = new File ("C:\\optionD.txt");
File QFile = new File ("C:\\Questions.txt");
File HighFile = new File ("C:\\highest.txt");
public void setOptions() throws IOException{
///A
BufferedReader readerA = null;
readerA = new BufferedReader (new FileReader(AFile));
String optionA = readerA.readLine();
///B
BufferedReader readerB = null;
readerB = new BufferedReader (new FileReader(BFile));
String optionB = readerB.readLine();
///C
BufferedReader readerC = null;
readerC = new BufferedReader (new FileReader(CFile));
String optionC = readerC.readLine();
///D
BufferedReader readerD = null;
readerD = new BufferedReader (new FileReader(DFile));
String optionD = readerD.readLine();
while (optionA!=null){
optionA= readerA.readLine(); optionB= readerB.readLine();
optionC= readerC.readLine();optionD= readerD.readLine();
AArray[x]=optionA;BArray[x]=optionB;
CArray[x]=optionC;DArray[x]=optionD;
x++;
}
readerA.close();
readerB.close();
readerC.close();
readerD.close();
}
public void mechanism() throws IOException{
//READER QUESTİON
BufferedReader readerQ = null;
readerQ = new BufferedReader (new FileReader(QFile));
String Question = readerQ.readLine();
// RANDOM
Random random = new Random();
int QuestionNum = random.nextInt(40);
this.QFlag=QuestionNum;
//System.out.println(QFlag);
while (Question!=null){
Question= readerQ.readLine();
QuestionArray[a]=Question;
a++;
}
readerQ.close();
System.out.println(QuestionArray[QFlag]);}
public void deneme () throws IOException{
BufferedReader readerH = null;
readerH = new BufferedReader (new FileReader(HighFile));
String readed =null;
int c=0;
int k;
while (readed!=null){
readed = readerH.readLine();
HighArray[x]= readed;
c++;
}
readerH.close();
System.out.println(HighArray[0]);
}
}
///////////////接口类
public class Interface2 extends JFrame implements ActionListener{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private final double width = screenSize.getWidth();
private final double height = screenSize.getHeight();
//int axx=0;
private final int WINDOW_HEIGHT = 1200;
private final int WINDOW_WIDTH = 800;
JRadioButton optionA;
JRadioButton optionB;
JRadioButton optionC;
JRadioButton optionD;
JButton resign;
JButton half;
JButton change;
JTextField timer;
JTextField question;
Questions QText = new Questions();
JPanel centerPanel;
ButtonGroup options1;
int soruSayac=1;
JTextField m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12;
File HighFile = new File ("C:\\highest.txt");
int QFlag;
public static void main(String[] args) throws IOException, InterruptedException {
Questions q2 = new Questions();
Interface2 i2 = new Interface2();
new Interface2();
}
public Interface2() throws IOException, InterruptedException{
setTitle ("Who wants to be a millionaire");
setSize (WINDOW_HEIGHT,WINDOW_WIDTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
//// CENTER
JLabel a;
JPanel centerPanel;
centerPanel = new JPanel();
centerPanel.setLayout(null);
Questions QText = new Questions();
//QText.deneme();
QText.setOptions();
QText.mechanism();
question = new JTextField (QText.QuestionArray[QText.QFlag]);
//( question = new JTextField(QText.mechanism());question.setEditable(false);
optionA = new JRadioButton("A) " +QText.AArray[(QText.QFlag)]); //optionA.addActionListener(this);
optionB = new JRadioButton("B) " +QText.BArray[(QText.QFlag)]); //optionB.addActionListener(this);
optionC = new JRadioButton("C) " +QText.CArray[(QText.QFlag)]); //optionC.addActionListener(this);
optionD = new JRadioButton("D) " +QText.DArray[(QText.QFlag)]); //optionD.addActionListener(this);
resign=new JButton("Resign"); resign.addActionListener(this);
half = new JButton (); half.addActionListener(this);
change = new JButton (); change.addActionListener(this);
ButtonGroup options1 = new ButtonGroup();
options1.add(optionA);options1.add(optionB);
options1.add(optionC);options1.add(optionD);
timer = new JTextField (); timer.setEditable(false);
a=new JLabel();
Image backg = new ImageIcon(this.getClass().getResource("/arkaplan2.png")).getImage();
a.setIcon(new ImageIcon(backg));
Image fifty = new ImageIcon(this.getClass().getResource("/5050.jpg")).getImage();
half.setIcon(new ImageIcon(fifty));
Image change1 = new ImageIcon(this.getClass().getResource("/change.jpg")).getImage();
change.setIcon(new ImageIcon(change1));
centerPanel.add(change);
centerPanel.add(optionA);centerPanel.add(optionB);
centerPanel.add(optionC);centerPanel.add(optionD);
centerPanel.add(question);centerPanel.add(timer);centerPanel.add(resign);
centerPanel.add(a); centerPanel.add(half);
half.setBounds(230, 100, 100, 58); change.setBounds(400, 100, 98, 60);
optionA.setBounds(112, 475, 250, 75);optionB.setBounds(438, 475, 250, 75);
optionC.setBounds(112, 600, 250, 75);optionD.setBounds(438, 600, 250, 75);
question.setBounds(112, 250, 575, 200);timer.setBounds(587, 180, 100,60);
resign.setBounds(112,180,100, 60);a.setBounds(0, 0, 800, 800);
question.setBackground(Color.black);question.setFont(new Font("sansserif", Font.BOLD, 18));
optionA.setBackground(Color.black);optionA.setFont(new Font("sansserif", Font.BOLD, 15));
optionB.setBackground(Color.black);optionB.setFont(new Font("sansserif", Font.BOLD, 15));
optionC.setBackground(Color.black);optionC.setFont(new Font("sansserif", Font.BOLD, 15));
optionD.setBackground(Color.black);optionD.setFont(new Font("sansserif", Font.BOLD, 15));
question.setForeground(Color.white);question.setHorizontalAlignment(SwingConstants.CENTER);
optionA.setForeground(Color.white);
optionB.setForeground(Color.white);
optionC.setForeground(Color.white);
optionD.setForeground(Color.white);
add (centerPanel,BorderLayout.CENTER);
//add(background,BorderLayout.CENTER);
//////////////////////////////////////////// NORTH
//JTextField northField;
//northField=new JTextField ("Who wants to be a milloniare");
//add(northField,BorderLayout.NORTH);
//northField.setEditable(false);
//////////////////////////////////////////// EAST
JPanel eastPanel;
eastPanel= new JPanel();
eastPanel.setLayout(null);
eastPanel.setBackground(Color.BLUE);
/// TEXT FIELDS ///
JTextField m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12;
Font f=new Font("sansserif", Font.BOLD, 14);
m12=new JTextField ("1.000.000 TL");m11=new JTextField ("250.000 TL");m10=new JTextField ("125.000 TL");
m9=new JTextField ("60.000 TL");m8=new JTextField ("30.000 TL");m7=new JTextField ("15.000 TL");
m6=new JTextField ("7.500 TL");m5=new JTextField ("5.000 TL");m4=new JTextField ("3.000 TL");
m3=new JTextField ("2.000 TL");m2=new JTextField ("1.000 TL");m1=new JTextField ("500 TL");
m12.setBackground(Color.BLACK); m11.setBackground(Color.BLACK); m10.setBackground(Color.BLACK);
m9.setBackground(Color.BLACK); m8.setBackground(Color.BLACK); m7.setBackground(Color.BLACK);
m6.setBackground(Color.BLACK); m5.setBackground(Color.BLACK); m4.setBackground(Color.BLACK);
m3.setBackground(Color.BLACK); m2.setBackground(Color.BLACK); m1.setBackground(Color.YELLOW);
m12.setEditable(false);m11.setEditable(false);m10.setEditable(false);m9.setEditable(false);
m8.setEditable(false);m7.setEditable(false);m6.setEditable(false);m5.setEditable(false);
m4.setEditable(false);m3.setEditable(false);m2.setEditable(false);m1.setEditable(false);
eastPanel.add(m12);eastPanel.add(m11);eastPanel.add(m10);eastPanel.add(m9);eastPanel.add(m8);
eastPanel.add(m7);eastPanel.add(m6);eastPanel.add(m5);eastPanel.add(m4);
eastPanel.add(m3);eastPanel.add(m2);eastPanel.add(m1);
m1.setFont(new Font("sansserif", Font.BOLD, 30));m2.setFont(new Font("sansserif", Font.BOLD, 30));m3.setFont(new Font("sansserif", Font.BOLD, 30));
m4.setFont(new Font("sansserif", Font.BOLD, 30));m5.setFont(new Font("sansserif", Font.BOLD, 30));m6.setFont(new Font("sansserif", Font.BOLD, 30));
m7.setFont(new Font("sansserif", Font.BOLD, 30));m8.setFont(new Font("sansserif", Font.BOLD, 30));m9.setFont(new Font("sansserif", Font.BOLD, 30));
m10.setFont(new Font("sansserif", Font.BOLD, 30));m11.setFont(new Font("sanssserif", Font.BOLD, 30));m12.setFont(new Font("sansserif", Font.BOLD, 35));
m1.setForeground(Color.WHITE);m4.setForeground(Color.WHITE);m7.setForeground(Color.WHITE);m10.setForeground(Color.WHITE);
m2.setForeground(Color.WHITE);m5.setForeground(Color.WHITE);m8.setForeground(Color.WHITE);m11.setForeground(Color.WHITE);
m3.setForeground(Color.WHITE);m6.setForeground(Color.WHITE);m9.setForeground(Color.WHITE);m12.setForeground(Color.WHITE);
m1.setHorizontalAlignment(SwingConstants.CENTER);m5.setHorizontalAlignment(SwingConstants.CENTER);m9.setHorizontalAlignment(SwingConstants.CENTER);
m2.setHorizontalAlignment(SwingConstants.CENTER);m6.setHorizontalAlignment(SwingConstants.CENTER);m10.setHorizontalAlignment(SwingConstants.CENTER);
m3.setHorizontalAlignment(SwingConstants.CENTER);m7.setHorizontalAlignment(SwingConstants.CENTER);m11.setHorizontalAlignment(SwingConstants.CENTER);
m4.setHorizontalAlignment(SwingConstants.CENTER);m8.setHorizontalAlignment(SwingConstants.CENTER);m12.setHorizontalAlignment(SwingConstants.CENTER);
m1.setBounds(40,700,320, 55);m2.setBounds(40,635,320, 53);m3.setBounds(40,573,320, 53);m4.setBounds(40,511,320, 53);
m5.setBounds(40,449,320, 53);m6.setBounds(40,387,320, 53);m7.setBounds(40,325,320, 53);m8.setBounds(40,263,320, 53);
m9.setBounds(40,201,320, 53);m10.setBounds(40,139,320, 53);m11.setBounds(40,75,320, 53);m12.setBounds(40,10,320, 53);
eastPanel.setPreferredSize(new Dimension(400,800));
add (eastPanel,BorderLayout.EAST);
setVisible(true);
System.out.println(QText.QFlag);
QFlag=QText.QFlag;
System.out.println("sa"+QFlag);
}
@Override
public void actionPerformed(ActionEvent e){
/* boolean cont = false;
if (e.getSource()== resign){
System.out.println(QFlag);
if (QFlag<=10 ){
if (optionA.isSelected()){
soruSayac++;
cont = true ;
}
else if (optionB.isSelected()) {
cont = false ;
}
else if (optionC.isSelected()) {
cont = false ;
}
else if (optionD.isSelected()) {
cont = false ;
}
}
else if (QFlag >10 && QFlag<=20){
if (optionB.isSelected()){
soruSayac++;
cont = true ;
}
else if (optionA.isSelected()) {
cont = false ;
}
else if (optionC.isSelected()) {
cont = false ;
}
else if (optionD.isSelected()) {
cont = false ;
}
}
else if (QFlag >20 && QFlag<=30 ){
if (optionC.isSelected()){
soruSayac++;
cont = true ;
}
else if (optionD.isSelected()) {
cont = false ;
}
else if (optionA.isSelected()) {
cont = false ;
}
else if (optionB.isSelected()) {
cont = false ;
}
}
else if (QFlag >30 && QFlag<=40){
if (optionD.isSelected()){
soruSayac++;
cont = true ;
}
else if (optionA.isSelected()) {
cont = false ;
}
else if (optionB.isSelected()) {
cont = false ;
}
else if (optionC.isSelected()) {
cont = false ;
}
}
else{
cont= false;
}
if (cont){
try {
changeJ();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
else if (e.getSource()==half){
try {
halfJ();
half.setEnabled(false);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if (e.getSource()==change){
try {
changeJ();
change.setEnabled(false);
//System.out.println(QText.AArray[(QFlag)]);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}*/
boolean cont = false;
if (e.getSource()==resign){
System.out.println("neler oluyor"+QFlag);
if (QFlag < 10 && (optionA.isSelected())){
cont=true;
soruSayac++;
}
else if (QFlag > 10 && QFlag <=20 && (optionB.isSelected())){
soruSayac++;
cont=true;
}
else if(QFlag > 20 && QFlag <=30 && optionC.isSelected() ){
soruSayac++;
cont=true;
}
else if(QFlag > 30 && QFlag <=40 && (optionD.isSelected())){
soruSayac++;
cont=true;}
else{
cont = false;
}}
if (!(optionA.isSelected()|| optionB.isSelected()|| optionC.isSelected()|| optionD.isSelected ())){
System.out.println("boşbırakma");
}
if (soruSayac==12){
System.out.println("bittik biz bittik");
}
//System.out.println(soruSayac);
//System.out.println(cont);
if (cont==true){
try {
// setVisible(false);
// System.exit(0);
// new Interface2();
// soruSayac++;
System.out.println(soruSayac);
System.out.println(cont);
QText.mechanism();
QText.setOptions();
QFlag=QText.QFlag;
System.out.println("SSSASSDS"+QFlag);
//options1.clearSelection();
// System.out.println(QFlag);
question.setText(QText.QuestionArray[(QFlag)]);
// System.out.println(cont);
optionA.setText(QText.AArray[(QFlag)]);
optionB.setText(QText.BArray[(QFlag)]);
optionC.setText(QText.CArray[(QFlag)]);
optionD.setText(QText.DArray[(QFlag)]);
//options1.clearSelection();
cont=true;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if (cont==false){
prizeF(soruSayac);
}}
public void changeJ () throws IOException{
QText.mechanism();
QText.setOptions();
QFlag=QText.QFlag;
options1.clearSelection();
System.out.println(QFlag);
question.setText(QText.QuestionArray[(QFlag)]);
optionA.setText(QText.AArray[(QFlag)]);
optionB.setText(QText.BArray[(QFlag)]);
optionC.setText(QText.CArray[(QFlag)]);
optionD.setText(QText.DArray[(QFlag)]);
}
public void halfJ () throws IOException{
if (QText.QFlag<=10){
optionB.setEnabled(false);
optionC.setEnabled(false);
}
else if (QText.QFlag<=20){
optionC.setEnabled(false);
optionD.setEnabled(false);
}
else if (QText.QFlag<=30){
optionA.setEnabled(false);
optionB.setEnabled(false);
}
else if (QText.QFlag<=40){
optionA.setEnabled(false);
optionC.setEnabled(false);
}
}
public void colorC (int k){
if (k==2){
m2.setBackground(Color.yellow);
m1.setBackground(Color.black);
}
else if (k==3){
m3.setBackground(Color.yellow);
m2.setBackground(Color.black);
}
else if (k==4){
m4.setBackground(Color.yellow);
m3.setBackground(Color.black);
}
else if (k==5){
m5.setBackground(Color.yellow);
m4.setBackground(Color.black);
System.out.println("calısmıyom cunku malım");
}
else if (k==6){
m6.setBackground(Color.yellow);
m5.setBackground(Color.black);
}
else if (k==7){
m7.setBackground(Color.yellow);
m6.setBackground(Color.black);
}
else if (k==8){
m8.setBackground(Color.yellow);
m7.setBackground(Color.black);
}
else if (k==9){
m9.setBackground(Color.yellow);
m8.setBackground(Color.black);
}
else if (k==10){
m10.setBackground(Color.yellow);
m9.setBackground(Color.black);
}
else if (k==11){
m11.setBackground(Color.yellow);
m10.setBackground(Color.black);
}
else if (k==12){
m12.setBackground(Color.yellow);
m11.setBackground(Color.black);
}
else {
System.out.println("ben else oldum");
}
}
public void prizeF(int l){
String award="sa";
if (l==1){
award = "Congrats!!! You earned 500 TL !!!";
}
else if (l==2){
award = "Congrats!!! You earned 1000 TL !!!";
}
else if (l==3){
award = "Congrats!!! You earned 2000 TL !!!";
}
else if (l==4){
award = "Congrats!!! You earned 3000 tl !!!";
}
else if (l==5){
award = "Congrats!!! You earned 5000 TL !!!";
}
else if (l==6){
award = "Congrats!!! You earned 7500 TL !!!";
}
else if (l==7){
award = "Congrats!!! You earned 15000 TL !!!";
}
else if (l==8){
award = "Congrats!!! You earned 30000 TL !!!";
}
else if (l==9){
award = "Congrats!!! You earned 60000 TL !!!";
}
else if (l==10){
award = "Congrats!!! You earned 125000 TL !!!";
}
else if (l==11){
award = "Congrats!!! You earned 250000 TL !!!";
}
else if (l==12){
award = "OMGG!!! YOU BECOME A MILLIONAIRE";
}
JOptionPane.showMessageDialog(null, award + " Your score has sucsessfully recorded");
}
}
答案 0 :(得分:0)
问题可能出在这方面:
while (Question!=null){
Question= readerQ.readLine();
QuestionArray[a]=Question;
a++;
}
我对此的理解是,虽然正在读取Question
,但要增加数组索引并插入它们。
这是问题所在:如果Question
数多于数组可以容纳的数量,该怎么办?您将a
的增量增加得太远,而下次您尝试将其添加到数组时,将会得到异常。
您应该在while
语句中添加一个测试,以确保a
仍然是有效值。
编辑:此外,似乎多次调用了该方法,但是索引变量a
并未每次都重置为0。