У меня есть 2 несколько простых проблемы:
1) Я пытаюсь установить btns[0][0]
(строка 0, столбец 0) единственной видимой кнопкой в моей сетке кнопок 00-99, и после того, как я нажимаю кнопку, те, которые касаются ее, становятся видимыми, и процесс продолжается до тех пор, пока все кнопки не будут видимый.
2) После нажатия каждой кнопки кнопка (именно нажатая, а не каждая кнопка) меняет свою цифровую метку на смайлик, setLabel(":)")
.
public class Project5 extends JApplet implements ActionListener {
JButton button;
Container contentPane;
JButton[][] btns = new JButton[10][10];
int clicks = 0;
public void init() {
setSize(600, 600);
contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
GridLayout grid = new GridLayout(10,10,0,4);
contentPane.setLayout(grid);
clicks = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
btns[i][j] = new JButton();
button = new JButton(""+i+j);
button.addActionListener(this);
contentPane.add(button);
//button.setVisible(false);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
Container contentPane = getContentPane();
btns[0][0].setVisible(true);
String button_label = e.getActionCommand();
int row = Character.getNumericValue(button_label.charAt(0));
int column = Character.getNumericValue(button_label.charAt(1));
clicks++;
if (clicks % 5 == 0) {
contentPane.setBackground(Color.PINK);
} else if (clicks % 5 == 1) {
contentPane.setBackground(Color.GREEN);
} else if (clicks % 5 == 2) {
contentPane.setBackground(Color.BLUE);
} else if (clicks % 5 == 3) {
contentPane.setBackground(Color.YELLOW);
} else if (clicks % 5 == 4) {
contentPane.setBackground(Color.RED);
}
}
public void checkDone() {
//if all buttons visible, change contentPane color to black.
}
}