為什麼在益智遊戲中添加或移動圖像後我的 JPanel 不更新?
益智遊戲中的 JPanel 未更新
在簡單的益智遊戲中,代表圖塊的圖像隨機放置在網格上。每個影像都有“地點”和“數字”屬性,指示其當前和所需的位置。遊戲邏輯正確地交換“數字”匹配的圖像並更新其“位置”屬性。但是,將更新的圖像新增至 JPanel 不會更新顯示的網格。
為了解決此問題,我們將修改 addComponents() 方法以正確刷新 JPanel:
public void addComponents(Img[] im){ this.removeAll(); for(int i=0; i<16; i++){ im[i].addActionListener(this); im[i].setPreferredSize(new Dimension(53,53)); add(im[i]); } // Explicitly revalidate and repaint the JPanel this.revalidate(); this.repaint(); }
透過呼叫 revalidate() 和 repaint(),我們強制 JPanel重新計算其佈局並使用新的佈局更新顯示
或者,考慮以下程式碼範例,它採用了更有效的方法:
import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.GridLayout; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.Timer; import javax.swing.WindowConstants; public class PuzzleGame { private static final int N = 4; private final JPanel panel = new JPanel(new GridLayout(N, N)); private final JButton[][] buttons = new JButton[N][N]; private final BufferedImage image; private PuzzleGame() throws IOException { image = ImageIO.read(new File("image.jpg")); createButtons(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(panel); frame.pack(); frame.setVisible(true); // Timer to simulate image swapping Timer timer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { shuffleButtons(); } }); timer.start(); } private void createButtons() { int count = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { buttons[i][j] = new JButton(); buttons[i][j].setPreferredSize(new Dimension(50, 50)); int w = image.getWidth() / N; int h = image.getHeight() / N; BufferedImage subImage = image.getSubimage(j * w, i * h, w, h); buttons[i][j].setIcon(new ImageIcon(subImage)); panel.add(buttons[i][j]); buttons[i][j].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JButton b = (JButton) e.getSource(); // Swap logic here } }); count++; } } } private void shuffleButtons() { Container parent = panel.getParent(); if (parent != null) { panel.remove(buttons[0][3]); parent.add(buttons[0][3], 0); } // Update the UI panel.revalidate(); panel.repaint(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { new PuzzleGame(); } catch (IOException e) { e.printStackTrace(); } } }); } }
此程式碼建立切片影像按鈕並將它們儲存在4x4 網格中。計時器透過將右上角的按鈕移動到第一個位置來刺激影像洗牌。 revalidate() 和 repaint() 方法確保 UI 在每次按鈕移動後正確更新。
以上是為什麼在益智遊戲中添加或移動圖像後我的 JPanel 不更新?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...
