Any Java Gurus About?
<div class="IPBDescription">Need a bit of help with my first applet</div> I keep getting a compiler error, heres the code
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Div extends Applet implements ActionListener
{
TextArea box1;
TextArea box2;
Button one;
public void init()
{
box1 = new TextArea("Fiddle Dee Dee!", 20, 50);
box2 = new TextArea(20, 50);
one = new Button("Lowercase");
one.AddActionListener(this);
add(box1);
add(one);
add(box2);
}
public void actionPerformed(ActionEvent e)
{
String box1.text = box1.getText();
box1.text.toLowerCase();
boxmethod.SetText("Compy Rox0rz! OMG!");
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
And the error I'm getting
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
---------- Javac ----------
Div.java:32: ';' expected
String box1.text = box1.getText();
^
1 error
Output completed (0 sec consumed) - Normal Termination
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
I'm at somewhat of a loss as to understand why it expects a ; there, this code is being copied from lecture notes so it *should* be correct though I may have messed a few things up here and there.
Anyone know what I'm doing wrong?
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Div extends Applet implements ActionListener
{
TextArea box1;
TextArea box2;
Button one;
public void init()
{
box1 = new TextArea("Fiddle Dee Dee!", 20, 50);
box2 = new TextArea(20, 50);
one = new Button("Lowercase");
one.AddActionListener(this);
add(box1);
add(one);
add(box2);
}
public void actionPerformed(ActionEvent e)
{
String box1.text = box1.getText();
box1.text.toLowerCase();
boxmethod.SetText("Compy Rox0rz! OMG!");
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
And the error I'm getting
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
---------- Javac ----------
Div.java:32: ';' expected
String box1.text = box1.getText();
^
1 error
Output completed (0 sec consumed) - Normal Termination
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
I'm at somewhat of a loss as to understand why it expects a ; there, this code is being copied from lecture notes so it *should* be correct though I may have messed a few things up here and there.
Anyone know what I'm doing wrong?
Comments
Try turning that one line into this...
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->String box1.text;
box1.text = box1.getText();
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
As [WHO]Them pointed out, your problem is this line:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->String box1.text = box1.getText();<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
is completely wrong. You cannot create a variable called "box1.text". I assume you're trying to set the text of the textarea named box1. I'm a bit rusty on my java, but assuming that you just set <i>text</i> directly rather than some accessor function, you should be able to get by with just getting rid of the String keyword right in front of that line.
Again, my java is rusty, so don't go looking for that to be the final solution, but that line is definately where your problem is.
box1.text is already defined; the text member is already defined by the TextArea class (one assumes).
Besides, I'd expect Java to smack your bottom for trying to declare a variable with a dot in it, anyway.
That's to be expected when someone just SWAGs an answer <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html//emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif' /><!--endemo-->
I tried renaming the box1.text to box1text, text, or simply box1 and it always generates far more errors then it solves.
I've tried splitting it across lines, how do I fix this damned error! ARRHHH!!! //tears hair out
I tried renaming the box1.text to box1text, text, or simply box1 and it always generates far more errors then it solves.
I've tried splitting it across lines, how do I fix this damned error! ARRHHH!!! //tears hair out<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
Er... no.You're seriously misunderstanding how this code works. And if this is out of a lecture slide and you're expected to understand it, then your lecturer should indeed be shot.
/me re-reads the code.
No, your lecturer shouldn't be shot, and if I'd bothered to read your code closely the first time I'd know that. "Compy Rox0rz", I assume, was not in the original lecture notes, so you have changed something. That suggests that you've made other changes, too, which you thought were trivial but actually weren't.
Anyway.... I...
Whoa. The more closely I read your code, the less sense it makes. It's clear you have a serious misunderstanding of object orientation - talk to your lecturer about it.
Anyway, the major problem is this: the identifier <span style='font-family:Courier'>box1.text</span> is illegal. You can't delcare a variable name that has a dot in it. The dot is an operator, and so cannot be part of a variable name.
You were correct in renaming it from <span style='font-family:Courier'>box1.text</span> to <span style='font-family:Courier'>box1text</span>. <span style='font-family:Courier'>text</span> is a better name, and you will encounter problems trying to use <span style='font-family:Courier'>box1</span>. It doesn't generate more errors than it solves - it simply fixes your main error, which allows the compiler to trip over all the <i>other</i> errors in your code!
Change it to <span style='font-family:Courier'>text</span> and then post the new error messages. I have a feeling I know what they're going to be, but let's find out...
Heres my revised code
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Div extends Applet implements ActionListener
{
TextArea box1;
TextArea box2;
Button one;
public void init()
{
?box1 = new TextArea("Fiddle Dee Dee!", 20, 50);
?box2 = new TextArea(" ", 20, 50);
?one = new Button("Lowercase");
?one.AddActionListener(this);
?add(box1);
?add(one);
?add(box2);
?
?
}
public void actionPerformed(ActionEvent e)
?
{
?String box1text = box1.getText();
?box1text.toLowerCase();
?box2.setText("HAHA!");
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
My current error message is this
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
---------- Javac ----------
Div.java:20: cannot resolve symbol
symbol ?: method AddActionListener (Div)
location: class java.awt.Button
?one.AddActionListener(this);
? ? ? ? ? ? ? ? ? ^
1 error
Output completed (1 sec consumed) - Normal Termination
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Hopefully that will be my final impediment...
{Edit}
Okay, after a little fiddling I've managed to get it to compile but now my button doesn't do a thing <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html//emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif' /><!--endemo-->
My aim is to get it to make any input all lowercase, and then modify so that it appends words so that if CHEESE is in box1 and MICE are in box2, then MICE CHEESE appears in box2 when the button is pressed.
Now, here's where things get interesting...
gah, beaten by ninja edit. Hang on, need to think <!--emo&;)--><img src='http://www.unknownworlds.com/forums/html//emoticons/wink.gif' border='0' style='vertical-align:middle' alt='wink.gif' /><!--endemo-->
[edit]...ok. Firstly, make sure that <span style='font-family:Courier'>actionPerformed()</span> is actually being called. It probably isn't, but we knew that. Just stick an output statement in there so that it prints something if it's invoked.
Now, I'm not familiar with the class hierarchy you're using, but I'll make some guesses.... hm...
Are you sure that box2's text isn't changing? I can't remember whether <span style='font-family:Courier'>.toLowerCase()</span> converts the string or simply returns the lowercase version of the string; if it's the latter, then that code won't change <span style='font-family:Courier'>box1</span> anyway.
I'm really not sure. I assume that <span style='font-family:Courier'>actionPerformed()</span> is defined in some base class, and you're overloading it to get polymorphism working. I'd assume that the base is abstract, and that it wouldn't let you compile if you hadn't defined <span style='font-family:Courier'>actionPerformed()</span>, ruling out your overloading being slightly incorrect for some reason, but maybe the base isn't abstract. Will it compile without <span style='font-family:Courier'>actionPerformed</span>? (And without the line <span style='font-family:Courier'>one.AddActionListener(this);</span>).
Make sure that it's not supposed to be <span style='font-family:Courier'>ActionPerformed()</span>, with a capital A?
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Div extends Applet implements ActionListener
{
TextArea box1;
TextArea box2;
Button one;
public void init()
{
box1 = new TextArea("Fiddle Dee Dee!", 20, 50);
box2 = new TextArea(" ", 20, 50);
one = new Button("Lowercase");
one.addActionListener(this);
add(box1);
add(one);
add(box2);
}
public void actionPerformed(ActionEvent e)
{
box1.setText((box1.getText()+box2.getText()).toLowerCase());
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
edit
change that line in actionperformed to
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
box2.setText((box1.getText()+box2.getText());
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
for that micecheese thing
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
---------- Javac ----------
Div.java:20: cannot resolve symbol
symbol : method AddActionListener (Div)
location: class java.awt.Button
one.AddActionListener(this);
^
1 error
Output completed (1 sec consumed) - Normal Termination
<!--c2--></td></tr></table><div class='postcolor'><!--ec2--> <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
A simple mistake - it should be one.addActionListener, not one.AddActionListener. I'm guessing you just tried cutting that line out, in which case, ActionPerformed doesn't get called when clicking the "one" button. (And so nothing happens when you press it in the applet.)
While we're at it, what do people know about canvas? I want to create a simple boardgame type dealie in Swing, and I can draw the squares easily enough - however, it's confining them to a panel/canvas that's the problem. I want it so that if the board is too big, it won't splurge out onto the rest of the applet. Anyone got any ideas?
pilemuis, that code worked beautifully for the appelation (is that the right word? D: ), I'll see if I can modify that code to get toLowerCase() going.
Final problem, heres a screen of the applet in action. although I wont have any marks penalised for a problem of this nature the layout just... irks me. Is there any way I can clean it up?
Thanks to everyone who helped me out so far, I would probably have cracked my skull over that problem for a while D:
EDIT: Have a butcher's.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Div extends Applet implements ActionListener
{
TextArea box1;
TextArea box2;
Button one;
//Declare panels
Panel box1Panel, box2Panel, onePanel;
public void init()
{
box1 = new TextArea("Fiddle Dee Dee!");
box2 = new TextArea(" ");
one = new Button("Lowercase");
//Add the panels
box1Panel = new Panel();
box2Panel = new Panel();
onePanel = new Panel();
one.addActionListener(this);
// Add the components to their panels
box1Panel.add(box1);
box2Panel.add(box2);
onePanel.add(one);
/* Now we add the panels themselves, using the default layout (FlowLayout).
This fits them in left to right, where it can. There are others,
like GridLayout, but FlowLayout should do here.*/
add(box1Panel);
add(box2Panel);
add(onePanel);
}
public void actionPerformed(ActionEvent e)
{
box1.setText((box1.getText()+box2.getText()).toLowerCase());
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
I got the following result. Hope it's ok.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
box2 = new TextArea(" ", 20, 50);
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
bits instead, still, your solution is excellent and I'll be saving that for future reference <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
On to a different problem though, I'm now trying my hand at graphical applications and my first problem seems simple enough, first, heres my code:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Div2 extends Applet implements ActionListener
{
Button button1;
double r = Math.random()*128;
double b = Math.random()*128;
double g = Math.random()*128;
public void init()
{
setBackground(new Color((int)r, (int)b, (int)g));
setSize(300,100);
button1 = new Button("Color My World");
add(button1);
button1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Random colors come up, but the repaint function does not appear to be working as the applet does not change color when I press the button. This could be to do with the fact that Opera does not refresh the applet every time it is run.
r = Math.random()*128;
b = Math.random()*128;
g = Math.random()*128;
in the ActionEvent.
Also, I believe repaint(); works by recalling the paint method - which you haven't overriden. Perhaps something like
public void paint(Graphics g)
{
super.paint(g);
setBackground(new Color((int)re, (int)bl, (int)gr));
}
would do. (Notice the r, g, b variables are renamed - g is already taken in Graphics).
I can tell my comp is playing up, because I told it to add a textfield to the applet - and it refuses to do so. I'll get back to you in a few mins.
EDITED EDIT: The paint thing is unneccessary. You don't need to use paint or repaint at all. So, the only things you were doing wrong were not changing the colour variables to a random number each time the mouse is clicked; and calling repaint() instead of just asking it to set the background colour again. I've amended the code accordingly. Since paint isn't used, I've called the colours r, b and g again.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Div2 extends Applet implements ActionListener
{
Button button1;
double r = Math.random()*128;
double b = Math.random()*128;
double g = Math.random()*128;
public void init()
{
setBackground(new Color((int)r, (int)b, (int)g));
setSize(300,100);
button1 = new Button("Color My World");
add(button1);
button1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
r = Math.random()*128;
b = Math.random()*128;
g = Math.random()*128;
setBackground(new Color((int)r, (int)b, (int)g));
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
The only thing I'm sorry about is that I've asked this all 14 hours before my programming exam <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html//emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif' /><!--endemo-->
Ah well, thanks again this has helped me immensley, I've gone from knowing next to nothing about applets to getting them working again, thank you thank thank you (am I making the gratitude clear?) <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
Well, wish me luck, and thanks again.
Thanks are not neccessary, although appreciated. I'd never have been able to understand Java if I hadn't had help from people with more know-how. (: