|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

11-04-2007, 05:16 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 5
|
|
|
Java Components are not displayed(sometimes)
My java applications consists of a 2 JTextArea, JButton, and 2 images.
When i execute my program, i have noticed that sometimes all these components dont get displayed.
For example, the 1st JTextArea gets displayed but the rest of the components are hidden. The JButton gets displayed when i move the mouse over it and the 2nd textarea gets displayed when i select its text.
I have included setVisible(True); at every possible and needed step.
This problem sounds strange. And it happens atleast once in 3 executions. Rest of the 2 executions seems just normal (as expected).
|
|

11-04-2007, 06:08 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,124
|
|
|
Let's see how you put your gui together.
|
|

11-05-2007, 06:53 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 5
|
|
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
public class welcome extends JFrame
{
JTextArea txt=new JTextArea("WELCOME"); //TEXT AREA
JTextArea txt1=new JTextArea("HELLO"); //TEXT AREA
JButton enter=new JButton(" ENTER "); //ENTER BUTTON
JLabel label_image1=new JLabel();
JLabel label_image2=new JLabel();
JPanel jp=(JPanel)this.getContentPane();
public static void main(String args[])
{
new welcome();
}
public welcome()
{
setExtendedState(getExtendedState() | Frame.MAXIMIZED_BOTH);//MAXIMIZES THE WINDOW ON EXECUTION
try
{
label_image1.setIcon(new ImageIcon("a4.jpg")); //SETS THE IMAGES OF THE LABELS
label_image2.setIcon(new ImageIcon("a5.jpg"));
jp.setLayout(null);//REQUIRED TO POSITION THE COMPONENTS
jp.setBackground(new Color(0,0,0)); //FRAME BACKGROUND SET TO BLACK
enter.setForeground(new Color(0,255,255)); //ENTER BUTTON FOREGROUND = LIGHT BLUE
txt.setForeground(new Color(255,0,0)); //FOREGROUND OF TEXT = RED
txt1.setForeground(new Color(255,0,0)); //FOREGROUND OF TEXT = RED
enter.setBackground(new Color(0,0,255)); //BACKGROUND OF BUTTON = DARK BLUE
txt.setBackground(new Color(0,0,0)); //BACKGROUND OF TEXT = BLACK
txt1.setBackground(new Color(0,0,0)); //BACKGROUND OF TEXT = BLACK
enter.setFont(new Font("Cooper Black",Font.PLAIN, 30)); //FONT OF ENTER BUTTON
txt.setFont(new Font("Courier", Font.BOLD, 48)); //FONT OF TEXT
txt1.setFont(new Font("Courier", Font.BOLD, 20)); //FONT OF TEXT
enter.setBounds(430,450,150,50); //PARAMETERS(X,Y,WIDTH,HEIGHT)
txt.setBounds(300,100,400,200); //SET POSITION AND SIZE OF THE COMPONENTS
txt1.setBounds(300,350,500,100); //SET POSITION AND SIZE OF THE COMPONENTS
label_image1.setBounds(50,50,100,200);
label_image2.setBounds(600,550,100,200);
enter.setBorder(BorderFactory.createLineBorder(Color.red, 10)); //CREATE BORDER FOR ENTER BUTTON AS RED
txt.setEditable(false); //SET TO UNEDITABLE
txt1.setEditable(false); //SET TO UNEDITABLE
setSize(800,600); //SET SIZE OF FRAME
setVisible(true);
jp.add(enter);
setVisible(true); //ADDED ALL THE COMPONENTS TO FRAME/PANEL
jp.add(txt);
setVisible(true);
jp.add(txt1);
setVisible(true);
jp.add(label_image1);
setVisible(true);
jp.add(label_image2);
setVisible(true);
}catch(Exception e){}
}
}
Do execute the code 4-5 times.. I have not sent the images.. It is supposed to diaplay 2 texts, 1 button and 2 images.
Last edited by JavaBean : 11-05-2007 at 09:14 AM.
Reason: Code placed inside [code] tag.
|
|

11-05-2007, 09:34 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,124
|
|
Works okay now: all components show up and remain visible.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Welcome extends JFrame
{
JTextArea txt=new JTextArea("Welcome");
JTextArea txt1=new JTextArea("Hello");
JButton enter=new JButton(" Enter ");
JLabel label_image1=new JLabel();
JLabel label_image2=new JLabel();
JPanel jp=(JPanel)this.getContentPane();
public static void main(String args[])
{
new Welcome();
}
public Welcome()
{
// setExtendedState(getExtendedState() | Frame.MAXIMIZED_BOTH);
label_image1.setIcon(new ImageIcon(//"a4.jpg"));
"images/Bird.gif"));
label_image2.setIcon(new ImageIcon(//"a5.jpg"));
"images/Rabbit.gif"));
jp.setLayout(null);
// jp.setBackground(new Color(0,0,0));
// enter.setForeground(new Color(0,255,255));
txt.setForeground(new Color(255,0,0));
txt1.setForeground(new Color(255,0,0));
// enter.setBackground(new Color(0,0,255));
// txt.setBackground(new Color(0,0,0));
// txt1.setBackground(new Color(0,0,0));
enter.setFont(new Font("Cooper Black",Font.PLAIN, 30));
txt.setFont(new Font("Courier", Font.BOLD, 48));
txt1.setFont(new Font("Courier", Font.BOLD, 20));
enter.setBounds(430,450,150,50); // ) <-- errant brace
txt.setBounds(300,100,400,200);
txt1.setBounds(300,350,500,100);
Dimension d = label_image1.getPreferredSize();
label_image1.setBounds(//50,50,100,200);
50,50,d.width,d.height);
d = label_image2.getPreferredSize();
label_image2.setBounds(//600,550,100,200); // was offscreen
50,350,d.width,d.height);
// enter.setBorder(BorderFactory.createLineBorder(Color.red, 10));
txt.setEditable(false);
txt1.setEditable(false);
jp.add(enter);
jp.add(txt);
jp.add(txt1);
jp.add(label_image1);
jp.add(label_image2);
setSize(800,600);
setLocation(100,100);
// Always call this method last.
setVisible(true);
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|