Let's make a calculator application with Java ~ Create a display area in the window

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

In Previous article, the application window was displayed. This time we will create a display area inside the window. In the previous code, the goal was to display the application window using Jframe for the time being, so I wrote all the code in the main method, but considering the ease of writing and reading in the future, I decided to use the MyFrame method. Created and instantiated in the main method.

MyFrame.java


import javax.swing.JFrame;

public class MyFrame extends JFrame{
    public static void main(String[] args) {
        MyFrame frame = new MyFrame("Java Swing test");
        frame.setVisible(true);
    }

    MyFrame(String title){
        setTitle(title);
        setSize(500, 600);                                //Window size(width,height)
        setLocationRelativeTo(null);                      //Display window in the center of the screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   // x(X)Quit the application with the button.
    }
}

JavaSwing has a layered structure in which the base JFrame and panels and buttons are placed on it. Since JFrame only displays a window, let's create a panel that displays the numbers required for the calculator app.

MyFrame.java


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Container;



public class MyFrame extends JFrame{
    public static void main(String[] args) {
        MyFrame frame = new MyFrame("Java Swing test");
        frame.setVisible(true);
    }

    MyFrame(String title){
        setTitle(title);
        setSize(500, 600);                                //Window size(width,height)
        setLocationRelativeTo(null);                      //Display window in the center of the screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   // x(X)Quit the application with the button.

        setLayout(new FlowLayout());

        JPanel panelDisplay = new JPanel();                         //Panel instantiation
        panelDisplay.setPreferredSize(new Dimension(500, 60));      //Panel size
        panelDisplay.setBackground(new Color(51, 51, 51));          //Color code#333333
        BevelBorder border = new BevelBorder(BevelBorder.RAISED);
        panelDisplay.setBorder(border);

        Container contentPane = getContentPane();
        contentPane.add(panelDisplay);
    }
}

The execution result is as shown in the figure below. This time, the panel size was set to 500 x 60 and the color was set to # 333333. After this, there are also arrangements such as buttons, so there may be changes in size etc.

JavaApp003.png


This article table of contents page

Try making a calculator app in Java

Recommended Posts

Let's make a calculator application with Java ~ Create a display area in the window
Let's make a calculator application in Java ~ Display the application window
Let's create a TODO application in Java 5 Switch the display of TODO
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 2 I want to create a template with Spring Initializr and make a Hello world
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 12 Processing when a request comes in with an unused HttpMethod ・ Processing when an error occurs in the server
Let's create a TODO application in Java 1 Brief explanation of MVC
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 3 Save temporary data in MySQL-> Get all-> Display on top
Create a CSR with extended information in Java
Let's create a timed process with Java Timer! !!
Let's create a super-simple web framework in Java
[Java basics] Let's make a triangle with a for statement
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
Create a SlackBot with AWS lambda & API Gateway in Java
Create a method to return the tax rate in Java
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Let's make a book management web application with Spring Boot part1
I can't create a Java class with a specific name in IntelliJ
Let's make a book management web application with Spring Boot part3
Let's make a book management web application with Spring Boot part2
Install Rails in the development environment and create a new application
Let's create a TODO application in Java 13 TODO form validation 1: Character limit ・ Gradle update to use @Validated
Let's make a Christmas card with Processing!
Create a jar file with the command
Create a simple web application with Dropwizard
Let's express the result of analyzing Java bytecode with a class diagram
[Java] Create a jar file with both compressed and uncompressed with the jar command
Let's create a Java development environment (updating)
Create a TODO app in Java 7 Create Header
Let's make a smart home with Ruby!
Try making a calculator app in Java
Split a string with ". (Dot)" in Java
I tried to make a talk application in Java using AI "A3RT"
[Java] Let's make a DB access library!
Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack
Create a java web application development environment with docker for mac part2
Let's create a versatile file storage (?) Operation library by abstracting file storage / acquisition in Java
Validate the identity token of a user authenticated with AWS Cognito in Java
Read a string in a PDF file with Java
Create a simple bulletin board with Java + MySQL
[LeJOS] Let's control the EV3 motor with Java
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
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
Try to create a bulletin board in Java
Let's create a custom tab view in SwiftUI 2.0
[Java] Create a collection with only one element
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
Create Scala Seq from Java, make Scala Seq a Java List
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
Let's make a search function with Rails (ransack)
Create a multi-key map with the standard library
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]