按照CustomCanvas.java 中的示例
我已經成功地用這段代碼在自定義 Swing 畫布中為一個 mxCell 畫了一個 JB 按鈕
public void drawVertex(mxCellState state,String label)
{
Object value = ((mxCell) state.getCell()).getValue()。
Pattern p = (Pattern)value。
System.out.println(p.length)。
MyPanel comp = new MyPanel()。
rendererPane.paintComponent(g, comp, graphComponent,
(int) (state.getX() translate.getX() )。
(int) (state.getY() translate.getY() )。
(int) state.getWidth(), (int) state.getHeight(), true) 。
g.setColor(Color.GREEN)。
for (int i = 0; i< p.length; i ) {
JButton b =new JButton(")。
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("click,click")。
}
});
rendererPane.paintComponent(g, b, graphComponent,
(int) (state.getX() translate.getX() i*state.getWidth()/p.long)。
(int) (state.getY() translate.getY() )。
(int) state.getWidth()/p.length, (int) state.getHeight(), true) 。
}
按鈕被畫出來了,但它并不作業,如果我點擊它,什么也不會發生
。uj5u.com熱心網友回復:
如果你希望組件是互動式的,你必須把它添加到容器中(容器本身被添加到另一個容器中,最終達到一個JFrame)。你現在所做的只是把它畫進一個單元格。
理論上,您可以向后彎曲以攔截對單元格的點擊并將其轉發至 JB 按鈕,但這可能不是一個可持續的方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/310278.html
標籤:
