Results 1 to 16 of 16

Thread: I need help?

  1. #1
    gandotra is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default I need help?

    This is a program given in my college but i am unable to get an output of this program. I need help. Tell me what is wrong in this program?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class JPanel extends JFrame
    {       JFrame frame= new JFrame("This is my JFrame");
    	
    	JTextField txt1,txt2,txt3;
    	JLabel lbl1,lbl2,lbl3;
    	JButton btn1,btn2,btn3,btn4;
    	JPanel lblpanel,btnpanel;
    	JComboBox cmb1;
    	Object box[]={"name","age","salary"};
    
    public JPanel()
    {
    	
    
    	txt1=new JTextField(20);
    	txt2=new JTextField(4);
    	txt3=new JTextField(10);
    	
    	lbl1=new JLabel("Name");
    	lbl2=new JLabel("Age");
    	lbl3=new JLabel("Salary");
    	
    	btn1=new JButton("add");
    	btn2=new JButton("modify");
    	btn3=new JButton("delete");
    	btn4=new JButton("exit");
    	
    
    	lblpanel=new JPanel();
    	btnpanel=new JPanel();
    
    	
    	cmb1=new JComboBox(box);
    	
    	lblpanel.add(lbl1); lblpanel.add(txt1);
    	lblpanel.add(lbl2); lblpanel.add(txt2);
    	lblpanel.add(lbl3); lblpanel.add(txt3);
    	
    	lblpanel.setLayout(new GridLayout(3,2));
    
    	btnpanel.add(btn1);
    	btnpanel.add(btn2);	
    	btnpanel.add(btn3);
    	btnpanel.add(btn4);
    	add(cmb1);
    	
    	btnpanel.setLayout(new FlowLayout());
    	
    	add(lblpanel);
    	add(btnpanel);
    	
    	setLayout(new GridLayout(2,1));
    	
    	frame.setVisible(true);
    }
    
    
    public static void main(String[]args)
    {
    	new JPanel();
    }
    }
    It compiles correctly without giving an error but just when i a run the program, the program doesn't run. Some message like "at JPanel.<init><JPanel.java:33> comes
    Last edited by gandotra; 06-29-2011 at 10:45 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Post code in code tags, not quote tags
    [code]
    YOUR CODE HERE
    [/code]

    Also, post the entire stack trace(copy/paste in code tags, no paraphrasing), and tell us what you think is wrong.

  3. #3
    gandotra is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Post code in code tags, not quote tags
    [code]
    YOUR CODE HERE
    [/code]

    Also, post the entire stack trace(copy/paste in code tags, no paraphrasing), and tell us what you think is wrong.
    Post edited now program written in code tag.

    I don't know what is wrong, I said in my first post that "program compiles without giving an error but its not running" I need help to make this program run

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm assuming you've gotten an exception? What is the exact thing printed when run? (called stack trace)

    Also, change your class name, JPanel is a library class and you will definitely run into problems having them both have the same name.

  5. #5
    gandotra is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    I am not getting an exception but when i try to run the program. A message comes "at JPanel.<init><JPanel.java:33>". That is it

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens when change the class name? You shouldn't give your classes the same name as existing library classes

  7. #7
    gandotra is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    No, my file name is different from the class name.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you are running that class you will want the class to be the same as the file name, and probably should make the class public. That is not the problem though. Your class is named JPanel, and these is a library class in the javax.swing package named JPanel, you should not name your class the same.

  9. #9
    gandotra is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    What should i do?

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Start by changing your class name to something else(and constructors and old uses of it)

  11. #11
    gandotra is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    I have changed my class name, constructor name and file name.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    public class my extends JFrame
    {
    	
    	JTextField txt1,txt2,txt3;
    	JLabel lbl1,lbl2,lbl3;
    	JButton btn1,btn2,btn3,btn4;
    	JPanel lblpanel,btnpanel;
    	JComboBox cmb1;
    	Object box[]={"name","age","salary"};
    
    public my()
    {
    	JFrame frame=new JFrame("This is my JFrame");
    	
    
    	txt1=new JTextField(20);
    	txt2=new JTextField(4);
    	txt3=new JTextField(10);
    	
    	lbl1=new JLabel("Name");
    	lbl2=new JLabel("Age");
    	lbl3=new JLabel("Salary");
    	
    	btn1=new JButton("add");
    	btn2=new JButton("modify");
    	btn3=new JButton("delete");
    	btn4=new JButton("exit");
    	
    
    	lblpanel=new JPanel();
    	btnpanel=new JPanel();
    
    	
    	cmb1=new JComboBox(box);
    	
    	lblpanel.add(lbl1); lblpanel.add(txt1);
    	lblpanel.add(lbl2); lblpanel.add(txt2);
    	lblpanel.add(lbl3); lblpanel.add(txt3);
    	
    	lblpanel.setLayout(new GridLayout(3,2));
    
    	btnpanel.add(btn1);
    	btnpanel.add(btn2);	
    	btnpanel.add(btn3);
    	btnpanel.add(btn4);
    	add(cmb1);
    	
    	btnpanel.setLayout(new FlowLayout());
    	
    	add(lblpanel);
    	add(btnpanel);
    	
    	setLayout(new GridLayout(2,1));
    	
    	frame.setVisible(true);
    }
    
    
    public static void main(String[]args)
    {
    	new my();
    }
    }
    Now I am getting an error :

    D:\Extra\Java>javac my.java
    .\JPanel.java:4: duplicate class: my
    class my extends JFrame
    ^
    my.java:10: cannot access JPanel
    bad class file: .\JPanel.java
    file does not contain class JPanel
    Please remove or make sure it appears in the correct subdirectory of the classpa
    th.
    JPanel lblpanel,btnpanel;
    ^

    D:\Extra\Java>^A

    D:\Extra\Java>p

    What to do?

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What do you think the errors messages mean? Is there a JPanel.class file in the same directory as the my.java file? Is the file named my.java?

  13. #13
    gandotra is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Bingo! I just deleted my JPanel.class file and now the "my.java" file compiles. The output of the program is just showing the JFrame. I want the Buttons, Texts, Labels, ComboBox inside the JPanel and then show the JPanel in the JFrame.

    What is wrong with my program code, sunde887?

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try setting the layout before adding stuff and see what happens. Now that you got it compiling, don't be afraid to play around with the code and experiment.

  15. #15
    gandotra is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    I did that but nothing happens, its just the same. Hmmm

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Since the class is a jframe(inheritance) it shouldn't have a local variable, you are adding everything to the "this" object and then making the local frame visible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •