FAQs
Search
Recent Topics
Flagged Topics
Hot Topics
Best Topics
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Liutauras Vilda
Bear Bibeault
Devaka Cooray
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Campbell Ritchie
Henry Wong
Saloon Keepers:
Tim Moores
Ron McLeod
salvin francis
Stephan van Hulst
Vijitha Kumara
Bartenders:
Tim Holloway
Carey Brown
Frits Walraven
Forum:
Swing / AWT / SWT
JLabels are not visible
Girish Singhal
Greenhorn
Posts: 1
posted 6 years ago
Here is my code:
import java.awt.*; import java.awt.Container; import java.awt.event.*; import javax.swing.*; import java.io.*; import static java.awt.BorderLayout.*; public class NetworkMap { private int x=0, y=0; private JFrame mainFrame; private JLabel star1 = new JLabel("starIcon"); private JLabel star2 = new JLabel("starIcon"); private JPanel mapPanel = new JPanel(null); public static final int WINDOW_WIDTH = 800; // pixels public static final int WINDOW_HEIGHT = 600; public NetworkMap() { this.x = 100; this.y = 100; mainFrame = new JFrame("Network Map"); mainFrame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); star1.addMouseMotionListener(new moveIconHandler()); star1.setBounds(x,y,star1.getIconWidth(), star1.getIconHeight()); mapPanel.add(star1); this.x+=100; this.y+=100; star2.addMouseMotionListener(new moveIconHandler()); star2.setBounds(x,y,star2.getIconWidth(), star2.getIconHeight()); mapPanel.add(star2); mainFrame.add(mapPanel); mainFrame.setVisible(true); } private class moveIconHandler extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { System.out.println("x = " + e.getX() + ", y = " + e.getY()+ "\n"); Component c = e.getComponent(); c.setLocation( c.getX()+e.getX(), c.getY()+e.getY() ); mainFrame.repaint(); } } public static void main(String[] args) { NetworkMap mymap = new NetworkMap(); } }
why my JLabels are not visible on the frame.
Mohamed Sanaulla
Bartender
Posts: 3185
34
I like...
posted 6 years ago
There seems to be some issue with the code- There is not getIconHeight, getIconWidth for the JLabel. You might be trying to use getIcon().getIconXXX().
Mohamed Sanaulla |
My Blog
| Author of
Java 9 Cookbook
Ranganathan Kaliyur Mannar
Bartender
Posts: 1104
10
I like...
posted 6 years ago
1
Your code doesn't even compile. First, post the correct code.
Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
It is sorta covered in the
JavaRanch Style Guide
.
Post Reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
2D Line & setCursor
Movable Objects
Semi-transparent drawing
Parallel dragging
GUI Issues. Ghosting when moving JLabel via Mouse
More...