因此对于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),该按钮不会自动显示。在出现网格按钮之前,应首先调整框架。我该如何使用这个?
答案 0 :(得分: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();
}
});
}
}
以下是此输出:
和
添加了这个新代码来回答新事物:
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();
}
});
}
}
答案 1 :(得分:2)
我不确定我是否理解这个问题或代码,但是我希望在3x3按钮上看到一个ActionListener,它会使用类似这样的方法创建JRadioButton
实例数组:< / p>
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;
}