Let's make a calculator application in Java ~ Display the application window

Let's start by using JavaSwing to display the application window.

The Java development environment uses OpenJDK 11.0.4 installed on Ubuntu 18.04.

Window display with JFrame

MyFrame.java


import javax.swing.JFrame;

public class MyFrame extends JFrame{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Java Swing test");
        frame.setVisible(true);
        // x(X)Quit the application with the button.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Window display position and size(Coordinates x,Coordinate y,width,height)
        frame.setBounds(300, 80, 500, 600);   
    }
}

Running this code will display an empty window at the specified position (x = 300, y = 80) and at the specified size (500, 600).

Display window in the center of the screen

In setBounds of the above code, the position and size of the window are specified, but by replacing setBounds with the following code, the window can be displayed in the center of the screen.

//Window size(width,height)
frame.setSize(500, 600);
//Display window in the center of the screen
frame.setLocationRelativeTo(null);

The window is. I want to display it in the center of the screen, so I will actually replace it.

MyFrame.java


import javax.swing.JFrame;

public class MyFrame extends JFrame{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Java Swing test");
        frame.setVisible(true);
        // x(X)Quit the application with the button.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        //Window size(width,height)
        frame.setSize(500, 600);
        //Display window in the center of the screen
        frame.setLocationRelativeTo(null);
    }
}

The execution result is here.

JavaApp001.png


This article table of contents page

Try making a calculator app in Java

Recommended Posts

Let's make a calculator application in Java ~ Display the application window
Let's make a calculator application with Java ~ Create a display area in the window
Let's create a TODO application in Java 5 Switch the display of TODO
Let's make a communication application in LAN Part 2 Display a window using JavaFX / Wait for socket reception
Let's create a TODO application in Java 4 Implementation of posting function
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 8 Implementation of editing function
Let's create a TODO application in Java 1 Brief explanation of MVC
Try making a calculator app in Java
[Java] Let's make a DB access library!
Let's create a TODO application in Java 3 Save temporary data in MySQL-> Get all-> Display on top
I tried to make a talk application in Java using AI "A3RT"
A story about the JDK in the Java 11 era
How to display a web page in Java
Measure the size of a folder in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
Let's create a super-simple web framework in Java
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Make Blackjack in Java
Let's create a TODO application in Java 9 Create TODO display Sort by date and time + Set due date default to today's date
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
Make "I'm not a robot" in Java EE (Jakarta EE)
[Java basics] Let's make a triangle with a for statement
[Personal memo] Make a simple deep copy in Java
A note for Initializing Fields in the Java tutorial
Let's make a robot! "A simple demo of Java AWT Robot"
I tried to make a login function in Java
[Introduction to Android application development] Let's make a counter
Let's create a TODO application in Java 11 Exception handling when accessing TODO with a non-existent ID
Let's create a TODO application in Java 12 Processing when a request comes in with an unused HttpMethod ・ Processing when an error occurs in the server
[Java] Throw a request and display the screen ② (GET / POST)
A quick explanation of the five types of static in Java
Let's make the app better
Refactoring: Make Blackjack in Java
[Java] Throw a request and display the screen (GET / POST)
I just wanted to make a Reactive Property in Java
Find a subset in Java
Create a method to return the tax rate in Java
[Java] Make it a constant
I tried to make a client of RESAS-API in Java
Make a rhombus using Java
Let's make a communication application in LAN Part 4 Set information in JavaFX control / Call JavaFX screen from thread
Let's make a LAN communication application Part 1 New project creation using Maven and Java entry point
Let's create a TODO application in Java 13 TODO form validation 1: Character limit ・ Gradle update to use @Validated
Let's make a book management web application with Spring Boot part1
Let's make a book management web application with Spring Boot part3
Let's make a combination without duplication | First, calculate the total number
Let's make a book management web application with Spring Boot part2
Automatically deploy a Web application developed in Java using Jenkins [Preparation]
Get the public URL of a private Flickr file in Java
[Java] I tried to make a maze by the digging method ♪
Install Rails in the development environment and create a new application
Let's make a custom_cop that points out the shaking of the name
Access the network interface in Java
Guess the character code in Java
Make a language! (Making a simple calculator ②)
3 Implement a simple interpreter in Java
I created a PDF in Java.
Display Firestore data in RecyclerView [Java]
How to make a Java container