1

所以对于java中的这个类,这是我的代码

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.*;

public class RadioSelection extends JFrame implements ActionListener
{
private ActionListener action;  
private JButton[][] button;
private JPanel bottomPanel;
private LineBorder lineBorder;

private int randomRowLimit;
private int randomColumnLimit;
private Random random;
private int size;
JLabel label = new JLabel("Select the no. of Grid");
public RadioSelection()
{
    randomRowLimit = 0;
    randomColumnLimit = 0;
    random = new Random();
    size = 0;

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationByPlatform(true);

    lineBorder = new LineBorder(Color.BLUE.darker());


    JPanel topPanel = new JPanel();
    topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

    bottomPanel = new JPanel();

    final JRadioButton threeSquareButton = new JRadioButton("3 X 3", false);
    final JRadioButton fourSquareButton = new JRadioButton("4 X 4", false);
    final JRadioButton fiveSquareButton = new JRadioButton("5 X 5", false);

    threeSquareButton.setBorder(lineBorder);
    fourSquareButton.setBorder(lineBorder);
    fiveSquareButton.setBorder(lineBorder);
    label.setFont(null);
    action = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {

            if (ae.getSource() == threeSquareButton)
            {
                remove(bottomPanel);
                bottomPanel = getCenterPanel(3);
                add(bottomPanel, BorderLayout.CENTER);
            }
            else if (ae.getSource() == fourSquareButton)
            {
                remove(bottomPanel);
                bottomPanel = getCenterPanel(4);
                add(bottomPanel, BorderLayout.CENTER);
            }
            else if (ae.getSource() == fiveSquareButton)
            {

                remove(bottomPanel);
                bottomPanel = getCenterPanel(5);
                add(bottomPanel, BorderLayout.CENTER);
            }


            invalidate(); // If you are using JDK 1.7 +
            //getContentPane().revalidate(); // if you using JDK 1.6 or lower
              repaint();
          }
      };

    threeSquareButton.addActionListener(action);
    fourSquareButton.addActionListener(action);
    fiveSquareButton.addActionListener(action);

    ButtonGroup bg = new ButtonGroup();
    bg.add(threeSquareButton);
    bg.add(fourSquareButton);
    bg.add(fiveSquareButton);


    topPanel.add(label);
    topPanel.add(threeSquareButton);
    topPanel.add(fourSquareButton);
    topPanel.add(fiveSquareButton);

    add(topPanel, BorderLayout.PAGE_START);

    add(bottomPanel, BorderLayout.CENTER);

    setSize(300, 300);
    //pack();
    setVisible(true);
 }
 private JPanel getCenterPanel(int size)
 {
    JPanel bottomPanel = new JPanel(new GridLayout(size, size));
    button = new JButton[size][size];
    this.size = size;

    for (int row = 0; row < size; row++)
    {
        for (int column = 0; column < size; column++)
        {
            button[row][column] = new JButton();
            button[row][column].setBorder(lineBorder);
            button[row][column].setMargin(new Insets(2, 2, 2, 2));
            button[row][column].addActionListener(this);
            bottomPanel.add(button[row][column]);
        }
    }

    randomRowLimit = random.nextInt(size);
    randomColumnLimit = random.nextInt(size);
    button[randomRowLimit][randomColumnLimit].setText("mouse");

    return bottomPanel;
}

 public void actionPerformed(ActionEvent ae)
{
    JButton button = (JButton) ae.getSource();

    if ((button.getText()).equals("mouse"))
    {
        randomRowLimit = random.nextInt(size);
        randomColumnLimit = random.nextInt(size);
        System.out.println("Row : " + randomRowLimit);
        System.out.println("Column : " + randomColumnLimit);
        button.setText("");
        this.button[randomRowLimit][randomColumnLimit].setText("mouse");            
    }
    else
    {
   JOptionPane.showMessageDialog(this, "Catch the mouse!", "Small Game : ",                     JOptionPane.ERROR_MESSAGE);
    }
}

public static void main(String... args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {

            new RadioSelection();
        }
    });
}

}

这个正在工作,但如果你注意到,我invalidate();在这里使用过。如果是revalidate();,它将不会运行。但是,我担心的是,当单击单选按钮(例如 3x3)时,该按钮不会自动显示。在出现网格按钮之前,应首先调整框架。我该如何使用这个?

4

2 回答 2

3

试试这个代码:

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

public class RadioSelection extends JFrame
{
    private ActionListener action;  
    private JPanel bottomPanel;
    private LineBorder lineBorder ;

    public RadioSelection()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        lineBorder = new LineBorder(Color.BLUE.darker());

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        bottomPanel = new JPanel();

        final JRadioButton threeSquareButton = new JRadioButton("3 X 3", false);
        final JRadioButton fourSquareButton = new JRadioButton("4 X 4", false);

        threeSquareButton.setBorder(lineBorder);
        fourSquareButton.setBorder(lineBorder);

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (ae.getSource() == threeSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(3);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                else if (ae.getSource() == fourSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(4);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                revalidate(); // If you are using JDK 1.7 +
                // getContentPane().revalidate(); // if you using JDK 1.6 or lower
                repaint();
            }
        };

