Click here to Skip to main content
15,868,419 members
Home / Discussions / Java
   

Java

 
QuestionError show ImageIcon ??? Pin
nghia09t39-Nov-12 19:34
nghia09t39-Nov-12 19:34 
I want to show ImageIcon by adding to Label, but it has error, help me?
<pre lang="java">import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
class CatchTheRat extends JFrame
{
// The Rat
JLabel lb;

// Move it randomly!
Random r;

public CatchTheRat(int k)
{

// Set frame properties
setTitle("Catch The Rat");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

// Set the background (just for a good look)
setContentPane(new JLabel(new ImageIcon("background.jpg")));

// Set layout to the content pane
getContentPane().setLayout(new FlowLayout());

// Create the rat
lb=new JLabel(new ImageIcon("rat.jpg"));

// Add the rat
getContentPane().add(lb);

// Create Random object
r=new Random();

// Create a timer and call it for every k seconds
new Timer(k,new ActionListener(){

public void actionPerformed(ActionEvent ae)
{

// Move the rat randomly, subtract 75, so that the rat should not meet the edges
lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));

}

}).start();

// Add mouselistener, notify when user clicks it!
lb.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent me)
{
// Create a beep sound when clicked to notify
Toolkit.getDefaultToolkit().beep();

// Also print it!
System.out.println("Caught!");
}

});

// Maximize the frame
setExtendedState(MAXIMIZED_BOTH);

}

public static void main(String args[])
{

// Create Scanner object
Scanner s=new Scanner(System.in);

// Let the user enter his capability of catching the rat!
System.out.println("Enter the speed");

// Read the input
int k=s.nextInt();

// Create the frame and send the value of k
new CatchTheRat(k);

}
}
</pre>
AnswerRe: Error show ImageIcon ??? Pin
Richard MacCutchan9-Nov-12 23:32
mveRichard MacCutchan9-Nov-12 23:32 
AnswerRe: Error show ImageIcon ??? Pin
TorstenH.10-Nov-12 14:02
TorstenH.10-Nov-12 14:02 
GeneralRe: Error show ImageIcon ??? Pin
Gowtham Gutha15-Nov-12 6:46
Gowtham Gutha15-Nov-12 6:46 
GeneralRe: Error show ImageIcon ??? Pin
TorstenH.15-Nov-12 9:00
TorstenH.15-Nov-12 9:00 
GeneralRe: Error show ImageIcon ??? Pin
Gowtham Gutha16-Nov-12 7:08
Gowtham Gutha16-Nov-12 7:08 
GeneralRe: Error show ImageIcon ??? Pin
TorstenH.18-Nov-12 19:10
TorstenH.18-Nov-12 19:10 
AnswerRe: Error show ImageIcon ??? Pin
Gowtham Gutha15-Nov-12 6:43
Gowtham Gutha15-Nov-12 6:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.