Author |
Message |
lord_pwnerer
|
Posted: Sat Oct 14, 2006 2:27 pm Post subject: Containre/JPanel problem |
|
|
Ok, I'm trying to add a tabbed pane to my container, that inturn will show different panels. This is the code I have so far,
(It's just going to be a simple program that calculated density, mass and volume using two of those varibles. I just want to make it to really learn how to use textboxes and buttons, containers and panels and stuff so that I can use it for future referance)
code: |
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class densityProgram extends JFrame
{
public densityProgram ()
{
super ("Density Program");
Container c = new Container (); //initaites a new container
JTabbedPane dmv = new JTabbedPane (); //initates tabbed panes
JPanel density = new JPanel (); //initiates a new panel
JPanel mass = new JPanel (); //initiates a new panel
JPanel volume = new JPanel (); //initiates a new panel
c.setLayout (new BorderLayout ()); //sets the layout of the container
c.add (dmv);
dmv.addTab ("Denisty", density);
dmv.addTab ("Mass", mass);
dmv.addTab ("Volume", volume);
setSize (300, 300); //sets the size of the panel
setVisible (true); //sets the visibilaty of the panel to true
setResizable (false); //sets the resizable option to false
setBackground (Color.black); //sets the background colour to black
}
public static void main (String args[]) throws IOException
{
densityProgram application = new densityProgram ();
application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
|
It runs, but nothing is shown. If anybody could help me understand what I'm doing wrong I'll truly be gratefull. Thanks. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Oct 14, 2006 2:43 pm Post subject: (No subject) |
|
|
Aside from atrocious code style, your problem is reasonably simple.
You inherit from JFrame. JFrame itself inherits from Container (via other parent classes). Therefore your class is itself a container.
What does an instance of your class contain? |
|
|
|
|
![](images/spacer.gif) |
lord_pwnerer
|
Posted: Sat Oct 14, 2006 3:28 pm Post subject: (No subject) |
|
|
Ok, I've hopefully neatened it up a bit, but I'm still having problems.
I believe this is the right way to do it (if it's wrong please let me know) but the compiler keeps on saying unexpected symbol ignored. I've tried to fix it but nothing's working. What should be happening if I'm not mistaken (if it would actually run) is that there should just be a blank window 300/300.
As soon as I get that running I think I know what to do.
code: |
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public static void main (String[] args
{
JFrame w = new JFrame ();
w.setVisible (true);
}
public class densityProgram extends JFrame
{
JPanel density = new JPanel ();
JPanel mass = new JPanel ();
JPanel volume = new JPanel ();
JTabbedPane dmv = new JTabbedPane ();
JTextField txt = new JTextField ();
public densityProgram ()
{
Container c = this.getContentPane ();
this.setTitle ("Density Calculator");
this.setBackground (Color.black);
this.setSize (300, 300);
this.setResizable (false);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Oct 14, 2006 4:06 pm Post subject: (No subject) |
|
|
Uhhh... if that's what you're tempted to replace your code with, just leave it like it was for now. At least in terms of code formatting.
The question still stands... your class is a Container. Contains have things added to them. What does your Container contain? |
|
|
|
|
![](images/spacer.gif) |
lord_pwnerer
|
Posted: Sat Oct 14, 2006 5:10 pm Post subject: (No subject) |
|
|
well if I kept it as it was, wouldn't the container contain the TabbedPane?
Because I added it to it. But it still didn't show. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Oct 14, 2006 6:05 pm Post subject: (No subject) |
|
|
Oh, that container (the one you call "c") is handled fine. Is that the same container I've been talking about? ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
lord_pwnerer
|
Posted: Sat Oct 14, 2006 6:17 pm Post subject: (No subject) |
|
|
apparantly not, and how would I go about handling the class to make it work? |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Oct 14, 2006 6:20 pm Post subject: (No subject) |
|
|
Well, there are two solutions to this dilemna. Both are based in realizing that your class is a Container. It can do anything Container can.
Can Containers add things to themselves?
Note: I could just give you the solution. I hope you are not overly frustrated by me asking you questions. My hope is that I can prompt you to discover the answer yourself. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
lord_pwnerer
|
Posted: Sun Oct 15, 2006 10:08 am Post subject: (No subject) |
|
|
well I assumed that, lol, I figured out my problem. I couldn't just add things to the frame. I had to use the getContentPane command to add the components. I think that I'll be able to do it. I'll post the code here when I get it working. Thanks |
|
|
|
|
![](images/spacer.gif) |
|