Actionlistener in swing won't let me play with my variables!
I am sort of new to java, having done some scripting before, but not much programming experiance. OOP was, until I learned java, something I didn't use that much. All this static and public stuff was hard at first, but I finally think I understand it all.
I am still learning java, and for one of my projects, I am trying to make a small quiz game to learn swing. I am having problems. My button actionlistener complains about my variables, and I have tried many things, like wrapping them in a container and moving them, but I cannot make it compile. Any help would be appriciated. Thanks in advance!
//The program is a quiz thingy.
//the error is down in the GUI section, where I make the button respond to commands
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;//for later use, download files to read off net
import java.io.*;
import java.math.*;
import java.util.Random;
public class Main {
public static void main(String args[]) {
File file = new File("problems.txt");
int probcount=5;
int answercount=0;
boolean onproblem = true;
String[] Problems={"","","","","","","","","","","",""};//I find if I don't init, I get nullpointer errors
String[] Answers={"","","","","","","","","","","",""};//sorry for the oddness
String[] thetext=new String[100];
int i=0;
int t=0;
if ( !file.exists( ) || !file.canRead( ) ) {
System.out.println( "Can't read " + file );
return;
try {
FileReader fr = new FileReader ( file );
BufferedReader in = new BufferedReader( fr );
String line;
while ((line = in.readLine( )) != null ){
thetext=line;
i++;
catch ( FileNotFoundException e ) {
System.out.println( "File Disappeared" );
catch ( IOException e ) {
System.out.println( "Error During File Reading" );
boolean writetoprob = true;
for(int y=0;y<i;y++)
System.out.println(thetext[y]);
for(int y=0;y<i;y++){
if(thetext[y].equals("-")){
if(writetoprob==true)
writetoprob=false;
else{
writetoprob=true;
t++;
else{
if(writetoprob==true)
Problems[t]=Problems[t].concat("\n").concat(thetext[y]);
else
Answers[t]=Answers[t].concat("\n").concat(thetext[y]);
System.out.println(Problems[0]);
System.out.println(Problems[1]);
//TODO:Randomize problems and display them, then answers when button clicked
boolean answerbutton=true;
int probindex=0;
Random rnums = new Random();
probindex=rnums.nextInt()%(t+1);
if(probindex<0)
probindex=-probindex;
System.out.println(probindex);
System.out.println(Problems[probindex]);
JButton action = new JButton("Click for Answer!");
JTextArea tp = new JTextArea(Problems[probindex]);
JFrame jf = new JFrame();
boolean onanswer = false;
action.addActionListener( new ActionListener( ) {
public void actionPerformed(ActionEvent theaction) {
System.out.println(answerbutton);
if(answerbutton==false){
answerbutton=true;
probindex=rnums.nextInt()%(t+1);
if(probindex<0)
probindex=-probindex;
tp.setText(Problems[probindex]);
else{
answerbutton=false;
tp.setText(Answers[probindex]);
Container content = jf.getContentPane( );
content.setLayout(new FlowLayout( ));
content.add(tp);
content.add(action);
jf.pack();
jf.setVisible(true);
init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Patrick\FirstCup\build\classes
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:91: local variable answerbutton is accessed from within inner class; needs to be declared final
System.out.println(answerbutton);
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:92: local variable answerbutton is accessed from within inner class; needs to be declared final
if(answerbutton==false){
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:93: local variable answerbutton is accessed from within inner class; needs to be declared final
answerbutton=true;
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:94: local variable probindex is accessed from within inner class; needs to be declared final
probindex=rnums.nextInt()%(t+1);
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:94: local variable rnums is accessed from within inner class; needs to be declared final
probindex=rnums.nextInt()%(t+1);
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:94: local variable t is accessed from within inner class; needs to be declared final
probindex=rnums.nextInt()%(t+1);
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:95: local variable probindex is accessed from within inner class; needs to be declared final
if(probindex<0)
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:96: local variable probindex is accessed from within inner class; needs to be declared final
probindex=-probindex;
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:96: local variable probindex is accessed from within inner class; needs to be declared final
probindex=-probindex;
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:97: local variable Problems is accessed from within inner class; needs to be declared final
tp.setText(Problems[probindex]);
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:97: local variable probindex is accessed from within inner class; needs to be declared final
tp.setText(Problems[probindex]);
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:97: local variable tp is accessed from within inner class; needs to be declared final
tp.setText(Problems[probindex]);
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:100: local variable answerbutton is accessed from within inner class; needs to be declared final
answerbutton=false;
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:101: local variable Answers is accessed from within inner class; needs to be declared final
tp.setText(Answers[probindex]);
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:101: local variable probindex is accessed from within inner class; needs to be declared final
tp.setText(Answers[probindex]);
C:\Documents and Settings\Patrick\FirstCup\src\firstcup\Main.java:101: local variable tp is accessed from within inner class; needs to be declared final
tp.setText(Answers[probindex]);
16 errors
BUILD FAILED (total time: 3 seconds)
Similar Messages
-
My ipod touch 2nd Gen let me sign up to game center but it won't load any of my games and won't let me play with my friend. I don't know if it's like this because it is old and slow, or what. Please help me figure this out. Thank You.
Try:
- Sign out of Game Center and sign back in
- Restore from backup. See:
iOS: Back up and restore your iOS device with iCloud or iTunes
- Restore to factory settings/new iOS device. -
I am playing card games at Yahoo in my iMac OS X version 10.6.8. Just recently it won't let me play anymore as it won't upload. Kinda frustrating. How can I fix it. The only thing that I know I have done is did an update on my computer.
You have no other option other than to activate that phone with a SIM card. The SIM doesn't need to be an active SIM, however, so any old deactivated SIM will do.
-
NBC app won't let me play through my iPad to my samsung tv. Say no airplay supported. How do I fix it.
Not all apps support airplay.
If NBC does not support it, there is nothing to fix. -
I made an iMovie last night and the video is in my library but it won't let me play it or edit it?? Help Please
Contact iTunes support at the link below.
https://ssl.apple.com/emea/support/itunes/contact.html -
Laptop won't let me play any video or change the brightness of the screen at all.
I got this new Laptop a few months ago and it has just recently started to play up slightly and not let me change my brightness settings at all and now it won't let me play any videos.
I got a solution
First check out your performance settings and if u dont know where to find it just right click on my computer and enter to propreties then click on performance and tools, then if the gamming and desktop aero system are 1.0 then you have three solutions.
1) Install a new windows
2) check out your computer driver and install tge latest version
3) or simply you can download DRIVER PAC SOLUTION and install it then after installation is done you might wanna restart the system and check performance again then it should desplay NEW HARDWARE DETECT REFRESH NOW then click on refreah and the rating is gonna go up from1.0.to higher than 4.0. -
My computer is authorized but won't let me play movies from external drive
Before the most recent update, I was backing up my movies and music to an external drive to save room on my hard drive. I would then play those movies in QT from the external drive. I didn't import into iTunes bc I wanted to save room. Now, however, it won't let me play those movies unless I import into iTunes. I get a please authorize field. I authorize and it says that this computer is already authorized but it still won't play the movie.
SO FAR I THINK IT IS "GENIUS" (IT'S A DUNCE!!!!)
TURN OFF GENIUS THE REBOOT iTUNES...
SO FAR THIS HAS WORKED FOR ME! -
I tried all these steps and iTunes still won't let me play songs I paid for... Every time I authorize, it repeats that it's already authorized, but won't play because it's not authorized ... Catch 22
Delete and redownload them if doing so is free in your country.
(83692) -
I can see a mirrored image of my laptop screen but it won't let me play videos something about my display please help
Can you send Messages with videos?
Have you tried:
iOS: Troubleshooting Messages -
Switched computers due to a crash. I authorised the new one. It won't let me play songs in my library that were purchased from I-tunes. Please help.
All content that you download from iTunes is tied to the account that downloaded it, and it can't be transferred to or merged with another account - so you will need to authorise that old account on that computer if you want to use its content.
If you still have access to the email accoun then you can try getting the password reset via this page : http://appleid.apple.com, then 'reset your password
If you don't have access to the email account then you could try contacting iTunes support and see if they can help you get access to the account : http://www.apple.com/support/itunes/contact/ - click on Contact iTunes Store Support on the right-hand side of the page -
I transferred my library to a new computer. Now we have a different ID and it won't let us play most of our library? It prompts us to use our old apple ID with a different email and lost password? Any ideas how to be able to play our extensive, expensive catalog?
All content that you download from iTunes is tied to the account that downloaded it, and it can't be transferred to or merged with another account - so you will need to authorise that old account on that computer if you want to use its content.
If you still have access to the email accoun then you can try getting the password reset via this page : http://appleid.apple.com, then 'reset your password
If you don't have access to the email account then you could try contacting iTunes support and see if they can help you get access to the account : http://www.apple.com/support/itunes/contact/ - click on Contact iTunes Store Support on the right-hand side of the page -
my speakers on my mac don't work.the icon of the speaker is dimmed and it won't let me play music or Skype.it tells me i have a problem with my speakers
you may have flipped the micro switch inside the audio jack. To rectify this issue take a toothpick an gently wiggle it inside your audio jack.
-
Just bought a new hp laptop. Downloaded iTunes. Won't let me play any of my selections. Reads "computer not authorized". What do I do to make this happen?
On your Computer... Open iTunes...
and from the Menu Bar select Store...
From the Store menu, choose Authorize This Computer.
When prompted, enter your Apple ID and password, then click Authorize.
See Here > http://support.apple.com/kb/HT1420 -
My iphone4 has the correct Apple ID in the information settings section and asks me to login to my ID most of the time but when I try to update my apps, it asks me to login with an ID that isn't mine. I deleted the wrong ID from my itunes but it still won't let me login with my own ID to update apps. How do I get my iphone on the same page?
How did you obtain these apps? All apps are DRM protected & tied forever to the account used to originally obtain them. They cannot be transferred to another Apple ID(account), nor can they be updated using any Apple ID other than the one that was used to originally obtain them.
-
I can play the music on my itunes under my purchased section but when I go to my phone when I have it connected the a lot of the songs won't play on my song or from the itunes under my phone. They have a little icon to the left of them that looks like a repeat icon. It won't let me delete them from there nor play them. When on my phone only it won't play them it just brings up the clip art but will not play. Please can anyone help me.
Thanks,Thank you - I thought that might be the case BUT why doesn't the music play that I have bought from itunes - do you happen to know please.
All that happens is it just flicks through it all and won't play any of it???
As in I select the song and it just flicks back one screen (if I am making sense).....
I am really stuck and cannot work out what's gone wrong that's all.
Considering a reset of my new iphone and trying again but done that once and it's still the same.
Maybe you are looking for
-
Memory Problems - Advice on quality memory for 865PE Neo2
I have 2 x 512 MB DDR400 in Dual Channel configuration in my 865PE Neo2 mobo. It's a noname brand (the only mention on it are the letters "VM"). A friend recently warned me about the problems that my arise with cheap memory, so I ran memtest-86 and W
-
Hi, Say A.tiff contains 6 pages and B.tiff contains 5 pages. I need to 5 pages of B.tiff into A.tiff, so that A.tiff contains 11 pages. Can someone pls show me the fastest way of acheiving this ? Currently I am using ImageDecoder to decode A and B to
-
A popup stating "XP antivirus 2012" prevents Firefox from opening
I want to open Firefox but keep getting message from "XP antivirus 2012 security center"that Firefox is infected with a Trojan and has 29 other viruses. Even when I tell this "antivirus 2012" security center I want to proceed anyway without signing u
-
Can't use encore in creative cloud
HEY , recently joined creative cloud ... and I am currently working with premier pro ... But my encore software doesn't work ... it wants a serial number .... please help me out because i joined creative cloud just to use encore ?
-
System locking up - trying to pin down why.
Recently my Lenovo SL 500 has been locking up completely using Arch x86_64 running kernel 2.6.31-ARCH. Its complete system hang, not just X. In fact, the laptop actually flashes the capslock LED when it happens. So far, I have not been able to pin it