Been trying to create a program which basically has a background (Which is painted using Paint() ) and then putting a Label with an Image in above that.
Keep getting the JLabel underneath the Paint…
Any ideas?
Thanks a lot in advance.
public class GUI extends JFrame {
JPanel menuBar = new JPanel();
JButton button1 = new JButton("Press Me");
JLayeredPane layeredPane = new JLayeredPane();
private ImageIcon image1;
private static JLabel label1;
public GUI() {
super("Add a profile");
setLayout(null);
try {
image1 = new ImageIcon(getClass().getResource(
"Images/location.PNG"));
} catch (Exception e) {
System.out.println("Image not found!");
}
label1 = new JLabel(image1);
label1.setBounds(new Rectangle(new Point(262, 94), label1.getPreferredSize()));
label1.setLocation(1, 1);
label1.setSize(114, 105);
add(label1);
}
public void paint(Graphics g) {
paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
// Menu Bar
g2d.setColor(Color.BLACK);
g2d.drawRect(60, 93, 190, 373);
g2d.setColor(Color.GRAY);
g2d.fillRect(61, 94, 189, 372);
// Background box
g2d.setColor(Color.BLACK);
g2d.drawRect(281, 106, 560, 360);
g2d.setColor(Color.GRAY);
g2d.fillRect(282, 107, 559, 359);
}
public static void main(String[] args) {
GUI gui = new GUI();
gui.setVisible(true);
gui.setSize(900, 550);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setResizable(false);
gui.setLocationRelativeTo(null);
}
}
Sadly can’t get it to work, thanks a bunch anyway
Oh i didn’t see the images you did! Sorry! i’ll have a look through now
Really appreciate it all!
From the looks of you’re code, you’re trying to use paint to do the work of other containers. I’d suggest, don’t.
Don’t override the
paintmethod of top level containers likeJFrame, these methods do to much important work for you to messy about it with and frames have a number of components already laid out on top of them normally rendering any of your work obsolete.Instead, create your self a custom component (extending from something like
JPanel) and use it’spaintComponentmethod instead THEN add this to the frame.In you’re case, you can get away with changing the background and border of the component and it will achieve the same result.
UPDATED
With just some basic layouts and a couple of components, I was able to produce this…
I’d suggest you would benefit from having a read through