java AWT图形化界面学习
java的图形化界面之一,虽然被淘汰了,但是思想很重要。
最基本的窗口输出:(没有监听事件)
把组件(Components)放在面板(panel)上,把面板放在窗口上,通过设置面板和窗口的布局(流式布局(FlowLayout)、东西南北中布局(BorderLayout)、表格布局(GridLayout))实现各种各样的输出窗口。
1 package com.yycome.awt_1; 2 3 import java.awt.*; 4 import java.awt.event.WindowAdapter; 5 import java.awt.event.WindowEvent; 6 7 //布局是可以嵌套的,下面是一个小栗子 8 public class LayoutDemo { 9 public static void main(String[] args) { 10 //创建窗口 11 Frame frame = new Frame("LayoutDemo"); 12 frame.setVisible(true); 13 frame.setSize(500,400); 14 frame.setLocation(200,100); 15 frame.setBackground(Color.gray); 16 //设置窗口布局 17 frame.setLayout(new GridLayout(2,1)); 18 //创建4个panel 19 Panel panel1 = new Panel(); 20 Panel panel2 = new Panel(); 21 Panel panel3 = new Panel(); 22 Panel panel4 = new Panel(); 23 //设置panel布局 24 panel1.setLayout(new GridLayout(2,1)); 25 panel2.setLayout(new BorderLayout()); 26 panel4.setLayout(new BorderLayout()); 27 panel3.setLayout(new GridLayout(2,2)); 28 //设置panel尺寸(可省略) 29 panel2.setSize(500,200); 30 panel4.setSize(500,200); 31 panel1.setSize(400,200); 32 panel3.setSize(400,200); 33 //添加布局,从小到大,用了简便的创建按钮方式 34 panel1.add(new Button("button1")); 35 panel1.add(new Button("button2")); 36 panel2.add(new Button("button3"),BorderLayout.EAST); 37 panel2.add(new Button("button4"),BorderLayout.WEST); 38 panel2.add(panel1,BorderLayout.CENTER); 39 panel3.add(new Button("button4")); 40 panel3.add(new Button("button5")); 41 panel3.add(new Button("button6")); 42 panel3.add(new Button("button7")); 43 panel4.add(new Button("button8"),BorderLayout.EAST); 44 panel4.add(new Button("button9"),BorderLayout.WEST); 45 panel4.add(panel3,BorderLayout.CENTER); 46 47 frame.add(panel2); 48 frame.add(panel4); 49 50 //添加监听,关闭窗口 51 frame.addWindowListener(new WindowAdapter() { 52 @Override 53 public void windowClosing(WindowEvent e) { 54 System.exit(0); 55 } 56 }); 57 58 } 59 }