Hello World, a cross-platform GUI app with Groovy running on the Java platform

What is Groovy

A scripting language that runs on Java. It is the result of adding the necessary functions to Java.

Groovy quick install

For Windows

  1. If not, install JDK (Java SE Development Kit).
  2. Download the installer from groovy site. At the time of writing, Groovy 2.4 was the latest stable version.
  3. Run the installer.
  4. If you can't find the Groovy environment variable in your PATH, add it. (The uninstaller does not remove this information) A warning will be displayed if JAVA_HOME is not set. Without it, Groovy will not work properly. If you get a warning, check that you may not have the JDK installed.
  5. Installation is complete when "Start Groovy Console" is displayed in the Start menu.

Hello Groovy World

  1. Start GroovyConsole.
  2. Write the following script at the top of the screen (script description area). println("HelloWorld");
  3. Press the ** Execute Groovy Script ** button or press ** Ctrl + R ** to execute the script.
  4. Success if * Hello World * is displayed as the execution result at the bottom of the screen. image.png

Also try GUI (AWT Frame). Below is the execution screen of Hello Groovy World (AWT).

image.png

Example) HelloGroovyWorld(AWT)


/* Hello World (GUI)script*/
import java.awt.*;
import java.awt.event.*;

/**
 *MainFrame class
 */
class MainFrame extends Frame
{
  def _menuBar;
  def _labelBody;

 /**
  *constructor
  */
  MainFrame() {

    /*Initialize title*/
    setTitle("Hello World")

    /*Initialize menu*/
    _menuBar = new MenuBar();
    def _menu = new Menu("File");
    def _menuItem = new MenuItem("close");
    _menuItem.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(ActionEvent e) {
            closeFrame(e);
        }
    });
    _menu.add(_menuItem);
    _menuBar.add(_menu);
    setMenuBar(_menuBar);

    /*Initialize in-frame display content*/
    _labelBody = new Label("Hello World");
    this.add(_labelBody);

    /*Initialize size*/
    setSize(new Dimension(300, 200));
  }

  /*Frame close operation*/
  def closeFrame(e)
  {
    this.dispose();
  }
}

/*Entry point*/
new MainFrame().show();

Groovy Features for Java

Reference: Apache Groovy --Differences with Java Reference: Code Zine --What kind of language is Groovy? Introduction to Groovy for Java Programmers

--Some of the class packages that are commonly used are imported by default, so you can use them without declaring them.

--Loose source code writing rules compared to Java (opposite of Strict). For example, the colon at the end of the line is unnecessary and you do not have to declare the data type. You can also write statements that do not belong to any class. (Loose statement) -Declaring a variable with def dynamically changes the appropriate type according to the value assigned to the variable. --Map type is available. map = ["Alice": 10, "Bob": 20, "Charlie": 30]; println (map ["Alice"]); --In the control syntax, it is implicitly false if the evaluation result is 0 or null, and true otherwise. --The switch statement can be divided into cases by evaluating other than numerical values. --The format of the for statement is different. for (m in list) {print m.toString ();}; --A dynamic class definition (Expando) function is provided. def e = new Expando (); e.msg =" Hello World "; e.show = {println (msg);}; e.show (); --The java.sql package makes it easy to write SQL query processing. --Builder allows you to easily describe structured data such as HTML / XML / Swing. ʻImport groovy.xml.MarkupBuilder; new MarkupBuilder (). html {body {div {mkp.yield ('Hello World')}; div (style:'style.css') {mkp.yield ('Groovy')} }} `

Recommended Posts

Hello World, a cross-platform GUI app with Groovy running on the Java platform
Create a Hello World web app with Spring framework + Jetty
Try Hello World using plain Java on a Docker container
Hello World on AWS Lambda + Java
Try Hello World with the minimum configuration of Heroku Java spring-boot
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Hello world with Java template engine Thymeleaf
Java development with Codenvy: Hello World! Run
Hello World on Mac VS Code Java
Deploy a Java web app on Heroku
I want to play a GIF image on the Andorid app (Java, Kotlin)
Come out with a suffix on the method
Publish the app made with ruby on rails
Run JSP Hello World with Tomcat on Docker
Come out with a suffix on the method 2
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Display "Hello World" in the browser using Java
Show a simple Hello World with SpringBoot + IntelliJ
Java starting with JShell-A peek into the Java world
I tried running Java on a Mac terminal
Java Hello World
Try running an app made with Quarkus on Heroku
Compile with Java 6 and test with Java 11 while running Maven on Java 8
[Introduction] Display Android Studio Hello World on the emulator
Hello World (console app) with Apache Camel + Spring Boot 2
How to reduce the load on the program even a little when combining characters with JAVA
"Hello World" in Java
Java Learning (1)-Hello World
Hello World on WebAssembly
Hello World with Micronaut
[Java] One type of alphabet prohibited With binding Hello, world! [Binding]
Hello World with Ruby extension library for the time being
How to deploy a simple Java Servlet app on Heroku
[Swift 5] Select a date with the IDate Picker on iOS14
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ① ~ Hello World ~
How to deploy a kotlin (java) app on AWS fargate
About the behavior when doing a file map with java
With [AWS] CodeStar, you can build a Spring (Java) project running on Lambda in just 3 minutes! !!