JButton po wcisnieciu zmienie koloru tła JPanel

0

Witam, chciałbym żeby po wcisnieciu Button2 kolor JPanelu zmieniał się na dowolny kolor z przedziału ( Kod poniżej ) chciałbym to zrobić na classach takich jak JPanel i Button2, jestem początkującą osobą, więc jeśli ktoś jest chętny pomagać mi w zrozumieniu języka, to bardzo proszę o kontakt.

Klasa Frame

import javax.swing.JFrame;

public class Frame {
	
	public JFrame jframe;
	public Button1 button1;
	public Button2 button2;
	public Button3 button3;
	public RenderPanel renderpanel;
	public static Frame frame;
	
	public Frame() {
	
	JFrame jframe = new JFrame("Nauka");
	jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	jframe.setSize(600, 500);
	jframe.setLocation(400, 300);
	jframe.setResizable(false);
	jframe.add(renderpanel = new RenderPanel(button2));
	jframe.setVisible(true);

	}
	public static void main(String[] args) {
		frame = new Frame();
	}
	
	
	
}
 

Class'a RenderPanel

 
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JPanel;


@SuppressWarnings("serial")

public class RenderPanel extends JPanel 
{
	
	public RenderPanel renderpanel;
	public Button1 button1;
	public Button2 button2;
	public Button3 button3;
	public RenderPanel(){
		super.setLayout(new FlowLayout());
		super.add(button1 = new Button1());
		super.add(button2 = new Button2());
		super.add(button3 = new Button3());
		super.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		setBackground(Color.YELLOW);
	
	}		
	@Override
	public void paintComponent(Graphics g) 
	{
	super.paintComponent(g);
	//g.fillRect(0, 0, 600, 500);	
	}
}

Class'a Button2

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;


@SuppressWarnings("serial")
public class Button2 extends JButton
{
	
	private Random r = new Random();
    private int a = r.nextInt(6);	
	public RenderPanel renderpanel;
	public Button2()
	{	
		super("Przycisk 2");
		JButton button2 = new JButton();
		button2.addActionListener(new ActionListener()
		{    
		    public void actionPerformed(ActionEvent e) 
		    {
			if (a==0){
		    	  
		    	  renderpanel.setBackground(Color.BLACK);
		    	  renderpanel.repaint();
			}else if (a==1){
		    	  		    	 
		          renderpanel.setBackground(Color.YELLOW);
		          renderpanel.repaint();
		    }else if (a==2){
		    	  
		    	  renderpanel.setBackground(Color.MAGENTA);
		    	  renderpanel.repaint();
		    }else if (a==3){
		    	 
		    	  renderpanel.setBackground(Color.RED);
		    	  renderpanel.repaint();
		    }else if (a==4){
		    	  
		          renderpanel.setBackground(Color.WHITE);
		          renderpanel.repaint();
		    }else if (a==5){
		    	 
		    	  renderpanel.setBackground(Color.ORANGE);
		    	  renderpanel.repaint();
		          
		    }
		   }
		});
	}
} 
3

Na początek polecałbym zacząć krok po kroku realizować jakiś dobry kurs z podstaw javy. Dowiedzieć się jak używać public / private i do czego służy konstruktor klasy. Napisać kilka konsolowych programów, żeby nabrać obycia z językiem a wtedy dużo łatwiej będzie Ci pisać kod, który będzie realizował to co sobie zamierzyłeś. Ten wklejony powyżej ani nie ma prawa działać ani realizować tego co chcesz a błędów jest w nim tak dużo, że ciężko by było wszystkie wypisać. Zacznij korzystać też z IDE - pomoże Ci poprawić co najmniej połowę z nich. Powodzenia :)

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