Reputation: 267
I want to add an Image to the JFrame, the add( )
method is generating an error which says "cannot find symbol: method add(JLabel)" ... How can I fix that?
** I still haven't called the ImageLoading( )
method from the main method.
import javax.swing.*;
public class NetworkingGame {
NetworkingGame(){
JFrame jfrm = new JFrame("Angry Painters");
jfrm.setSize(800, 480);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setVisible(true);
}
public void ImageLoading(){
ImageIcon i = new ImageIcon("angry-painters.jpg");
JLabel jl = new JLabel(i);
add(jl); //The error is in this line
}
public static void main(String[] args) throws Exception{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new NetworkingGame();
}
});
}
}
Upvotes: 1
Views: 213
Reputation: 15664
You are calling the add
method as if it was an instance method in your class NetworkingGame
and your class doesn't have any method, so you are getting the compiler error as the compiler can't find any method of such a name in your class NetworkingGame
, so add it to the Jframe instead.
NetworkingGame(){
JFrame jfrm = new JFrame("Angry Painters");
jfrm.setSize(800, 480);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setVisible(true);
ImageIcon i = new ImageIcon("angry-painters.jpg");
JLabel jl = new JLabel(i);
jfrm.add(jl);
}
Upvotes: 2
Reputation: 9579
Visibility of JFrame jfrm
is limited by constructor of NetworkingGame
. So add
method does not exist in NetworkingGame
. Make your JFrame
member of NetworkingGame
.
public class NetworkingGame {
private JFrame jfrm;
NetworkingGame(){
jfrm = new JFrame("Angry Painters");
...
JLabel jl = new JLabel(i);
jfrm.add(jl);
Upvotes: 4
Reputation: 30147
Probably you should write
jfrm.add(jl);
and have jfrm
as a member.
In you case you really calling this.add()
which is not exist.
Upvotes: 3