Results 1 to 6 of 6
Thread: java program
- 07-10-2007, 02:19 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 5
- Rep Power
- 0
java program
Hi friends ,Java Code:import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class Anu extends JApplet { JLabel j1, j2,j3, j4; JTextField t1,t2,t3,t4; JButton b1,b2; JFrame f1; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); j1=new JLabel("enter your name"); c.add(j1); t1=new JTextField(10); c.add(t1); j2=new JLabel("enter your age"); c.add(j2); t2=new JTextField(10); c.add(t2); j3=new JLabel("enter your sex"); c.add(j3); t3=new JTextField(10); c.add(t3); j4=new JLabel("enter your email id"); c.add(j4); t4=new JTextField(10); c.add(t4); b1=new JButton("Submit"); c.add(b1); b2=new JButton("Cancel"); c.add(b2); } b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { t1.setText(" "); t2.setText(" "); t3.setText(" "); t4.setText(" "); } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String rin; rin=t4.getText(); if (rin=="rinki") JOptionPane.showMessageDialog(f1,"great"); else JOptionPane.showMessageDialog(f1,"bad"); } }); };
When I execute the above program its giving me following error
b1.addActionListener(new ActionListener() {
^
<identifier> expected
b2.addActionListener(new ActionListener(){
^
2 errors
Please help me out..................dont know why i am getting this error...
ThaksLast edited by JavaBean; 07-10-2007 at 12:48 PM. Reason: Code placed inside [code] tag!
- 07-10-2007, 12:50 PM #2
Because those lines are not inside any method of the class! You can't place such statements inside the class directly. Your code should be inside methods.
Move those lines inside init method and try again.
- 07-10-2007, 07:16 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 5
- Rep Power
- 0
Thanks for the suggestion You are correct that was a mistake....
but when i put it inside the program i am getting following error....
==================================================
anu.java:6: anu is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class anu extends JApplet implements ActionListener
================================================== ====
just pasting the whole program again
Java Code:import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class anu extends JApplet implements ActionListener { JLabel j1, j2,j3, j4; JTextField t1,t2,t3,t4; JButton b1,b2; JFrame f1; JPanel p1; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); /*p1.add(f1);*/ f1.add(c); f1.setVisible(true); j1=new JLabel("enter your name"); f1.add(j1); t1=new JTextField(10); f1.add(t1); j2=new JLabel("enter your age"); f1.add(j2); t2=new JTextField(10); f1.add(t2); j3=new JLabel("enter your sex"); f1.add(j3); t3=new JTextField(10); f1.add(t3); j4=new JLabel("enter your email id"); f1.add(j4); t4=new JTextField(10); f1.add(t4); b1=new JButton("Submit"); f1.add(b1); b2=new JButton("Cancel"); f1.add(b2); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { t1.setText(" "); t2.setText(" "); t3.setText(" "); t4.setText(" "); } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String rin; rin=t4.getText(); if (rin=="rinki") JOptionPane.showMessageDialog(f1,"great"); else JOptionPane.showMessageDialog(f1,"bad"); } }); } };Last edited by JavaBean; 07-10-2007 at 08:20 PM. Reason: Code is placed inside code tag.
- 07-10-2007, 08:24 PM #4
Nehasahu, first of all please use [code] tag around your codes. That will make your codes more readable.
And in your code, your anu class is declared to implement ActionListener but it does not override public void actionPerformed(ActionEvent e). That is the meaning of the error you see.
You have two choices:
1. Combine your existing actionPerformed methods inside anu class as a separate method and handle all your actions inside that method.
2. Remove "implements ActionListener" text from your anu class and continue with your already written action listeners.
- 07-12-2007, 08:19 AM #5
Member
- Join Date
- Jul 2007
- Posts
- 5
- Rep Power
- 0
i have removed "implements ActionListener" text from your anu class but still my program is not working its giving me a new error Applet not initialized...i am getting a blank applet..
- 07-12-2007, 11:05 AM #6
The applet should return an exception. You need to learn the details about the exception. What is it and which line the exception is being thrown? These details should be shown on Java Console. Check following resources:
Java Plug-in Console
Enable and View Java Console Window - 1.4.2_xx
After knowing the details about the exception, we can advise you a solution.
Similar Threads
-
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
New to Java Program
By jvasilj1 in forum New To JavaReplies: 1Last Post: 02-05-2008, 07:22 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM -
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:40 AM -
help with java program
By mattvgt in forum New To JavaReplies: 3Last Post: 07-14-2007, 04:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks