I don't know how to add a JLabel
on top of a JPanel
. I want to display some information in a label on the mobile image. The image is in the JPanel
. I already managed to make the JFrame
undecorated.
Output http://i44.tinypic.com/2mhacsj.png
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
class Remote extends JFrame {
JPanel p2;
private Window Remote;
public Remote() {
createAndShowGUI();
}
public void createAndShowGUI() {
// Set title and default close operation
setTitle("Transparent Panel");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setUndecorated(true);
p2 = new JPanel();
JLabel label = new JLabel(new ImageIcon("C://Mobile.png"));
p2.add(label);
p2.setPreferredSize(new Dimension(1000, 800));
add(p2);
setLayout(new FlowLayout());
setSize(315, 610);
setVisible(true);
setLocation(800, 100);
}
public static void main(String args[]) {
// Run in the EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Remote remote = new Remote();
}
});
}
}
I have 3 Questions:
How do I remove the borders [background] surrounding the mobile?
How do I add some label in the
JPanel
?How to make the window movable?
This should meet your requirements:
For a draggable window, call this method from your
Remote()
constructor:Further notes:
JFrame.add()
use:getContentPane().add()
orsetContentPane()
AWTUtilities.setWindowOpaque()
removes the gray background of an undecorated frameJLayeredPane
can be used to stack components on top of each otherJLayeredPane
has to be done manuallyIt should now look something like this::