Results 1 to 4 of 4
- 11-04-2007, 04:16 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 5
- Rep Power
- 0
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, 05:08 PM #2
Let's see how you put your gui together.
- 11-05-2007, 05:53 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 5
- Rep Power
- 0
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.Java Code: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){} } }Last edited by JavaBean; 11-05-2007 at 08:14 AM. Reason: Code placed inside [code] tag.
- 11-05-2007, 08:34 AM #4
Works okay now: all components show up and remain visible.
Java Code: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); } }
Similar Threads
-
JCombox DropDown is displayed on primary monitor when the box is in between two monit
By Kalpana.k in forum AWT / SwingReplies: 1Last Post: 05-01-2008, 03:19 PM -
displayed the rownumbers
By geeta_ravikanti in forum JDBCReplies: 1Last Post: 04-22-2008, 02:30 AM -
Useful Java Application Components 1.0.0-b3
By JavaBean in forum Java SoftwareReplies: 0Last Post: 03-16-2008, 08:41 PM -
File Save not showing FileSAVE dialog box , getting displayed in browser
By deepdba in forum Java ServletReplies: 0Last Post: 12-06-2007, 06:10 PM -
Useful Java Application Components 0.9.27
By JavaBean in forum Java SoftwareReplies: 0Last Post: 10-02-2007, 04:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks