How to Create a Java GUI App that shows the Logged-in User and Hostname

0 min read 93 words

Create a new file called Application.java and paste the following:

import javax.swing.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Application {
    public static void main(String...args) throws UnknownHostException {
        JFrame frame = new JFrame("Sample App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);

        String user = System.getProperty("user.name");
        String host = InetAddress.getLocalHost().getHostName();

        JTextArea txt = new JTextArea();
        txt.setText(
                "\n" +
                "  User: "+user+"\n" +
                "  Host: "+host+"\n"
        );
        frame.getContentPane().add(txt);

        frame.setVisible(true);
    }
}

Now open the terminal/command-line and type:

java Application.java

This will show a GUI application that will have a textarea with two lines of text:

  1. Your logged-in username
  2. The machine’s hostname
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts