Usuwanie obrazków w javie

0

Witam mam problem nie wiem jak usunąć obrazek w javie obrazek dodaje przez Jlabel

		JLabel errorimg = new JLabel(new ImageIcon("image/error.png"));
		errorimg.setBounds(50,190,200,200);
		add(errorimg);

chce zrobić powrót do panelu logowania lecz nie moge usunąć tego obrazka metoda remove(errorimg); nie działa

Proszę o pomoc

0

Wrzucaj kod do znaczników.

errorimg.setVisible(false);
0
Kandif napisał(a):

Wrzucaj kod do znaczników.

errorimg.setVisible(false);

coś nie działa wywala błąd

0

Podaj błąd, w ogóle podaj więcej kodu.

Ewentualnie usuń starą metodą, ale dodaj :

        remove(errorimg);
        revalidate();
        repaint();
0
Kandif napisał(a):

Podaj błąd, w ogóle podaj więcej kodu.

Ewentualnie usuń starą metodą, ale dodaj :

        remove(errorimg);
        revalidate();
        repaint();

Błąd jest następujący

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at modul_1.program.actionPerformed(program.java:206)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
	public void error(){
		setSize(760,500);
		setTitle("nazwa");
		setLayout(null);
		images();
		remove(login);
		remove(plogin);
		remove(phaslo);
		remove(pass);
		remove(zaloguj);
		remove(user);
		remove(title);
		getContentPane().setBackground(Color.WHITE);
		errortitle = new JLabel("Nazwa » Error");
		errortitle.setBounds(230, 90, 500,60);
		errortitle.setForeground(new Color(0,200,200));
		errortitle.setFont(new Font("SansSerif", Font.BOLD, 40));
		add(errortitle);
		
		error = new JLabel("Error !");
		error.setBounds(380,160,200,200);
		error.setFont(new Font("SanSerif", Font.BOLD,20));
		error.setForeground(Color.RED);
		add(error);
		
		errorinfo = new JLabel("Niepoprawny login lub hasło !");
		errorinfo.setBounds(290,220,400,200);
		errorinfo.setFont(new Font("SanSerif", Font.BOLD,20));
		errorinfo.setForeground(Color.RED);
		add(errorinfo);
		
		back = new JButton("Powrot");
		back.setBounds(240,380,260,50);
		back.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.darkGray));
		back.setBackground(Color.DARK_GRAY);
		back.setFont(new Font("SansSerif", Font.ITALIC,20));
		back.setForeground(new Color(0,200,200));
		back.addActionListener(this);
		add(back);
		
		JLabel errorimg = new JLabel(new ImageIcon("image/error.png"));
		errorimg.setBounds(50,190,200,200);
		add(errorimg);
	}
0

Etykiete "errorimg" tworzysz lokalnie w metodzie error, dlatego nie masz do niej dostępu z innego miejsca, więc program wyrzuca wyjątek NullPointerException.

0
Kandif napisał(a):

Etykiete "errorimg" tworzysz lokalnie w metodzie error, dlatego nie masz do niej dostępu z innego miejsca, więc program wyrzuca wyjątek NullPointerException.

hmm czyli powinienem zrobić takie coś

final JLabel errorimg = new JLabel(new ImageIcon("image/error.png"));

Dziękuje za pomoc działa :D

1 użytkowników online, w tym zalogowanych: 0, gości: 1