简体   繁体   中英

Change the color of a certain pane in gridlayout

So, what I want to happen is for every box to be white but when the user clicks on one of the boxes in the grid, it will change to a randomly generated color. I know how to write the code for the randomly generated color, but I'm not sure how to make it so that the program chooses the correct panel to change its color. Here is what i have so far:

Client

import javax.swing.*;    
import java.awt.*;
import java.util.Random;       

public class Prog3
{
   public static void main(String[] args)
   {
       int rows=8;
       int cols=8;
       JFrame theGUI = new JFrame();
       theGUI.setTitle("GUI Example");
       theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Container pane = theGUI.getContentPane();
       pane.setLayout(new GridLayout(rows, cols,5,5));

       for (int i = 1; i < rows * cols; i++)
       {
           Color backColor = Color.white;
           Prog3_Server panel = new Prog3_Server(backColor,50,50);
           pane.add(panel);
       }
       theGUI.pack();
       theGUI.setVisible(true);
    }
}

Server

import javax.swing.*;
import java.awt.*;

public class Prog3_Server extends JPanel
{
    public static void main(String[] args)
    {
        JFrame theGUI = new JFrame();
        theGUI.setTitle("GUI Example");
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Prog3_Server panel = new Prog3_Server(Color.white, 200, 200);
        Container pane = theGUI.getContentPane();
        pane.add(panel);
        theGUI.pack();
        theGUI.setVisible(true);
    }

   // Client provides color and preferred width and height
   public Prog3_Server(Color backColor, int width, int height){
      setBackground(backColor);
      setPreferredSize(new Dimension(width, height));
   }

   // Client provides color 
   // Preferred width and height are 0, 0 by default   
   public Prog3_Server(Color backColor){
      setBackground(backColor);
   }    
}

Any ideas on how i would use mouse events to make sure the right panel is changed from white to a random color?

You need a MouseListener/MouseAdapter

MouseListener detectClick = new MouseAdapter() {
  public void mouseClicked(MouseEvent me) {
    me.getSource().setBackground(createRandomBackgroundColor());
  }
}

If you add this MouseListener to all of your sub-panels, you should get the result you want

You should add the MouseListener where you're creating and adding the subpanels to the parent panel, here:

   //create MouseListener here
   for (int i = 1; i < rows * cols; i++)
   {
       Color backColor = Color.white;
       Prog3_Server panel = new Prog3_Server(backColor,50,50);
       //add mouse listener to the panel you've created here
       pane.add(panel);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM