我在 clojure 中编写了这个程序来将背景图像添加到 JTable。我被这个prepareRenderer
功能困住了。似乎 clojure 无法处理第一个参数rd
(等效 Java TableCellRenderer rd
)。我可能在这里做错了,如果有人可以帮助我解决这个问题,我将非常感激。要运行该程序,只需将其保存到一个tabletest.clj
文件并在 clojure 提示符下键入:
(load-file "tabletest.clj")
如果您注释掉该prepareRenderer
函数,您可以在不看到背景图像的情况下成功运行代码。
请为 JTable 背景提供您自己的图像文件。该程序如下所列:
(import '(javax.swing JFrame JTable JPanel JScrollPane)
'(javax.swing.table DefaultTableModel TableCellRenderer)
'(java.awt Component BorderLayout Dimension Image))
(import javax.imageio.ImageIO)
(import java.io.File)
(defn tabletest []
(let [tableData (to-array-2d [
["numbers" "67890" "This"]
["mo numbers" "2598790" "is"]
["got Math" "2598774" "a"]
["got Numbers" "1234567" "Column"]
["got pi" "3.1415926" "Apple"]
])
colNames (to-array ["Col Labels" "Go" "Here"])
frame (JFrame. "Table Example")
panel (JPanel.)
sP (JScrollPane.)
dataModel (DefaultTableModel. tableData colNames)
table (proxy [JTable] [dataModel]
(prepareRenderer [rd row col] ; problem starts here
(proxy-super rd row col) ; Don't know how to translate
; Java code below to clojure:
(.setOpaque this false) ;if (c instanceof JComponent)
; ((JComponent)c).setOpaque(false);
this
)
(paintComponent [g2d]
;(proxy-super paintComponent g2d)
(def image (ImageIO/read (File. "GreenCar.png")))
(.drawImage g2d image 0 0 this)
(proxy-super paintComponent g2d)
)
)
]
(.setCellSelectionEnabled table true)
(.setOpaque table false)
(.setPreferredSize sP (Dimension. 300 150))
(.setView (.getViewport sP) table)
(.add panel sP)
(doto frame
(.setLayout (BorderLayout.))
(.add panel BorderLayout/CENTER)
(.setSize 400 200)
(.setVisible true)
)
)
)
(tabletest)