4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JAVA GUI(Graphical User Interface

Last updated at Posted at 2015-11-19

■Test18.java

import java.awt.FlowLayout;

import javax.swing.*;

public class Test18 {
	public static void main(String[] args) {
		JFrame frame = new JFrame("はじめてのGUI");
		JLabel label = new JLabel("Hello World!!");
		JButton button = new JButton("押してね");
		frame.getContentPane().setLayout(new FlowLayout());
		frame.getContentPane().add(label);
		frame.getContentPane().add(button);
        // setDefaultCloseOperation()→ユーザーがこのフレームで「クローズ」を開始した時に実行される処理を返す
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 100);
        // ウィンドウを表示する
		frame.setVisible(true);
	}

}

■実行結果
testgui.png

ContentPaneとは、JFrameの表示領域です。

 JFrameに表示したいコンポーネントは、ContentPaneに追加する必要があります。

一番右の「×」ボタンを押した時にどのような処理を行うのかを設定します。設定を行うにはJFrameクラスで定義されているsetDefaultCloseOperationメソッドを使います。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?