GUI question

3

3ashmawy

I have just started writing a simple gui application . I add to the
frame and ImageIcon(an image as the background) .then i added a
textArea . the textArea dissapears . Why ?

here is the code ...

import javax.swing.*;
import java.awt.*;


public class chatGUI {

JFrame frame = new JFrame();
JLabel contacts = new JLabel();
JTextArea sendRecieveArea = new JTextArea();
JTextArea typingArea = new JTextArea();
JLabel backgroundLabel;

public chatGUI()
{

frame.setLayout(null);
frame.setTitle("Chat");
frame.setBounds(300,200,500,500);


backgroundLabel = new JLabel(new
ImageIcon("graphics/background.gif"));
backgroundLabel.setBounds(0,0,500,500);
frame.add(backgroundLabel);


sendRecieveArea.setBounds(0,0,350,300);
sendRecieveArea.setLineWrap(true);
frame.add(sendRecieveArea);


typingArea.setLineWrap(true);
typingArea.setBounds(10,330,400,100);
frame.add(typingArea);


frame.setVisible(true);
}



public static void main(String args[])
{
chatGUI gui = new chatGUI();

}
}



Thanx in advance for your help.
3ashmawy
 
D

Daniel Pitts

3ashmawy said:
I have just started writing a simple gui application . I add to the
frame and ImageIcon(an image as the background) .then i added a
textArea . the textArea dissapears . Why ?

here is the code ... [snip code]

Thanx in advance for your help.
3ashmawy

The biggest thing I saw is that you should use
frame.getContentPane().add() instead of frame.add()

HTH,
Daniel.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

The biggest thing I saw is that you should use
frame.getContentPane().add() instead of frame.add()

I think they fixed that in 1.5 ...

Arne
 
A

Andrew Thompson

3ashmawy said:
I have just started writing a simple gui application . I add to the
frame and ImageIcon(an image as the background) .then i added a
textArea . the textArea dissapears . Why ?

here is the code ...

The problem starts...
public class chatGUI { ......
public chatGUI()
{

frame.setLayout(null);

...here.

If you layout these components (rather than simply
chucking them wherever you reckon they should go),
this problem will most likely disappear.

Note that if you follow Daniel's advice, the GUI
should be compatible with 1.4, and possibly 1.2 and
1.3 as well. It is a sensible change to make, unless
the project is already locked in to 1.5+ for other reasons.

Andrew T.
 
I

Ian Wilson

3ashmawy said:
I have just started writing a simple gui application . I add to the
frame and ImageIcon(an image as the background) .then i added a
textArea . the textArea dissapears . Why ?

1. You misunderstand how to set a background image.
I recommend you delete the image stuff until the rest works OK.

2. Create a JPanel and add your components to the panel, then finally
add the JPanel to the frame. Don't add anything else to the JFrame.

3. NEVER use setLayout(null).
Try "panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS);"

4. Delete all your setBounds() statements. Really.

5. Add "frame.pack();" just above "frame.setVisible(true);

If you do all the above, I am confident your program will appear fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top