我正在寫一個程序在java swt中播放tic tac toe,並且我找不到使用button.setImage(image)方法在按鈕上顯示帶有x或o的圖像的方法。當我這樣做時,圖像變成灰色,我不想那樣。有沒有什麼辦法可以讓我點擊按鈕,按鈕變成禁用狀態,圖像顯示在屏幕上,或者我至少可以讓按鈕在禁用狀態時不會灰顯?在禁用按鈕上顯示圖像,而不會顯示灰色圖像?
另外它應該指出我使用SWT爲我的GUI。
下面是我在使用,如果它是任何幫助,麻煩的部分代碼:
public static void drawX(Button b, Shell s, Image x){ //draws an X image
int topLeft_X=b.getLocation().x;
int topLeft_Y=b.getLocation().y;
GC gc = new GC(b);
gc.drawImage(x, topLeft_X, topLeft_Y);
}
public static void drawO(Button b, Shell s, Image o){ //draws an O image
int topLeft_X=b.getLocation().x;
int topLeft_Y=b.getLocation().y;
GC gc = new GC(b);
gc.drawImage(o, topLeft_X, topLeft_Y);
}
static double turnCount = 1;
public static void button(final Button b, final Shell s, final int i, final int j, final Image g, final Image h){ //the method that would make the image appear
b.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
b.setEnabled(false);
turnCount++;
if(p1()){
a[i][j]++;
drawX(b, s, g);
b.setVisible(false);
}
else{
a[i][j]--;
drawX(b, s, h);
b.dispose();
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
可能有辦法做到這一點,我不知道,但另一種方法是刪除/隱藏按鈕,並放置一個'JLabel'的地方。 – twain249