6.GUIプログラミング

(1)AWT  javaに最初から提供されているGUIコンポーネントのことです。

    
package sample001;				
				
import java.awt.Frame;				
import java.awt.Graphics;				
import java.awt.event.WindowAdapter;				
import java.awt.event.WindowEvent;				
				
class sample001 extends Frame {				
	public static void main(String[] args) {			
		sample001 app = new sample001();		
	}			
				
	public sample001() {			
		setSize(1000, 500);		
		setVisible(true);		
				
		addWindowListener(new WindowAdapter() {		
			public void windowClosing(WindowEvent e) {	
				System.exit(0);
			}	
				
		});		
	}			
	public void paint(Graphics g) {			
		g.drawString("Hello Java AWT!", 10, 60);		
	}			
}				
(2)Swing  どのOSのウィンドウシステム上で使っても同じ見た目と操作ができるGUIコンポーネント。
package sample006;							
							
import java.awt.Container;							
							
import javax.swing.JButton;							
import javax.swing.JFrame;							
import javax.swing.JLabel;							
import javax.swing.JTextArea;							
							
class sample006 {							
	public static void main(String args[]) {						
		JFrame J = new JFrame("Label TEST");					
							
		// ウィンドウズをクローズしたら終了					
		J.setDefaultCloseOperation(J.EXIT_ON_CLOSE);					
							
		// フレームのコンテナを得る					
		Container C = J.getContentPane();					
							
		C.setLayout(null);					
		// ラベルを作る					
		JLabel L = new JLabel("ラベルです。");					
		// 文字列つきボタンを作る					
		JButton B = new JButton("P U S h");					
		//テキストエリアを作る					
		JTextArea T=new JTextArea("これがTextです。\n",3,20);					
		T.append("複数行のテキスト領域です");					
		T.insert("Area",7);					
							
		// ラベルをコンテナに入れる					
		C.add(L);					
		// ボタンをコンテナにいれる					
		C.add(B);					
		// テキストをコンテナにいれる					
		C.add(T);					
							
		//配置する。					
		L.setBounds(300, 100, 100, 100);					
		B.setBounds(300, 200, 100, 100);					
		T.setBounds(300, 300, 100, 100);					
							
		// フレームのサイズを設定					
		J.setSize(1000, 500);					
		J.setVisible(true);					
	}						
}							

SwingプログラミングPOINT

①フレームの宣言(タイトルがLabel Test):JFrame J = new JFrame("Label TEST"); ② ウィンドウをクローズ: J.setDefaultCloseOperation(J.EXIT_ON_CLOSE); ③フレームのコンテナを得る:Container C = J.getContentPane(); ④ラベルを作る:JLabel L = new JLabel("ラベルです。"); ⑤文字列つきボタンを作る:JButton B = new JButton("P U S h"); ⑥テキストエリアを作る:JTextArea T=new JTextArea("これがTextです。\n",3,20); ⑦配置する。:L.setBounds(300, 100, 100, 100);

(3)レイアウト javaでは配置にレイアウトマネージャーが用意されており、9種類の配置方法があります。 パッケージ クラス java.awt  FlowLayout  GridLayout  BorderLayout  CardLayout  GridBagLayout javax.swing  BoxLayout  OverlayLayout  ScrollPaneLayout  ViewPortLayout フィールド名 整列方法 LEFT    各行のGUIコンポーネントを左揃えにする CENTER  各行のGUIコンポーネントを中央揃えにする RIGHT  各行のGUIコンポーネントを右揃えにする LEADING  各行のGUIコンポーネントをコンテナの方向のリーディングエッジに備える TRAILING  各行のGUIコンポーネントをコンテナの方向のトレイリングエッジに揃える ①フローレイアウト
package sample007;					
import java.awt.Container;					
import java.awt.FlowLayout;								
import javax.swing.JButton;					
import javax.swing.JFrame;					
					
public class SwingLayout {					
					
	public static void main(String[] args) {				
		// TODO 自動生成されたメソッド・スタブ			
		JFrame f = new JFrame("FlowLayout1");			
		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);			
		Container c = f.getContentPane();			
		// レイアウトマネージャのインスタンスを作る			
		FlowLayout f1 = new FlowLayout(FlowLayout.CENTER,15,15);			
		// レイアウトを設定			
		c.setLayout(f1);			
					
		// ボタンを作ってコンテンツ区画に追加			
		c.add(new JButton("1"));			
		c.add(new JButton("2"));			
		c.add(new JButton("3"));			
		c.add(new JButton("4"));			
		c.add(new JButton("5"));			
		c.add(new JButton("6"));			
					
		f.pack();			
		f.setVisible(true);			
	}				
					
}
②ボーダーレイアウトとグリッドレイアウトを使用した矢印パネル
package sample008;				
				
import java.awt.BorderLayout;				
import java.awt.GridLayout;				
				
import javax.swing.JButton;				
import javax.swing.JFrame;				
import javax.swing.JLabel;				
import javax.swing.JPanel;				
				
class SwingLayout2 {				
				
	public static void main(String[] args) {			
		// TODO 自動生成されたメソッド・スタブ		
		JFrame f = new JFrame("JFrame");		
				
		JPanel p = new JPanel();		
				
		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);		
		// レイアウトマネージャのインスタンスを作る		
		GridLayout g1 = new GridLayout(3, 3);		
		// レイアウトを設定		
		p.setLayout(g1);		
				
		// ボタンを作ってコンテンツ区画に追加		
		p.add(new JLabel(""));// ダミー		
		p.add(new JButton("↑"));		
		p.add(new JLabel(""));// ダミー		
		p.add(new JButton("←"));		
		p.add(new JLabel("")); // ダミー		
		p.add(new JButton("→"));		
		p.add(new JLabel("")); // ダミー		
		p.add(new JButton("↓"));		
		p.add(new JLabel("")); // ダミー		
				
		// フレームのコンテンツ区画にボタンを乗せる		
		f.add(new JButton("CANCEL"), BorderLayout.NORTH);		
				
		//フレームのコンテンツ区画(中央)にパネルを載せる		
		f.add(p,BorderLayout.CENTER);		
				
		//フレームのコンテンツ区画(中央)にパネルを載せる		
		f.add(new JButton("O K"), BorderLayout.SOUTH);		
				
		f.pack();		
		f.setVisible(true);		
	}			
				
}