在 girdlaout 中随机放置 64 个按钮 java

placing 64 buttons randomly in a girdlaout java

目标是创建一个类似规则立方体的游戏,用户必须根据匹配的颜色重新排列按钮。这就是我随机放置按钮所做的,但是当按钮出现时它不起作用。如果有意义,随机顺序将被视为有序顺序。有想法该怎么解决这个吗?

  while(list1.size()!=501){

     int x=rand.nextInt(8);
     list1.add(x);
  }
  while(list2.size()!=501){

     int x=rand.nextInt(8);
     list2.add(x);
  }
  for(int b=0;b<500;b++){
     int l= list1.get(b);
     //System.out.println(l);
     int j= list2.get(b);
     panel.add(buttons[l][j]);
     //System.out.println(buttons[l][j].getBackground());

  }

考虑:

  1. 为按钮赋予某种代表其真实顺序的值。有几种方法可以做到这一点,包括将它们放入指定顺序的数组中,或者扩展 JButton 并为您的 class 提供一个 int 值字段,或者使用 HashMap
  2. 将这些按钮放入 ArrayList<JButton>
  3. 通过 Collections.shuffle(...)
  4. 打乱 ArrayList
  5. 然后将按钮添加到您的 GUI
  6. 或者,您可以使用非混排的 JButtons 而不是混排的 AbstractActions,然后将其设置到按钮中。

任何解决方案的细节都将取决于您当前程序的细节,这是我们还不够了解的。如果您需要更详细的帮助,请考虑在您的问题中创建并发布有效的 MCVE

比如编译和运行这个,然后看注释:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;

public class RandomButtons extends JPanel {
    private static final long serialVersionUID = 1L;
    private static final int ROWS = 8;
    private JButton[][] buttons = new JButton[ROWS][ROWS];
    private List<JButton> buttonList = new ArrayList<>();
    private JPanel buttonsPanel = new JPanel(new GridLayout(ROWS, ROWS));


    public RandomButtons() {
        for (int i = 0; i < buttons.length; i++) {
            for (int j = 0; j < buttons[i].length; j++) {
                // create new JBUtton
                final JButton button = new JButton("Button");
                // put into array
                buttons[i][j] = button;
                // put into ArrayList
                buttonList.add(button);

                // unique value 0 to 63 for each button
                // order it has been created
                final int value = i * ROWS + j;

                // create one of 64 different color hues using value above
                float hue = ((float) value) / (ROWS * ROWS);
                float sat = 0.7f; // reduce sat so we can see text
                float brightness = 1.0f;
                Color color = Color.getHSBColor(hue, sat, brightness);
                button.setBackground(color); // set button's color
                button.addActionListener(e -> {
                    // display the button's order
                    System.out.println("Value: " + value);
                });

            }
        }
        randomizeButtons();

        JButton randomizeButton = new JButton("Randomize");
        randomizeButton.addActionListener(e -> { randomizeButtons(); });
        JButton orderButton = new JButton("Put in Order");
        orderButton.addActionListener(e -> { orderButtons(); });

        JPanel bottomPanel = new JPanel(new GridLayout(1, 2));
        bottomPanel.add(randomizeButton);
        bottomPanel.add(orderButton);

        setLayout(new BorderLayout());
        add(buttonsPanel, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);
    }

    public void randomizeButtons() {
        buttonsPanel.removeAll(); // remove all buttons
        Collections.shuffle(buttonList); // shuffle the ArrayList

        // re-add the buttons **using the ArrayList**
        for (JButton jButton : buttonList) {
            buttonsPanel.add(jButton);
        }

        // tell JPanel to layout its newly added components
        buttonsPanel.revalidate();
        // and then paint them
        buttonsPanel.repaint();
    }

    public void orderButtons() {
        buttonsPanel.removeAll();  // remove all buttons

        // re-add the buttons **using the 2D array**
        for (JButton[] buttonRow : buttons) {
            for (JButton jButton : buttonRow) {
                buttonsPanel.add(jButton);
            }
        }
        buttonsPanel.revalidate();
        buttonsPanel.repaint();
    }

    private static void createAndShowGui() {
        RandomButtons mainPanel = new RandomButtons();

        JFrame frame = new JFrame("RandomButtons");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}