所以情况是我正在制作一个装有8 * 8按钮的面板,就像一个矩阵,我将不得不根据点击按钮的位置更改特定的按钮文本。制作我自己的JButton似乎是一个好主意,我可以将x和y索引分配给按钮,这样我就可以轻松地检查按钮是否被索引点击了。 这是代码:
import javax.swing.JButton;
public class MatrixButton extends JButton {
private int x;
private int y;
public MatrixButton(int x, int y, String text){
super(text);
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
它确实解决了这个任务,但是这些Matrix按钮不希望出现,直到我将鼠标悬停在它们上面。可能是什么问题?
这里是面板的代码,它包含这些按钮并处理它们的动作:
public class PlayField extends JPanel implements ActionListener {
private MatrixButton[][] button = new MatrixButton[8][8];
public PlayField(){
Random rand = new Random();
setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; ++i){
for (int j = 0; j < 8; ++j){
button[i][j] = new MatrixButton(j, i, "" + rand.nextInt(80));
button[i][j].addActionListener(this);
add(button[i][j]);
}
}
}
private String incrementText(MatrixButton mb){
return "" + (Integer.parseInt(mb.getText()) + 1);
}
@Override
public void actionPerformed(ActionEvent e){
MatrixButton mb = (MatrixButton)e.getSource();
for (int i = mb.getY(); i >= 0; --i){
button[i][mb.getX()].setText(incrementText(button[i][mb.getX()]));
}
for (int i = 0; i < 8; ++i){
if (i != mb.getX())
button[mb.getY()][i].setText(incrementText(button[mb.getY()][i]));
}
}
}
PS:如果我填写普通的JButton,它们就会出现。这就是为什么我很困惑,因为我在JButton扩展上没有太大改变,只是增加了2个变量。
答案 0 :(得分:4)
这是危险的代码:
public int getX() {
return x;
}
public int getY() {
return y;
}
你确实意识到这会覆盖两个JButton的关键方法(实际上,这些方法来自JButton的父级,JComponent),它们有助于在GUI上设置按钮的位置(添加{{1在方法之上看到这是如此)。我强烈建议您更改这些方法的签名,可能@Override
和getGridX()
,甚至更好,使用组合而不是继承,因为您遇到这些类型问题的风险 - 无意中覆盖一种关键方法 - 当你扩展复杂的类时,例如Swing组件类。出于这个原因,我尽量避免扩展这些类,除非绝对必要。
例如:
getGridY()
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PlayField {
private static final int ROWS = 8;
private static final int COLS = ROWS;
private static final int MAX_RAND = 80;
private JPanel mainPanel = new JPanel();
private MatrixButton[][] button = new MatrixButton[ROWS][COLS];
public PlayField(){
Random rand = new Random();
mainPanel.setLayout(new GridLayout(ROWS, COLS));
for (int i = 0; i < ROWS; ++i){
for (int j = 0; j < COLS; ++j){
button[i][j] = new MatrixButton(j, i, rand.nextInt(MAX_RAND));
button[i][j].addActionListener(new MyMatrixListener(i, j));
mainPanel.add(button[i][j].getButton());
}
}
}
private class MyMatrixListener implements ActionListener {
private int i;
private int j;
public MyMatrixListener(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i2 = 0; i2 < button.length; i2++) {
if (i2 != i) {
int value = button[i2][j].getValue();
value++;
button[i2][j].setValue(value);
}
}
for (int j2 = 0; j2 < button[i].length; j2++) {
if (j2 != j) {
int value = button[i][j2].getValue();
value++;
button[i][j2].setValue(value);
}
}
}
}
public JPanel getMainPanel() {
return mainPanel;
}
private static void createAndShowGui() {
JFrame
frame = new JFrame("PlayField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new PlayField().getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
最好只使用整数而不是字符串