2015年7月18日土曜日

ウィンドウおよびボタン(なにも機能なし)を作る

以下のコードはjavaで書かれています。以下をコンパイルおよびコマンドプロンプト上で実行するとウィンドウおよびボタンが表示されます。ただし、ボタンを押しても何も起こらないですし、GUIプログラミングの勉強用以外の意味はありません。色々カスタマイズしてみてください。

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;

class Sample extends JFrame{
  public static void main(String args[]){
 Sample frame = new Sample("Practice window.");
 frame.setVisible(true);
 }

  Sample(String title){
 setTitle(title);
 setSize(500, 400);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 JButton btn = new JButton("Button");

 Container contentPane = getContentPane();
 contentPane.add(btn, BorderLayout.SOUTH);
 ImageIcon icon = new ImageIcon("./icon.jpg");/*Set a image in the same folder with the name "icon.jpg" */
 setIconImage(icon.getImage());
   }
}