Results 1 to 11 of 11
Thread: Displaying an image
- 11-06-2010, 07:22 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
Displaying an image
I want the picture to show for a few seconds and then go away....
it does show, but it doesnt want to go away...
frame.setVisible(false); does not work...
please help me!!
Java Code:import javax.swing.*; import java.awt.*; public class GoeiePunt extends JFrame { public void showImage() { JFrame frame = new JFrame("VOLPUNTE! Baie geluk!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 500); frame.setResizable(false); frame.setLocationRelativeTo(null); String imgStr = "PIC.jpg"; ImageIcon image = new ImageIcon(imgStr); JLabel label1 = new JLabel(" ", image, JLabel.CENTER); frame.getContentPane().add(label1); frame.validate(); frame.setVisible(true); for(int tel = 1; tel < 9999990;tel++) { } } GoeiePunt() { showImage(); } }Last edited by Eranga; 11-06-2010 at 12:30 PM. Reason: moderator edit: code tags added
- 11-06-2010, 11:02 AM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 139
- Rep Power
- 0
Hey Samari! Glad to see you found the forum... Please use code tags to put in code: Unformatted code is really hard to read. Click on edit at the bottom of your post an put it in. Here is the link on how to do this: Java Forums - BB Code List
Hope you find this useful.Last edited by Johanis; 11-06-2010 at 11:03 AM. Reason: us = is
- 11-06-2010, 12:33 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please use code tags next time when you are posting code segments over here in the forum. Unformatted codes are really hard to read.
Regarding your question, use swing timer instead serVisible invoke.
-
I've added code tags for you, but I'm curious that you say that setVisible(false) doesn't work, yet I don't see where you have used setVisible(false) in the code above.
Myself, I'd use a Swing Timer if I wanted to show something for a set period of time, and I probably wouldn't display it in a JFrame but more likely a JDialog or a JPanel in the main JFrame.
- 11-06-2010, 12:44 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-06-2010, 02:13 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
Thank you...I deleted the setVisible(false) because it did not work.
As i only started programming a while ago I dont really know how to do everything. Could you maybe explain to me how to use a Swing Timer?
-
When that happens, we ask that you post the non-working code so that we can see what you're doing wrong. Otherwise we have no idea really.
I can do better than that. I can link you to the tutorial where you'll find information and sample code: Swing Timer TutorialAs i only started programming a while ago I dont really know how to do everything. Could you maybe explain to me how to use a Swing Timer?
Much luck!
- 11-06-2010, 04:36 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
I used a swing timer.
It still doesnt seem to be working.
Could you help me find my mistake?
Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.Timer; import javax.swing.*; import java.awt.event.*; public class Volpunte extends JFrame { public void showImage() { JFrame frame = new JFrame("VOLPUNTE! Baie geluk!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 500); frame.setResizable(false); frame.setLocationRelativeTo(null); String imgStr = "PIC.jpg"; ImageIcon image = new ImageIcon(imgStr); JLabel label1 = new JLabel(" ", image, JLabel.CENTER); frame.getContentPane().add(label1); frame.validate(); } public void Timer() { ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { showImage(); } } ; Timer timer = new Timer(500, actionListener); timer.start(); } Volpunte() { Timer(); } }
-
Your timer is repeatedly calling the showImage() method every 500 ms so I imagine that you're displaying a bunch of JFrames with images in them, I don't see anywhere where you make the image disappear, and I don't think this is what you want. I recommend that you display your image in your main program, then create a Timer has as its time delay the amount of time you wish to display the image and that is set to not repeat (the Timer tutorial and API will help you to see how to do this). In the timer ActionListener's actionPerformed method you'll want to have your code that will make the image disappear. Then after you have displayed your image call start() on the timer and in xxx ms (whatever the timer delay has been set to), it will call your methods to make the image go away. Please look at the examples in the tutorial as they will help.
LuckLast edited by Fubarable; 11-06-2010 at 04:47 PM.
- 11-06-2010, 07:39 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
Thank you =)
-
Similar Threads
-
Displaying an image
By SWEngineer in forum AWT / SwingReplies: 11Last Post: 01-28-2010, 05:48 AM -
Trouble Displaying Image
By LankanSniper in forum Java 2DReplies: 3Last Post: 12-10-2009, 09:26 AM -
Displaying image in same jsp
By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-08-2008, 06:01 AM -
Displaying an image in a group
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 08:01 PM -
Displaying different image types
By splinter64uk in forum AWT / SwingReplies: 1Last Post: 12-05-2007, 08:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks