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

时间:2018-04-21 12:29:43

标签: java swing jpanel grid-layout

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

echo mb_strimwidth($row['name'], 0, 20, "...");

1 个答案:

答案 0 :(得分:1)

考虑:

  1. 为按钮提供某种代表其真实顺序的值。有几种方法可以做到这一点,包括将它们放在指定顺序的数组中,或者扩展JButton并给你的类赋一个int值字段,或者使用HashMap
  2. 将这些按钮放入ArrayList<JButton>
  3. 通过Collections.shuffle(...)
  4. 改组ArrayList
  5. 然后将按钮添加到GUI
  6. 或者,您可以使用非混乱的JButton,而是随后将AbstractActions随机设置到按钮中。
  7. 任何解决方案的细节都取决于您当前计划的详细信息,这些内容我们还不太了解。如果您需要更详细的帮助,请考虑在问题中创建并发布有效的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());
        }
    }