遵循CustomCanvas.java中的示例
我用以下代码在自定义Swing画布中为JButton绘制了一个mxCell
        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.length),
                    (int) (state.getY() + translate.getY()),
                    (int) state.getWidth()/p.length, (int) state.getHeight(), true);
            }
        }按钮被绘制,但它不工作,如果我点击它,什么都不会发生
发布于 2021-08-30 23:19:03
如果希望组件具有交互性,则必须将其添加到容器中(容器本身被添加到另一个容器中,最终到达JFrame)。你现在所拥有的只是把它描绘成一个细胞。
从理论上讲,你可以向后弯曲来拦截手机的点击,并将其转发给JButton,但这可能不是一种可持续的方法。
https://stackoverflow.com/questions/68988786
复制相似问题