Groovy classpath issue

2019-08-07 06:11发布

问题:

I have run the following code in this page RsyntaxTextArea using Java and i run the program exactly the way that is been mentioned in this site.And i'm getting the output as intended. But i have tried to modify this java code to Groovy code, something like:

import groovy.swing.SwingBuilder
import javax.swing.*
import java.awt.*
swing =  new SwingBuilder()
frame = swing.frame(title : "test", defaultCloseOperation:JFrame.EXIT_ON_CLOSE, pack:true, show : true, size :[100,100])
{
        panel
        {
             RSyntaxTextArea textArea = new RSyntaxTextArea();
             textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        }
}

And when i try to run this script as follows:

groovyc -classpath rsyntaxtextarea.jar TextEditorDemo.groovy 

I get the errors stating that:

groovy: 9: unable to resolve class RSyntaxTextArea 
 @ line 9, column 19.
        RSyntaxTextArea textArea = new RSyntaxTextArea();
                     ^

/home/anto/Groovy/Rsyntax/ST.groovy: 9: unable to resolve class RSyntaxTextArea 
 @ line 9, column 30.
        RSyntaxTextArea textArea = new RSyntaxTextArea();
                                ^

/home/anto/Groovy/Rsyntax/ST.groovy: 10: unable to resolve class RSyntaxTextArea 
 @ line 10, column 7.
         textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);

I guess i have made wrong in running the program. How i do i run the program in this case by defining the classpath too.

回答1:

It doesn't look like you're importing the package for RSyntaxTextArea. Have you tried adding the following imports to your program?

import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;


回答2:

This code should do what you want... You needed to add the RSyntaxTextArea into the view (using the widget method)

You also needed to add it into a JScrollPane, so that it scrolls nicely when full.

import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL
import static javax.swing.JFrame.EXIT_ON_CLOSE
import org.fife.ui.rsyntaxtextarea.*

RSyntaxTextArea textArea = new RSyntaxTextArea()
textArea.syntaxEditingStyle = SyntaxConstants.SYNTAX_STYLE_JAVA

swing =  new SwingBuilder()
frame = swing.frame(title:"test", defaultCloseOperation:EXIT_ON_CLOSE, size:[600,400], show:true ) {
  borderLayout()
  panel( constraints:BL.CENTER ) {
    borderLayout()
    scrollPane( constraints:BL.CENTER ) {
      widget textArea
    }
  }
}

edit

Without using widget, your code would need to look something like this:

import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL
import static javax.swing.JFrame.EXIT_ON_CLOSE
import org.fife.ui.rsyntaxtextarea.*

RSyntaxTextArea textArea = new RSyntaxTextArea()
textArea.syntaxEditingStyle = SyntaxConstants.SYNTAX_STYLE_JAVA

swing =  new SwingBuilder()
frame = swing.frame(title:"test", defaultCloseOperation:EXIT_ON_CLOSE, size:[600,400], show:true ) {
  borderLayout()
  panel( constraints:BL.CENTER ) {
    borderLayout()
    sp = scrollPane( constraints:BL.CENTER )
    sp.viewport.add textArea
  }
}