Java Imagine in JPanel

Salut, este cineva care stie Java pe aici si ma poate ajuta cu aceasta problema… ma chinui de cateva ore bune si nu reusesc sa pun o imagine.

Clasa unde este problema este :

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MainWindow {
	
	public static JFrame mainFrame;
	public static JPanel loginRegisterPanel;
	
	
	public MainWindow() {
		MainFrame();
		LoginRegisterPanel();
	}
	
	public void MainFrame() {
		
		mainFrame = new JFrame();
		mainFrame.setSize(640, 480);
		mainFrame.setVisible(true);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setResizable(false);
		
	}
	
	public void LoginRegisterPanel() {
		
		loginRegisterPanel = new JPanel();
		loginRegisterPanel.setLayout(null);
		//loginRegisterPanel.setBackground(Color.RED);
		mainFrame.add(loginRegisterPanel);
		
		JButton loginButton = new JButton("Login");
		JButton registerButton = new JButton("Register");
		
		/*ImageIcon logoImage = new ImageIcon("logo.png");
		JLabel logoImageLabel = new JLabel(logoImage);
		logoImageLabel.setBounds(0, 0, 640, 200);
		loginRegisterPanel.add(logoImageLabel);*/
		
		loginButton.setBounds(260, 180, 120, 50);
		loginRegisterPanel.add(loginButton);
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					loginRegisterPanel.setVisible(false);
					LoginPanel loginPanel = new LoginPanel();
					mainFrame.getContentPane().add(loginPanel.loginP);
				}
				catch (Exception ce){
					ce.printStackTrace();
				}
			}
		});
		registerButton.setBounds(260, 250, 120, 50);
		loginRegisterPanel.add(registerButton);
		
	}

	public static void main (String[] args) {
		MainWindow mainWindow = new MainWindow();
	}
}

Nu reusesc sa pun acel logo.jpg in Panel… am incercat o multime de variante de pe google si tot nu imi merge. Am incercat pana si varianta oficiala: http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html fara folos.
Am verificat daca legatura este bune… si este, folderul in care am imaginea este facut fisier sura, cand testam cu varianta de la oracle, acolo primeam mesaj daca nu era legatura buna, dar este. Nu acolo e problema.

Nu imi dau seama de ce cand scriu aceasta linie de cod:

ImageIcon logoImage = new ImageIcon("logo.png");

fara alte lucruri si mai si rulez aplicatia, nu-mi mai apare nimic un element in Panel (dispar butoanele);
Am incercat varianta de GUI pentru eclipse, dar acolo imi pune imaginea direct pe Frame si eu nu vreau asta pentru ca am nevoie sa ma mut de pe un Panel pe altul.

Mda… dupa o groaza de timp am reusit sa vad unde era problema…

loginRegisterPanel.add(logoImageLabel);
logoImageLabel.setBounds(0, 0, 640, 200);

Bounds-ul trebuia pus dupa add. Nu era problema de cod…

1 Like