我有一个我制作的程序选择工具。它会打开一个带有 17 个按钮的 JFrame,其中 15 个是可自定义的,它们从位于 C: 驱动器中的 .txt 文档中获取文本。当我单击分配按钮时,它会打开一个 JFileChooser 以在单击按钮时选择要打开的文件。然后选择要更改的按钮,然后键入要通过该按钮显示的文本。之后,程序会重写 .txt 文件并更新按钮。这是更新的代码:
public static void restart() {
start.assignButtonActions();
start.assignButtonText();
start.paint(graphics);
}
public void assignButtonActions() {
/**
* assign button actions
*/
for (int i = 0; i < buttonAction.length; i++) {
buttonAction[i] = io.readSpecificFromHD("C:\\ButtonActions.txt", i
+ 1 + actionButton.length);
}
}
public void assignButtonText() {
for (int i = 0; i < actionButton.length; i++) {
/**
* set button text
*/
actionButton[i].setText(io.readSpecificFromHD(
"C:\\ButtonActions.txt", i + 1));
}
}
public void paint(Graphics g) {
g.drawImage(getImage("files/background.png"), 0, 0, FRAMEWIDTH,
FRAMEHEIGHT, null);
refresh();
}
public void refresh() {
graphics.drawImage(getImage("files/background.png"), 0, 0, FRAMEWIDTH,
FRAMEHEIGHT, null);
for (int i = 0; i < actionButton.length; i++) {
actionButton[i].repaint();
}
assignButton.repaint();
helpButton.repaint();
}
这就是我相信这个问题所需的所有代码。问题是,restart()
调用该方法后,背景就在那里,按钮周围有一个白色方块,方块内部是白色的。不是一个真正的大问题,但真的非常烦人而且非常不专业。起初我以为是在绘制背景后按钮正在调整大小,所以我做了它,以便每次调用时刷新运行两次。一点帮助都没有。
编辑:我解决了这个问题。我接受了气垫船的回答并修改了我学到的一点。我所要做的就是将restart()
方法修改为:
public static void restart() {
start.assignButtonActions();
start.assignButtonText();
start.repaint();
}
因为repaint();
重绘了气垫船所说的整个组件。非常感谢大家!希望这有助于未来的问题。