我使用此代码创建了一个带有3x3网格按钮的框架......
JFrame frame = new JFrame("my 3x3");
JPanel panel = new JPanel();
Container pane = frame.getContentPane();
panel.setLayout(new GridLayout(3,3));
panel.add(upperLeft);
panel.add(upperCenter);
panel.add(upperRight);
panel.add(midLeft);
panel.add(midCenter);
panel.add(midRight);
panel.add(bottomLeft);
panel.add(bottomCenter);
panel.add(bottomRight);
pane.add(panel);
...每个上下左右元素都是JButton对象。
在执行的后期,我需要一个这些按钮的列表来迭代重置它们,但我在那一点上只有框架。我知道埋在框架对象的某个地方是一个组件列表,也许层深,但在哪里?是否有直接的方法来检索框架的按钮?
答案 0 :(得分:3)
每次挥杆Container
都有getComponents()
。因此,从JFrame(或其getContentPane()
)开始,您可以递归下去并获取所有组件。
但是,当您将按钮添加到面板时,您也可以按住List<JButton>
并填写它。
答案 1 :(得分:2)
由于您的按钮网格是3x3,并且因为您事先知道(在程序运行之前),为什么不创建一个2维3x3按钮数组。然后按钮索引将自然地反映他们的位置。
编辑:例如
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class ButtonGridGuiPanel extends JPanel {
// The number of rows and columns
private static final int MAX_ROWS = 3;
private static final int MAX_COLS = MAX_ROWS;
// the size of the button text in "points"
private static final float BTN_PTS = 64;
// starting text for button. Has spaces so button isn't too narrow
private static final String INITIAL_TEXT = " ";
// The 2-D JButton array that holds our buttons
private JButton[][] buttonGrid = new JButton[MAX_ROWS][MAX_COLS];
private int buttonPressCount = 0;
public ButtonGridGuiPanel() {
JPanel buttonGridPanel = new JPanel(); // jpanel to hold the buttons
buttonGridPanel.setLayout(new GridLayout(MAX_ROWS, MAX_COLS));
// create an action listener to add to all buttons
ButtonListener btnListener = new ButtonListener();
// iterate through the 2-d button array
for (int row = 0; row < buttonGrid.length; row++) {
for (int col = 0; col < buttonGrid[row].length; col++) {
// create a new button with blank text
JButton btn = new JButton(INITIAL_TEXT);
// set its font
btn.setFont(btn.getFont().deriveFont(Font.BOLD, BTN_PTS));
// add the action listener
btn.addActionListener(btnListener);
buttonGridPanel.add(btn); // add button to the JPanel
buttonGrid[row][col] = btn; // and add it to the 2-d array
}
}
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
reset(); // call the reset method, that's it!
}
});
JPanel resetBtnPanel = new JPanel(); // to hold reset button
resetBtnPanel.add(resetButton);
setLayout(new BorderLayout()); // main JPanel uses border layout
add(buttonGridPanel, BorderLayout.CENTER); // add button grid panel to main
add(resetBtnPanel, BorderLayout.PAGE_END); // add reset btn panel to main
}
public void reset() {
buttonPressCount = 0; // in order that first press gives "X"
// then iterate through the button grid's rows and columns
// setting button text back to its initial state
for (int row = 0; row < buttonGrid.length; row++) {
for (int col = 0; col < buttonGrid[row].length; col++) {
buttonGrid[row][col].setText(INITIAL_TEXT);
}
}
}
// a simple action listener that finds out which button has been pressed
// and sets the button's text alternatingly to "X" or "O"
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource(); // get pressed button
String text = btn.getText().trim(); // get its text
if (text.isEmpty()) { // if text is blank
// iterate through the 2d button array to find out the
// row and col position of the pressed button
int rowPressed = -1;
int colPressed = -1;
for (int row = 0; row < buttonGrid.length; row++) {
for (int col = 0; col < buttonGrid[row].length; col++) {
if (btn == buttonGrid[row][col]) {
// button has been found!
rowPressed = row;
colPressed = col;
}
}
}
// make the button text alternate between X and O
text = (buttonPressCount % 2 == 0) ? "X" : "O";
btn.setText(text); // set the button's text
buttonPressCount++;
// TODO: perform whatever logic is necessary based on the row
// and column position of the button
System.out.printf("[row, col]: [%d, %d]%n", rowPressed, colPressed);
}
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Button Grid");
frame.getContentPane().add(new ButtonGridGuiPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
// create the GUI in a thread-safe manner
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}