Draw lines on jpanel

ⅰ亾dé卋堺 提交于 2019-12-24 05:49:45

问题


I want to make it like Draw a ruler (line with tick marks at 90 degree angle) just not on jframe but on jpanel.

So I tried:

JFrame f = new JFrame();
JPanel ff = new JPanel();

ff.add(new JComponent() {
...
});

f.add(ff);
...

but I failed. :( How to?


回答1:


You can simply override paintComponent(Graphics g){} for ff and draw your within that method.

i.e.

JPanel ff = new JPanel(){ 
    public void paintComponent(Graphics g){
        // Draw what you want to appear on your JPanel here.
        // g.drawLine(blah blah blah), etc.
    }
};

In which case you have no need for this...

ff.add(new JComponent() {
    ...
});

You don't need this generic component unless you want to implement the custom component as suggest in the link you provided. In the case that you do want to create such a custom component, then you don't need ff, since a JFrame is already a container that can hold your component.



来源:https://stackoverflow.com/questions/14164392/draw-lines-on-jpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!