MasterMind zmiana ikon na przyciskach

0

Witam, mam do napisania grę Mastermind ale utknąłem w punkcie w którym nie wiem jak przesłać dalej wybrane przyciski. Chodzi mi o to, że po otworzeniu metody wybieraczKolorow() nie potrafię aby przyciski jButon1 ... jButon4 zmieniły swoje kolory.
przesyłam kod

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Gra extends JFrame implements ActionListener{
    
    JButton cztery, piec, wyjscie;
    JButton jButton1, jButton2, jButton3,jButton4,jButton5,jButton6,jButton10;
    ImageIcon[] ikony =  {new ImageIcon("czerwony.png"), new ImageIcon("fioletowy.png"),
            new ImageIcon("niebieski.png"),new ImageIcon("pomaranczowy.png"),new
             ImageIcon("rozowy.png"),new ImageIcon("turkusowy.png"),
            new ImageIcon("zielony.png"),new ImageIcon("zolty.png")};
    JButton[] przyciski = new JButton[8];
    public Gra(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        initComponents();
    }
     private void initComponents() {

            cztery = new javax.swing.JButton();
            piec = new javax.swing.JButton();
            wyjscie = new javax.swing.JButton();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("MisterMind");

            cztery.setText("4");
            cztery.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    czteryActionPerformed(evt);
                }

                
            });

            piec.setText("5");

            wyjscie.setText("Zamknij");

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(231, Short.MAX_VALUE)
                    .addComponent(cztery)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(piec)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(wyjscie)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(40, 40, 40)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cztery)
                        .addComponent(piec)
                        .addComponent(wyjscie))
                    .addContainerGap(237, Short.MAX_VALUE))
            );

            pack();
        }
     private void czteryActionPerformed(ActionEvent evt) {
             czteryOkno();
             
             
            
        }
     public static void main(String[] args){
         new Gra().setVisible(true);
     }
     private void czteryOkno()
     {
         final JFrame okno = new JFrame();
         okno.setVisible(true);
            jButton1 = new javax.swing.JButton();
            jButton1.addActionListener(this);
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jButton5 = new javax.swing.JButton();
            jButton6 = new javax.swing.JButton();

            okno.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            jButton1.setIcon(new javax.swing.ImageIcon("Pusty.png")); // NOI18N
            jButton1.setBackground(Color.white);
            jButton1.setBorderPainted(false);
            jButton2.setIcon(new javax.swing.ImageIcon("Pusty.png")); // NOI18N
            jButton2.setBackground(Color.white);
            jButton2.setBorderPainted(false);
            jButton3.setIcon(new javax.swing.ImageIcon("Pusty.png")); // NOI18N
            jButton3.setBackground(Color.white);
            jButton3.setBorderPainted(false);
            jButton4.setIcon(new javax.swing.ImageIcon("Pusty.png")); // NOI18N
            jButton4.setBackground(Color.white);
            jButton4.setBorderPainted(false);
            jButton5.setText("Wstaw");

            jButton6.setText("Wyjście");
            

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(okno.getContentPane());
            okno.getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton4))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton6)))
                    .addContainerGap(72, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jButton4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton5)
                        .addComponent(jButton6))
                    .addContainerGap(204, Short.MAX_VALUE))
            );
jButton6.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    okno.dispose();
                    
                }
            });
            okno.pack();
         
     }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==jButton1)
        {
            System.out.println("cos sie stalo");
            jButton1.setIcon(wybieraczKolorow());
            
        }
        
        
    }
    private ImageIcon wybieraczKolorow() {
        ImageIcon ikonka;
        JFrame wybieracz= new JFrame();
        wybieracz.setVisible(true);
        wybieracz.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        wybieracz.setLayout(new GridLayout(2, 4));
        for(int i=0 ;i<ikony.length;i++)
        {
            przyciski[i] = new JButton(ikony[i]);
            wybieracz.getContentPane().add(przyciski[i]);
        }
        wybieracz.pack();
        return ikony[2];
        
    }
    
    
    
     
}

1

Zasad gry nie znam, ale w metodzie wybieraczKolorów masz referencje do JFrame:

JFrame wybieracz= new JFrame();

To ci stworzy całkiem nowe okno. Jeśli chcesz działać na tym oknie które aktualnie używasz napisz cos takiego:

 private ImageIcon wybieraczKolorow() {
        ImageIcon ikonka;  //to jest tutaj niepotrzebne z tego co widze - wywalić :P
       
        this.setVisible(true);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setLayout(new GridLayout(2, 4));
        for(int i=0 ;i<ikony.length;i++)
        {
            przyciski[i] = new JButton(ikony[i]);
            this.getContentPane().add(przyciski[i]);
        }
        this.pack();
        return ikony[2];
 
    }

"this" oznacza, że działasz jakby w kontekście "tej" klasy - która defacto jest już JFrame bo po niej dziedziczy (extends JFrame)
Wcześniej w kodzie też widzę, że tworzysz często nowe instancje JFrame. Spróbuj je wyrzucić, a na ich miejsce wstawić słowo "this". Tzn jak masz:

JFrame ramka = new JFrame();
ramka.setVisible(true);
ramka.jakas_metoda();

zrób mniej-wiecej coś takiego:

JFrame ramka = new JFrame(); // TEGO MA NIE BYC, zbędne będzie
this.setVisible(true);
this.jakas_metoda();

Pisze dość na szybko i nie wiem czy dobrze zrozumiałem czy to chcesz zrobić, jesli nie to wybacz:) (Nie wiem czy przypadkiem nie chciałeś tworzyć nowych okienek małych do jakichś tam działań na programie, jeśli tak to zamiast JFrame użyj JDialog)
Poczytaj też na temat słówka "this" bo się przydaje :)
pozdrawiam

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