        threeSquareButton.addActionListener(action);
        fourSquareButton.addActionListener(action);

        ButtonGroup bg = new ButtonGroup();
        bg.add(threeSquareButton);
        bg.add(fourSquareButton);

        topPanel.add(threeSquareButton);
        topPanel.add(fourSquareButton);

        add(topPanel, BorderLayout.PAGE_START);
        add(bottomPanel, BorderLayout.CENTER);

        setSize(300, 300);
        setVisible(true);
    }

    private JPanel getCenterPanel(int size)
    {
        JPanel bottomPanel = new JPanel(new GridLayout(size, size));

        for (int row = 0; row < size; row++)
        {
            for (int column = 0; column < size; column++)
            {
                JButton button = new JButton("Button " + row + " " + column);
                button.setBorder(lineBorder);
                button.setMargin(new Insets(2, 2, 2, 2));
                bottomPanel.add(button);
            }
        }

        return bottomPanel;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new RadioSelection();
            }
        });
    }
}

这是这个的输出:

3 X 3 矩阵4 X 4 矩阵

添加了这个新代码来回答一个新问题:

import java.awt.*;
import java.awt.event.*;

import java.util.Random;

import javax.swing.*;
import javax.swing.border.*;

public class RadioSelection extends JFrame implements ActionListener
{
    private ActionListener action;  
    private JButton[][] button;

    private JPanel bottomPanel;
    private LineBorder lineBorder;

    private int randomRowLimit;
    private int randomColumnLimit;
    private Random random;
    private int size;

    public RadioSelection()
    {
        randomRowLimit = 0;
        randomColumnLimit = 0;
        random = new Random();
        size = 0;

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        lineBorder = new LineBorder(Color.BLUE.darker());

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        bottomPanel = new JPanel();

        final JRadioButton threeSquareButton = new JRadioButton("3 X 3", false);
        final JRadioButton fourSquareButton = new JRadioButton("4 X 4", false);
        final JRadioButton fiveSquareButton = new JRadioButton("5 X 5", false);

        threeSquareButton.setBorder(lineBorder);
        fourSquareButton.setBorder(lineBorder);
        fiveSquareButton.setBorder(lineBorder);

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (ae.getSource() == threeSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(3);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                else if (ae.getSource() == fourSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(4);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                else if (ae.getSource() == fiveSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(5);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                revalidate(); // If you are using JDK 1.7 +
                // getContentPane().revalidate(); // if you using JDK 1.6 or lower
                repaint();
            }
        };

        threeSquareButton.addActionListener(action);
        fourSquareButton.addActionListener(action);
        fiveSquareButton.addActionListener(action);

        ButtonGroup bg = new ButtonGroup();
        bg.add(threeSquareButton);
        bg.add(fourSquareButton);
        bg.add(fiveSquareButton);

        topPanel.add(threeSquareButton);
        topPanel.add(fourSquareButton);
        topPanel.add(fiveSquareButton);

        add(topPanel, BorderLayout.PAGE_START);
        add(bottomPanel, BorderLayout.CENTER);

        setSize(300, 300);
        //pack();
        setVisible(true);
    }

    private JPanel getCenterPanel(int size)
    {
        JPanel bottomPanel = new JPanel(new GridLayout(size, size));
        button = new JButton[size][size];
        this.size = size;

        for (int row = 0; row < size; row++)
        {
            for (int column = 0; column < size; column++)
            {
                button[row][column] = new JButton();
                button[row][column].setBorder(lineBorder);
                button[row][column].setMargin(new Insets(2, 2, 2, 2));
                button[row][column].addActionListener(this);
                bottomPanel.add(button[row][column]);
            }
        }

        randomRowLimit = random.nextInt(size);
        randomColumnLimit = random.nextInt(size);
        button[randomRowLimit][randomColumnLimit].setText("X");

        return bottomPanel;
    }

    public void actionPerformed(ActionEvent ae)
    {
        JButton button = (JButton) ae.getSource();

        if ((button.getText()).equals("X"))
        {
            randomRowLimit = random.nextInt(size);
            randomColumnLimit = random.nextInt(size);
            System.out.println("Row : " + randomRowLimit);
            System.out.println("Column : " + randomColumnLimit);
            button.setText("");
            this.button[randomRowLimit][randomColumnLimit].setText("X");            
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Please Click on X Mark to follow it.", "Small Game : ", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new RadioSelection();
            }
        });
    }
}
于 2012-03-07T11:46:47.867 回答
2

我不确定我是否理解问题或代码,但我希望在 3x3 按钮上看到一个 ActionListener,它将JRadioButton使用如下方法创建一个实例数组:

private JRadioButton [][] createRadioButtonArray(int squareSize) {
    JRadioButton [][] arrayOfButtons = new JRadioButton[squareSize][squareSize];
    for (int i = 0; i < squareSize; ++i) {
        for (int j = 0; j < squareSize; ++j) {
            arrayOfButtons[i][j] = new JRadioButton("button" + i + "," + j,false);
        }
    }
    return arrayOfButtons;
}
于 2012-03-07T10:31:28.150 回答