首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义秋千画布中的mxGraph JButton?

自定义秋千画布中的mxGraph JButton?
EN

Stack Overflow用户
提问于 2021-08-30 18:29:05
回答 1查看 67关注 0票数 0

遵循CustomCanvas.java中的示例

我用以下代码在自定义Swing画布中为JButton绘制了一个mxCell

代码语言:javascript
运行
复制
        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);
            }
        }

按钮被绘制,但它不工作,如果我点击它,什么都不会发生

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-30 23:19:03

如果希望组件具有交互性,则必须将其添加到容器中(容器本身被添加到另一个容器中,最终到达JFrame)。你现在所拥有的只是把它描绘成一个细胞。

从理论上讲,你可以向后弯曲来拦截手机的点击,并将其转发给JButton,但这可能不是一种可持续的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68988786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档