![](/img/trans.png)
[英]How to set an ImageIcon to a JButton and resize the picture according to the button's size?
[英]ImageIcon cannot be set to JButton?
我在程序中使用了此過程來制作JButton
的數組。 JButtons
確實顯示在框架中,但是ImageIcon
沒有放入按鈕中。 圖片與程序位於同一目錄。 問題是什么? (空間是我的框架)
static void BasicSetup() {
int count1 = 0;
int count2 = 0;
ImageIcon cell = new ImageIcon("cell.png");
for (int y = 0; y < 16; y++) {
count1 = count1 + 1;
count2 = 0;
for (int x = 0; x < 16; x++) {
count2 = count2 + 1;
field[y][x] = new JButton();
field[y][x].setIcon(cell);
constraints.gridx = count1;
constraints.gridy = count2;
constraints.weightx = 1;
jpanel.add(field[y][x], constraints);
}
}
space.add(jpanel);
}
據我所知,您的錯誤是由於您讀取圖像的方式引起的。
在這種情況下,我使用了這個問題的圖像。
當您將應用打包為JAR文件時,無論如何都需要將圖像作為資源進行訪問,因此,最好立即以這種方式開始訪問它們。 此時,您的代碼將直接從文件系統加載圖像,而不是將其作為資源。 您可以通過調用ImageIO.read()
方法來更改它(鏈接的是由於鏈接到上面鏈接的問題的圖像而使用的URL,但是您也可以通過File
, InputStream
或ImageInputStream
進行讀取。
例如:
img = ImageIO.read(getClass().getResource("L5DGx.png"));
由於每個按鈕使用相同的圖像(每個按鈕可能使用相同的大小),因此建議您使用GridLayout
而不是GridBagLayout
。
您可以復制粘貼此代碼而無需進行修改,這被稱為“ 最小,完整和可驗證的示例(MCVE)”或“ 簡短,自包含,正確的示例(SSCCE)” ,下次您應發布一個類似的示例 ,因此我們沒有編寫導入或推斷該field
是一個JButton
數組而不是另一個JComponent
(例如,一個可能更適合field
名稱的JTextField
)
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ImageIconArray {
private JButton[][] buttons = new JButton[16][16];
private JPanel pane;
private JFrame frame;
private GridBagConstraints constraints = new GridBagConstraints();
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ImageIconArray().createAndShowGui();
}
});
}
public void createAndShowGui() {
BufferedImage img = null;
URL url;
try {
url = new URL("https://i.stack.imgur.com/L5DGx.png");
img = ImageIO.read(url);
} catch (IOException ioe) {
ioe.printStackTrace();
}
Icon cell = new ImageIcon(img);
frame = new JFrame("Example");
pane = new JPanel();
pane.setLayout(new GridLayout(16, 16));
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
buttons[i][j] = new JButton();
buttons[i][j].setIcon(cell);
constraints.gridx = i;
constraints.gridy = j;
constraints.weightx = 1;
pane.add(buttons[i][j], constraints);
}
}
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
這是運行上述代碼時得到的輸出圖像:
您正在創建用於創建GUI的static
方法,建議您改為創建類的實例,然后以這種方式調用該方法(不帶static
修飾符)。
從顯示的代碼中,我推斷出您沒有將程序放在事件分發線程(EDT)上 ,這可能會由於線程問題而導致您出現問題,您可以像在main
方法中那樣,通過調用SwingUtilities#invokeLater()
進行修復SwingUtilities#invokeLater()
方法。
您的變量命名很混亂,您將space
調用給JFrame
,如果您是我,我將其稱為frame
或spaceFrame
。 field
我稱field
到JTextField
不是JButton
陣列,我會調用JButton
陣列buttons
(在復數它指的是其中許多)或fieldButtons
。
(在我看來)您的for
循環是反向的,我將從x
開始,然后繼續到y
(或像每個人一樣做,並在簡單循環中使用i
, j
, k
作為計數器變量,或者使它們具有更具描述性的名稱)
您正在使用count1
和count2
變量,它們不是必需的,您可以設置constraints.gridx = i;
和constraints.gridy = j;
(如果您更喜歡按照上述建議在for
循環中使用i
和j
作為計數器變量)或constraints.gridx = y;
和constraints.gridy = x;
(如果您決定忽略我的提示#4)或constraints.gridx = x;
和constraints.gridy = y;
(如果您決定接受我的小費#4)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.