所以对于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)时,该按钮不会自动显示。在出现网格按钮之前,应首先调整框架。我该如何使用这个?