我需要将Java摆动窗口划分为多个字段,类似于桌子或棋盘。每个单元格的颜色应取决于该单元格所代表的对象(每个对象的坐标在游戏中都会发生变化,因此每个单元格的颜色不是恒定的)。

此外,如果用户单击空白字段(白色),则会创建一个新的随机对象,并将该对象分配给这些字段(并且字段颜色正在更改)。

哪个Java摆动控件最适合这些功能?

最佳答案

如果我是我,我将创建2个panel类(白色和黑色),白色的类带有MouseAdapter,这样,当单击面板之一时,您可以从数组中提取随机的JLabel。这是一个可能有帮助的示例(大约120行):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;


public class ScratchPaper extends JFrame {
    private static final long serialVersionUID = 1L;
    private static final int GRIDSIZE = 8;
    private JPanel[][] whitePanel = new WhitePanel[GRIDSIZE][GRIDSIZE];
    private JPanel[][] blackPanel = new BlackPanel[GRIDSIZE][GRIDSIZE];
    private Random rand = new Random();
    JButton b1 = new JButton("Btn1");
    JButton b2 = new JButton("Btn2");
    JButton b3 = new JButton("Btn3");
    JLabel l1 = new JLabel("Lbl1");
    JLabel l2 = new JLabel("Lbl2");
    JLabel l3 = new JLabel("Lbl3");
    JPanel panel = new JPanel();

    private JComponent[][] randObjects = {{b1, b2, b3}, {l1, l2, l3}, {panel, panel, panel}};
    private Color[] randColors = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA};


    public ScratchPaper() {
        initGUI();
        setTitle("EXAMPLE");
        pack();
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void initGUI() {
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(GRIDSIZE, GRIDSIZE)); // makes 8*8 grid
        add(centerPanel, BorderLayout.CENTER);

        MouseAdapter ma = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Component clickedComp = findComponentAt(e.getPoint());
                JPanel target = (JPanel) clickedComp;
                panel.setBackground(randColors[rand.nextInt(randColors.length)]);

                if (target instanceof WhitePanel){
                    target.add(randObjects[rand.nextInt(randObjects.length)][rand.nextInt(randObjects[0].length)]);
                    target.updateUI();
                }
            }
        };
        addMouseListener(ma);

         for (int row=0; row<GRIDSIZE; row++) {
            for (int col=0; col<GRIDSIZE; col++) {
                whitePanel[row][col] = new WhitePanel(row, col);
                blackPanel[row][col] = new BlackPanel(row, col);

                if ((row%2 == 0 && col%2 == 0) || ((row+1)%2 == 0 && (col+1)%2 == 0)) {
                    centerPanel.add(whitePanel[row][col]);
                }
                else {
                    centerPanel.add(blackPanel[row][col]);
                }
            }
         }
    }


    public static void main(String args[]) {
        try {
            String className = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel(className);
        } catch (Exception ex) {
            System.out.println(ex);
        }

        EventQueue.invokeLater(new Runnable(){
            @Override
            public void run(){
                new ScratchPaper();
            }
        });
    }

    class WhitePanel extends JPanel {
        private static final int SIZE = 50;

        public WhitePanel(int row, int col) {
            Dimension size = new Dimension(SIZE, SIZE);
            setPreferredSize(size);
            setBackground(Color.WHITE);
        }
    }

    class BlackPanel extends JPanel {
        private static final int SIZE = 50;

        public BlackPanel(int row, int col) {
            Dimension size = new Dimension(SIZE, SIZE);
            setPreferredSize(size);
            setBackground(Color.BLACK);
        }
    }
}


尝试运行它!这实际上很有趣!

09-10 08:08
查看更多