Skip to content

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

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); } }
Code language: Java (java)

Now open the terminal/command-line and type:

java Application.java
Code language: Bash (bash)

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

See also  Find the unique number using Java
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x