Using Replit to create and share GUI's - tstorrnetnz/teaching2022 GitHub Wiki

How to think like a computer scientist (HTTLACS)

Otago Workbook L2

Otago Workbook L3


Learning Aim: Use Replit to create and share GUI's so that your teacher and others can see your work

Replit can be used to create Java Swing GUI's. The advantages are:

  • No software has to be installed on your computer
  • Runs from anywhere
  • The program is easily shared with others
  • Replit integrates with code versioning software such as Github

Some disadvantages include:

  • Replit sometimes runs slow
  • Code must either be hand coded or copied from an IDE
  • There must be a Main class with a Main method for the repl to run.

This last point is essential and is covered in detail below.

Using a Main Class and method to start your GUI

Repl needs a Main class and a Main method to run. When creating Java Swing GUI's it is straight forward, but essential to do this. In the following repl there are two classes, Main and BorderPanel.

BorderPanel contains the code to create a small panel with coloured rectangles. Main has a Main method only, which creates a new BorderPanel instance.

import javax.swing.JFrame;

public class Main{

public static void main(String[]args){

    JFrame window = new JFrame("A window");
    window.getContentPane().add(new BorderPanel());
    window.setVisible(true);  
    window.pack();  
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

This Main class/method can be customised to run other GUI classes simply by replacing new BorderPanel in window.getContentPane().add(new BorderPanel());

to have the name of the GUI class (e.g. TemperatureConverterGUI).

GUI in Repl