본문 바로가기

IT自主学習 (2023.05.23~)/Java(2023.05.23~)

プログラミング自主学習 4日目Java Desktop Application/データと演算

Java Desktop Application

import javax.swing.*;   
import java.awt.Dimension;
import java.awt.Toolkit;
public class HelloWorldGUIApp{
    public static void main(String[] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("HelloWorld GUI");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setPreferredSize(new Dimension(800, 300));
                JLabel label = new JLabel("Hello World!!", SwingConstants.RIGHT);
                frame.getContentPane().add(label);
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                frame.setLocation(dim.width/2-400/2, dim.height/2-300/2);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

 

 

以下は、JavaのSwing機能を使用して構築されたGUIのソースコードだ。

GUI(Graphical User Interface)とは?
ユーザーが直感的に理解しやすいように図で表現された方法で、プログラムを直接使用できるようにするものである。 


javax.swing.JFrame.JFrame(String title)        
                                   JFrame("HelloWorld GUI");

特定のタイトルを持つフレームを最初に表示するウィンドウを作成します。


Creates a new, initially invisible Frame with the specified title.

This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale.

Parameters:title the title for the frame                  

 

 

 

void javax.swing.JFrame.setDefaultCloseOperation(int operation)       
                                           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

DefauldCloseOperation= ウィンドウの閉じるボタンの操作

 

Sets the operation that will happen by default when the user initiates a "close" on this frame.
You must specify one of the following choices


DO_NOTHING_ON_CLOSE:何も起こらない。

HIDE_ON_CLOSE:フレームを非表示にする。(例:カカオトーク、LINE)。

DISPOSE_ON_CLOSE:特定のフレームを閉じる。

EXIT_ON_CLOSE:すべてのフレームを終了する。

 

 

 

void java.awt.Component.setPreferredSize(Dimension preferredSize)

                                                                         (Dimension(800, 300));

PreferredSize=コンポーネントの適切なサイズを調整する。


Sets the preferred size of this component to a constant value.
Parameters:preferredSize The new preferred size, or null

 

 


javax.swing.JLabel.JLabel(String text, int horizontalAlignment)

               JLabel("Hello World!!", SwingConstants.RIGHT);


JLabel=
特定のテキストを垂直中央に配置し、水平位置を調整する。

int=整数

horizontalAlignment=テキストの水平位置


Creates a JLabel instance with the specified text and horizontal alignment.
The label is centered vertically in its display area.
Parameters:text The text to be displayed by the label.horizontalAlignment
One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.

LELEADINGはテキストの開始地点から、TRAINLINGは終了地点からの意味する。
日本やアラビア語では右から始まる場合もあるため、LEADINGをうまく活用することが重要だ。

 

 

void java.awt.Window.setLocation(int x, int y)

setLocation=コンポーネントの位置を指定します(プログラム起動時にウィンドウをどこに表示するか)。

Moves this component to a new location. 
The top-left corner of the new location is specified by the x and y parameters in the coordinate space of this component's parent. 

※yパラメータ(y軸)は、一番下ではなく一番上から開始されます。

출저:https://blog.csdn.net/hujyhfwfh2/article/details/89302638

 

 データと演算

 

 データにはさまざまな種類があり、代表的なものには数値(Number)、文字列(String)、音声、映像などがある。
このようなデータをどのように処理し、操作するかは、コンピューターサイエンスとプログラミングにおいて重要である。各データ型には、異なる処理方法がある。

 

 

출저:Youtube '생활코딩', JAVA1 - 6.1. 데이터와 연산