首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在一个小女孩的java中随机放置64个按钮

在一个小女孩的java中随机放置64个按钮
EN

Stack Overflow用户
提问于 2018-04-21 12:29:42
回答 1查看 152关注 0票数 0

其目标是创建一个类似于rubrics立方体的游戏,用户必须根据匹配的颜色重新排列按钮。这就是我随机放置按钮时所做的,但是当按钮出现时,它不起作用。如果这是有意义的话,随机顺序就被看作是有序的顺序。有什么办法解决这个问题吗?

代码语言:javascript
复制
  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());

  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-21 13:01:51

考虑:

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

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

例如,编译并运行它,然后阅读注释:

代码语言:javascript
复制
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());
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49955778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档