Author Topic: Java-scripters help me out here.  (Read 1148 times)

Trying to make my first Swing GUI but stuff's forgeted up.
This is my code, trying to make a GUI where there's a Text Area and 2 buttons. In which Button2 clears the Text Area, and Button1 checks if you did the math problem it starts with.

The problem was 7x57 which is 237, so I set the String variable, Coolvar to 237 fyi. Just clarifying

The main problem here is I keep getting this error at line 53,

And it's called " ')' expected, "

I tried adding that in but it provided nothing but the error again. I read that it is caused by incorrect bracketing or something but I couldn't find anything wrong.

Please help, here's my sourcecode.
Code: [Select]
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class TextCheck extends JFrame implements ActionListener
{
JButton Button1 = new JButton("Check Answer");
JButton Button2 = new JButton("Clear Text");
JTextArea Textbox1 = new JTextArea("How much is 5 x 47?");
JPanel bottomPanel = new JPanel();
JPanel holdAll = new JPanel();
String coolvar = "237";
public TextCheck()
{
bottomPanel.setLayout(new FlowLayout());
bottomPanel.add(Button2);
bottomPanel.add(Button1);

holdAll.setLayout(new BorderLayout());
holdAll.add(bottomPanel, BorderLayout.SOUTH);
holdAll.add(Textbox1, BorderLayout.CENTER);

getContentPane().add(holdAll, BorderLayout.CENTER);

Button1.addActionListener(this);
Button2.addActionListener(this);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args)
{
TextCheck myApplication = new TextCheck();

myApplication.setLocation(10, 10);
myApplication.setLocation(300, 300);

myApplication.setVisible(true);
JOptionPane.showMessageDialog(frame, "This is a test of a small thing that will test whether you can do basic math");
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == Button2) {
Textbox1.setText();
} if (e.getSource() == Button1) {
if (coolvar.equals(TextArea.getText())) {
TextBox1.setText("Correct!"); }
else
{
TextBox1.setText("Incorrect!");
}
}
}
}
[/size]
« Last Edit: October 23, 2010, 12:52:36 AM by SeventhSandwich »

I checked the brackets, quotes, and anything really. They're all correct.
I used to get several problems while using Java in school. No one could find what was causing said problems and I'd have to rewrite the thing.

...But this forgeter took me 3 hours of figuring out syntax errors.

Please try.

Found it.
derp one sec

Line 53 needs another )

         if (coolvar.equals(TextArea.getText()) {
         if (coolvar.equals(TextArea.getText())) {

Found it.
derp one sec
too late


too late
I did find it first, but I disliked his formatting and realized that my line number would've confused him so I ninja'd it.

Curious how that fixes it?
You have 3 ( and 2 ) on that line.

Curious how that fixes it?
if ( coolvar.equals(TextArea.getText() )
{

k...
But I have these errors now.
Code: [Select]
TextCheck.java:44: cannot find symbol
symbol  : variable frame
location: class TextCheck
JOptionPane.showMessageDialog(frame, "This is a test of a small thing that will test whether you can do basic math");
                              ^
TextCheck.java:44: cannot find symbol
symbol  : variable JOptionPane
location: class TextCheck
JOptionPane.showMessageDialog(frame, "This is a test of a small thing that will test whether you can do basic math");
^
TextCheck.java:50: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to ()
Textbox1.setText();
        ^
TextCheck.java:52: cannot find symbol
symbol  : variable TextArea
location: class TextCheck
if (coolvar.equals(TextArea.getText())) {
                   ^
TextCheck.java:53: cannot find symbol
symbol  : variable TextBox1
location: class TextCheck
TextBox1.setText("Correct!"); }
^
TextCheck.java:56: cannot find symbol
symbol  : variable TextBox1
location: class TextCheck
TextBox1.setText("Incorrect!");
^
6 errors

How did THESE get triggered and how can I fix it?


Code: [Select]
JOptionPane.showMessageDialog(frame, "This is a test of a small thing that will test whether you can do basic math");"frame" doesn't exist, but "this" (a TextCheck object) is a JFrame so changing it to "this" might work. Just try "null" if "this" fails.

Code: [Select]
JOptionPane.showMessageDialog(frame, "This is a test of a small thing that will test whether you can do basic math");Ditto.


Code: [Select]
Textbox1.setText();There is no setText(), but there is a setText(String a)
You can send an empty string to clear it: setText("");


Code: [Select]
if (coolvar.equals(TextArea.getText())) {Textbox1 is a TextArea, you can't refer to TextArea


Code: [Select]
TextBox1.setText("Correct!"); }You defined "Textbox1" but never "TextBox1"

Code: [Select]
TextBox1.setText("Incorrect!");Ditto

loving Java and it's capital letter sensitivity :(

One more question, when you say String a, can I replace a with really anything I want or do I have to set a string variable named a to the message I want?

One more question, when you say String a, can I replace a with really anything I want or do I have to set a string variable named a to the message I want?

Code: [Select]
String a = "abc";
Code: [Select]
String a;
a = "abc";
?

...What?

I'm confused, Elaborate.