向上摆放组件,保留了GridBagLayout的很多优点,但是没有那么复杂
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet implements ActionListener{
JButton bt1 = new JButton("North");
JButton bt2 = new JButton("West");
JButton bt3 = new JButton("East");
JButton bt4 = new JButton("South");
JLabel lb1 = new JLabel("Center");
Container cp = getContentPane();
public void init()
{
cp.setLayout(new BorderLayout());
cp.add("North", bt1);
cp.add("West", bt2);
bt2.addActionListener(this);
cp.add("East", bt3);
cp.add("South", bt4);
bt4.addActionListener(this);
cp.add("Center", lb1);
}
public void actionPerformed(ActionEvent e)
{
lb1.setText("Please Press a Button");
if(e.getSource() == bt2)
lb1.setText("Press West");
if(e.getSource() == bt4)
lb1.setText("Press South");
}
}
AWT基本组件有
P386
ButtonCanvasCheckboxCheckboxGroupChoiceFrameLabelListPanelScrollbarScrollPaneTextAreaTextField
Java事件模型的流程
Event Source:事件发生的场所,通常是各个组件,例如按钮,窗口,菜单等Event:封装了GUI组件上发生的特定事情(通常是一次用户操作),程序员要获得GUI组件上所发生事件的相关信息,都通过Event对象来取得。EventListener:负责监听事件源所发生的的事件,并对其作出响应处理
AWT菜单
MenuBar:菜单条,菜单的容器Menu:菜单项的容器,也可以作为菜单项使用PopupMenu:上下文菜单组件MenuItem:菜单项组件CheckboxMenuItem:复选框菜单项组件MenuShortcut:菜单快捷键组件
画图
Component类里提供了和绘图有关的三个方法
paint(Graphics g):绘制组件的外观update(Graphics g):调用paint()方法,刷新组件外观repaint():调用update()方法,刷新组件外观
Graphics类
Graphics是一个抽象的画笔对象,可以在组件上绘制丰富多彩的几何图形和位图。
Swing
Swing是一种轻量级组件,100%java实现,不再依赖本地平台的图形界面,对跨平台的支持更加出色。
Swing提供了比AWT更多的的图形界面组件
Swing组件都采用了MVC(Modle-View-Controller)的设计模式,可以实现GUI组件的显示逻辑和数据逻辑的分离,提供更多的灵活性。
大部分Swing组件都是JComponent抽象类的子类,JComponent类是AWT里Container类的子类,所以Swing组件都可以作为容器使用。
Swing为除了Canvas之外的所有AWT组件提供了相应的实现
很多Swing组件可以使用图标修饰自己
支持插拔式的的外观风格。每个JComponent对象都有一个相应的ComponentUI对象,为它完成所有的绘画,事件处理,决定尺寸大小等工作。
支持设置边框
JFrame
A Frame is a top-level window with a title and a border
A fraem, implemented as an instance of the JFrame class, is a window that typically has decorations such as a border, a title and buttons for closing and iconifying the window.
它的setDefaultCloseOperation操作可设置用户发起“close”时的默认操作,选项有
DO_NOTHING_ON_CLOSEHIDE_ON_CLOSEDISPOSE_ON_CLOSEEXIT_ON_CLOSE
默认情况下,被设置为 HIDE_ON_CLOSE
JOptionPane
通过JOptionPane可以非常方便地创建一些简单的对话框,JOptionPane提供了如下4个方法来创建对话框。
showMessageDialog/showInternalMessageDialog:消息对话框showConfirmDialog/showInternalMessageDialog:确认对话框showInputDialog/showInternalInputDialog:输入对话框showOptionDialog/showInternalOptionDialog:自定义选项对话框
线程
创建线程的方法
定义Thread类的子类,并重写run()方法。通过调用start()方法来启动多线程实现Runnable接口创建线程类,并以此实例作为Thread的target来创建Thread对象,调用Thread对象的start()方法来启动该线程。创建Callable接口的实现类,实现call()方法,该方法有返回值,使用FutureTask类来包装Callable对象,使用FutureTask对象作为Thread对象的target创建线程对象,并启动新线程。使用FutureTask对象的get()方法来获得子线程执行结束后的返回值。
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class Test implements Callable
{
public Integer call()
{
int i=0;
for(; i<10; i++)
{
System.out.println(Thread.currentThread().getName() + ", i = " + i);
}
return i;
}
public static void main(String[] args)
{
Test t = new Test();
FutureTask
task = new FutureTask
(t); for(int i=0; i<10; i++) { System.out.println(Thread.currentThread().getName() + ", i = " + i); if(i == 2) { new Thread(task, "Thread with return value").start(); } } try { System.out.println("Sub thread's return value: "+task.get()); } catch(Exception e) { e.printStackTrace(); } } }
线程的生命周期
新建状态,当程序使用new关键字创建了一个线程之后,该线程就出于新建状态。就绪状态,当线程对象调用了start()方法之后,线程就出于就绪状态。至于线程合适开始运行,取决于JVM里线程调度器的调度运行状态,出于就绪状态的线程获得了CPU,开始执行run()方法,就处于运行状态阻塞状态,在采用抢占式调度策