1

我的问题只是TestMethods#newGame(), or button[2][2].setBackground(Color.red);, 中的注释行不起作用,说它无法解析为变量。我将在哪里以及如何在顶部编写JButton,JPanel等声明行以使注释行起作用?我知道这可能与权限有关,但我让它工作得有多冷?顺便说一句,我知道代码效率极低。感谢您的所有回复,这是我的完整代码:

import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.io.*;

public class Test implements ActionListener
{
    public JButton[][] button = new JButton[8][8];
    public JFrame frame = new JFrame();
    public JPanel[][] square = new JPanel[8][8];
    Test() throws IOException
    {
        frame.setLayout(new GridLayout(9,8));

        for(int x=0; x<8; x++)
        {
            for(int y=0; y<8; y++)
            {
                square[x][y] = new JPanel();
                frame.add(square[x][y]);
                square[x][y].setSize(100,100);
                square[x][y].setLayout(new GridLayout(1,1));
                if(y%2==0)
                    if(x%2==0)
                        square[x][y].setBackground(Color.cyan);
                    else
                        square[x][y].setBackground(Color.blue);
                if(y%2!=0)
                    if(x%2==0)
                        square[x][y].setBackground(Color.blue);
                    else
                        square[x][y].setBackground(Color.cyan);
                button[x][y] = new JButton();
                button[x][y] = new TestMethods(x,y);
                square[x][y].add(button[x][y]);
                button[x][y].setOpaque(false);
                button[x][y].setContentAreaFilled(false);
                button[x][y].setBorderPainted(false);
                button[x][y].addActionListener(this);
            }
        }

        frame.setSize(800,900);
        frame.setVisible(true);
    }

    public static void main(String[] args) throws IOException
    {
        new Test();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object obj = e.getSource();
        TestMethods clicked = (TestMethods)obj;
        int x = clicked.getGridX();
        int y = clicked.getGridY();
        System.out.println(x + " : " + y);
    }
}

class TestMethods extends JButton
{
    private int gridX, gridY;
    public TestMethods(int x, int y)
    {
        this.gridX = x;
        this.gridY = y;
    }

    public int getGridX() {return gridX ;}
    public int getGridY() {return gridY ;}

    public void newGame()
    {
        //button[2][2].setBackground(Color.red);
    }
}
4

1 回答 1

1

我更改了您的代码以显示几种不同的方式来访问网格中面板中的按钮。单击New按钮或任何网格按钮以查看。

  • TestPanel覆盖,getPreferredSize()那么你可以pack().frame

  • 很多东西都被移入了TestButton,包括对它的parent.

  • 每个private static class都可以变成一个 package-private class

图片

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

public class Test {

    private static final int N = 8;
    private JFrame frame = new JFrame();
    private TestPanel[][] square = new TestPanel[N][N];
    private TestButton[][] button = new TestButton[N][N];

    public Test() {
        JPanel gridPanel = new JPanel();
        gridPanel.setLayout(new GridLayout(N, N));
        for (int x = 0; x < N; x++) {
            for (int y = 0; y < N; y++) {
                TestPanel tp = new TestPanel(x, y);
                square[x][y] = tp;
                button[x][y] = tp.getButton();
                gridPanel.add(tp);
            }
        }
        frame.add(gridPanel);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton(new AbstractAction("New") {
            @Override
            public void actionPerformed(ActionEvent e) {
                button[2][2].newGame();
                square[3][3].setBackground(Color.magenta);
                button[4][4].parent.setBackground(Color.yellow);
            }
        }));
        frame.add(buttonPanel, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private static class TestPanel extends JPanel {

        private static final int SIZE = 48;
        private TestButton button;

        public TestButton getButton() {
            return button;
        }

        public TestPanel(int x, int y) {
            super(new GridLayout(1, 1));
            this.button = new TestButton(this, x, y);
            this.add(button);
            if ((x + y) % 2 == 0) {
                this.setBackground(Color.cyan);
            } else {
                this.setBackground(Color.blue);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(SIZE, SIZE);
        }
    }

    private static class TestButton extends JButton {

        private TestPanel parent;
        private int gridX, gridY;

        public TestButton(final TestPanel parent, int x, int y) {
            this.parent = parent;
            this.gridX = x;
            this.gridY = y;
            this.setOpaque(false);
            this.setContentAreaFilled(false);
            this.setBorderPainted(false);
            this.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    TestButton clicked = (TestButton) e.getSource();
                    parent.setBackground(Color.green);
                    int x = clicked.getGridX();
                    int y = clicked.getGridY();
                    System.out.println(x + " : " + y);
                }
            });
        }

        public int getGridX() {
            return gridX;
        }

        public int getGridY() {
            return gridY;
        }

        public void newGame() {
            parent.setBackground(Color.red);
        }
    }

    public static void main(String[] args) {
        new Test();
    }
}
于 2013-04-29T13:44:05.360 回答