我在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)
答案 0 :(得分:0)
错误就是这句话:
(proxy-super rd row col)
正确的编码方式是:
(def c (proxy-super prepareRenderer [rd row col]))
和行:
(.setOpaque this false)
this
将替换为:
(if (instance? JComponent c)
(.setOpaque c false))
c