本文實例為大家分享了Java流布局圖形界面編寫代碼,供大家參考,具體內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package jisuanqi; import java.awt.*; public class MyFrame extends Frame{ //繼承Frame類 public MyFrame() { super ( "第一個圖形界面" ); //設置框架窗口標題 this .setSize( 200 , 130 ); //設置組件尺寸(寬,高) this .setLocation( 300 , 240 ); //設置組件的顯示位置 this .setBackground(Color.lightGray); //設置組件的背景顏色 this .setLayout( new FlowLayout()); //設置的容器布局為流布局,居中 this .add( new Label( "姓名:" )); //創建標簽,添加到框架上 this .add( new TextField( "陳浩翔" , 10 )); //創建文本行,10列 this .add( new Label( "密碼" )); this .add( new TextField( 10 )); //創建10列的文本行 this .add( new Button( "OK" )); //創建按鈕 this .add( new Button( "Cancel" )); //創建按鈕 this .setVisible( true ); //是否顯示框架窗口,必須在添加組件后 } public static void main(String[] args) { new MyFrame(); } } |
第一次寫圖形界面的源代碼,小小的激動啊。
流布局的圖形構造方法默認為居中排列;
對齊常量:
LEFT(0):左對齊
CENTER(1):居中
RIGHT(2):右對齊
public FlowLayout(int align)
//align參數指定對齊方式,取值為對齊常量。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/qq_26525215/article/details/49721107