gpt4 book ai didi

java - 扩展 JButton 在鼠标悬停之前无法显示

转载 作者:行者123 更新时间:2023-12-01 17:56:16 27 4
gpt4 key购买 nike

所以情况是,我正在制作一个充满 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;
}
}

它确实解决了任务,但是这些矩阵按钮在我将鼠标悬停在它们上方之前不想出现。可能是什么问题?

这是包含这些按钮并处理它们的操作的面板的代码:

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 个变量。

最佳答案

这是危险的代码:

public int getX() {
return x;
}
public int getY() {
return y;
}

您确实意识到这会覆盖 JButton 的两个关键方法(实际上,这些方法来自 JButton 的父级 JComponent),这些方法有助于设置按钮在 GUI 上的位置(在方法看看是不是这样)。我强烈建议您更改这些方法的签名,也许是 getGridX()getGridY(),或者更好的是,使用组合而不是继承,因为您会遇到当您扩展复杂的类(例如 Swing 组件类)时,存在此类问题的风险——无意中覆盖关键方法。因此,除非绝对必要,否则我会尽量避免扩展这些类。

例如:

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());
}
}

class MatrixButton {
private int column;
private int row;
private JButton button = new JButton();
private int value;

public MatrixButton(int column, int row, int value) {
this.column = column;
this.row = row;
setValue(value);
}

public void addActionListener(ActionListener listener) {
button.addActionListener(listener);
}

public int getColumn() {
return column;
}

public int getRow() {
return row;
}

public JButton getButton() {
return button;
}

public int getValue() {
return value;
}

public final void setValue(int value) {
this.value = value;
button.setText("" + value);
}
}

最好只使用整数而不是字符串

关于java - 扩展 JButton 在鼠标悬停之前无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44531574/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com