|
-
January 7th, 2007, 06:19 AM
#1
[ Beginner ] Code Errors
Hello Everyone,
This is the code of 2 seperate panels :
- the first panel contains 2 buttons + 2 textboxes
- the second panel contains 3 buttons (Erase, Change, and Exit)
When we enter some text in the first textbox , if we click on "change"
the data entered in textbox1 will be sent to textbox2.
I'm trying this code but unfortunately it is not working. Would someone
help me find the errors ? Thanks in advance.
Code:
package untitled5;
import java.awt.*;
import java.awt.event.*;
public class Echange extends Frame implements ActionListener {
Label l1=new Label("text1");
TextField t1=new TextField(20);
Label l2=new Label("text2");
TextField t2=new TextField(20);
Button ef=new Button("Effacer");
Button ech=new Button("Echanger");
Button fin=new Button("Fin");
public Echange(){
Panel p1=new Panel();
Panel p2=new Panel();
GridLayout g=new GridLayout(2,2);
FlowLayout f=new FlowLayout(1,3);
p1.setLayout(g);
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p2.setLayout(f);
p2.add(ef);
p2.add(ech);
p2.add(fin);
ef.addActionListener(this);
ech.addActionListener(this);
fin.addActionListener(this);
BorderLayout b=new BorderLayout();
this.setLayout(b);
this.add("North",p1);
this.add("Center",p2);
pack();
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
public void actionPerformed(ActionEvent e){
if(e.getSource()==ef){
t1.setText(" ");
t2.setText(" ");
}
else
if(e.getSource()==ech){
String s;
s=t1.getText();
t1.setText(t2.getText());
t2.setText(s);
}
else if(e.getSource()==fin){
System.exit(0);
}
}
}
public static void main(String[] args) {
new Echange();
}
}
Last edited by tima; January 7th, 2007 at 08:04 AM.
-
January 7th, 2007, 07:57 AM
#2
Re: [ Beginner ] Code Errors
 Originally Posted by tima
I'm trying this code but unfortunately it is not working. Would someone
help me find the errors ?
Can I persuade you to say in what way it isn't working?
From what I can see the code won't even compile.
If you are getting compiler errors, post up the full text of the error messages. Java error messages usually tell you exactly what's wrong.
If you are getting run time errors, post up the errors and the actual code you are using, because it can't be what you just posted.
If it gives no errors but just doesn't work, explain what isn't working and post the actual code you run.
Mistakes are the portals of discovery...
J. Joyce
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
January 7th, 2007, 08:03 AM
#3
Re: [ Beginner ] Code Errors
Error #: 204: illegal start of expression at line 53, column 3
Error #: 206: malformed expression at line 49, column 3
The lines are highlighted in the code .
-
January 7th, 2007, 11:23 AM
#4
Re: [ Beginner ] Code Errors
Well that's odd - the error messages you posted didn't look right, so I compiled the code you posted and got these (with both Java 1.4.2 and 1.5):
Information:Compilation completed with 2 errors and 0 warnings
Information:2 errors
Information:0 warnings
H:\Source\TEMP\src\untitled5\Echange.java
Error:Error:line (46)illegal start of expression
Error:Error:line (62)';' expected
The first is because you are declaring the actionPerformed method in the middle of the Echange constructor. Java doesn't allow methods to be declared inside methods. The second error is a result of the first error messing up the code structure.
Are you using Sun's standard javac compiler?
There are other errors in the code, e.g. FlowLayout doesn't have a constructor that takes 2 ints.
Act in haste and repent at leisure; code too soon and debug forever...
R. Kennington
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
January 7th, 2007, 01:16 PM
#5
Re: [ Beginner ] Code Errors
My version is JBuilder 4.0.152.0.
Thx a lot dlorde, it finally worked ! I* really appreciate your time and
ur kind cooperation 
Here's the right code:
Code:
package untitled5;
import java.awt.*;
import java.awt.event.*;
public class Echange extends Frame implements ActionListener {
Label l1=new Label("text1");
TextField t1=new TextField(20);
Label l2=new Label("text2");
TextField t2=new TextField(20);
Button ef=new Button("Effacer");
Button ech=new Button("Echanger");
Button fin=new Button("Fin");
public Echange(){
Panel p1=new Panel();
Panel p2=new Panel();
GridLayout g=new GridLayout(2,2);
FlowLayout f=new FlowLayout(FlowLayout.LEFT,1,3);
p1.setLayout(g);
p2.setLayout(f);
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p2.add(ef);
p2.add(ech);
p2.add(fin);
ef.addActionListener(this);
ech.addActionListener(this);
fin.addActionListener(this);
BorderLayout b=new BorderLayout();
this.setLayout(b);
this.add("North",p1);
this.add("Center",p2);
pack();
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ef){
t1.setText(" ");
t2.setText(" ");
}
else
if(e.getSource()==ech){
String s;
s=t1.getText();
t1.setText(t2.getText());
t2.setText(s);
}
else if(e.getSource()==fin){
System.exit(0);
}
}
public static void main(String[] args) {
new Echange();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|