我扩展了两个jtextpane,可以在其中一个(debug)上面添加断点图标,然后在另一个(edit)jtextpane上添加highlight(高亮度条)。它们被加在一个jscrollpane上,edit是始终获得焦点,可以在edit光标所在的行用快捷键在左边的debug上添加断点图标。现在我是把它们都写在了一起,请问怎么把debug封装成一个独立的类。
debug中用到edit的地方是lp=new LinePainter(edit,currentColor);
                                lp.addHighlight(TextUtils.getHead(Inf[0],edit.editLines), Info[0]);
                                edit.repaint();
debug的构造函数中需要一个edit对象参数
使用快捷键的时候,以edit中光标所在的行为准去添加断点(需要一个debug的对象传入edti的构造函数)。
所以说就很麻烦,麻烦了各位。

解决方案 »

  1.   

    class LinePainter extends DefaultHighlighter.DefaultHighlightPainter{
        
            private JTextComponent component;
            private DefaultHighlighter highlighter;
            private Lightline lightLine;
            
            /**
             * @param component paint的组件
             * @param color paint的颜色 
             */
            public LinePainter( JTextComponent component, Color color){
                
                super( color );
                this.component = component;            highlighter =  (DefaultHighlighter)component.getHighlighter();
                highlighter.setDrawsLayeredHighlights( true );
              
            }
        
        
            /**
             * This method is overridden to get desired behaviour.
             *
             * The offs1 parameter is ignored and the entire line is highlighted.
             *
             * @param g the graphics context
             * @param offs0 the starting model offset >= 0
             * @param offs1 the ending model offset >= offs1
             * @param bounds the bounding box of the view, which is not necessarily the region to paint.
             * @param c the editor
             * @param view View painting for
             * @return region drawing occured in
             */
            public Shape paintLayer( Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view ){
                try
                {
                    // Only use the first offset to get the line to highlight
                    Rectangle r = c.modelToView( offs0 );
                    r.x = 0;
                    r.width = c.getSize().width;                // --- render ---
                    g.setColor( getColor() );
                    g.fillRect( r.x, r.y, r.width, r.height );
                    return r;
                }
                catch ( BadLocationException e )
                {
                    return null;
                }
            }
            /** 添加高亮度条 */
            private void addHighlight( int offset,int i ){           lightLine=new Lightline(i);
               
               try{
                   highlighter.addHighlight( offset, offset + 1, this );
               }
               catch( BadLocationException ble ){
                   ble.printStackTrace();
               }
               //以lighlight数去设置lightLine的count
               lightCount++;
               lightLine.setCount(lightCount);
              
               lightLines.addElement(lightLine);        }        /** 删除高亮度条 */
            private void removeHighlight(int i){
                int c=0;
                boolean b=false;
                
                Highlighter.Highlight[] highlights = highlighter.getHighlights();            for(int j=0;j<lightLines.size();j++){
                    lightLine=(Lightline)lightLines.elementAt(j);
                    if(lightLine.getIndex()==i){
                        c=lightLine.getCount();
                        lightLines.remove(j);
                        b=true;
                    }
                   
                }
                
                //删除highline之后把count在它后面的count -1
                if(b){
                     for(int j=0;j<lightLines.size();j++){
                        lightLine=(Lightline)lightLines.elementAt(j);
                        if(lightLine.getCount()>c)
                            lightLine.setCount(lightLine.getCount()-1);
                    }
                }
               
                Highlighter.Highlight h = highlights[c];            try{
                    highlighter.removeHighlight(h);            }
                catch(Exception e){
                    e.printStackTrace();
                }
                
                lightCount--;
            }
     
     
        }