Results 1 to 16 of 16
Thread: I need help?
- 06-29-2011, 07:11 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
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?
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> comesJava 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(); } }Last edited by gandotra; 06-29-2011 at 09:45 AM.
- 06-29-2011, 08:55 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 06-29-2011, 09:48 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
- 06-29-2011, 10:16 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 06-29-2011, 10:41 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
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
- 06-29-2011, 11:00 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What happens when change the class name? You shouldn't give your classes the same name as existing library classes
- 06-29-2011, 11:38 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
No, my file name is different from the class name.
- 06-29-2011, 11:48 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 06-29-2011, 11:59 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
What should i do?
- 06-29-2011, 12:03 PM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Start by changing your class name to something else(and constructors and old uses of it)
- 06-29-2011, 12:14 PM #11
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
I have changed my class name, constructor name and file name.
Now I am getting an error :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(); } }
What to do?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
- 06-29-2011, 12:25 PM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 06-29-2011, 12:32 PM #13
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
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?
- 06-29-2011, 12:36 PM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 06-29-2011, 12:54 PM #15
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
I did that but nothing happens, its just the same. Hmmm
- 06-29-2011, 01:51 PM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks