AWT
GUI(Graphical
User Interface),图形用户界面。
AWT(Abstract Window Toolkit),抽象窗口工具集,第一代的java GUI组件,是重量
级的
Swing,不依赖于底层细节,轻量级的组件。
Window有两种形式,一种是Frame ,一种是Dialog
Frame是一个顶级窗口,缺省为BorderLayout布局管理器
Panel无法单独显示,必须添加到某个容器中,缺省为FlowLayout布局管理器。
Frame的使用
public class FrameTest { public static void main(String[] args) { Frame frame = new Frame(); frame.setSize(500,500); frame.setBackground(Color.GRAY); frame.setTitle("AWT first App"); frame.setVisible(true); } } |
带容器窗口的实现
import java.awt.Color; import java.awt.Frame; import java.awt.Panel; public class FrameWithPanel extends Frame { public FrameWithPanel(String title) { super(title); } public static void main(String[] args) { FrameWithPanel frame = new FrameWithPanel("Frame with panel"); Panel panel = new Panel(); frame.setSize(200, 200); frame.setBackground(Color.BLACK); frame.setLayout(null); panel.setSize(100 , 100); panel.setBackground(Color.YELLOW); frame.add(panel); frame.setVisible(true); } } |
带按钮的窗口
public class ExGui extends Frame { private Frame frame; private Button btn1; private Button btn2; public void go() { frame = new Frame("gui example"); frame.setLayout(new FlowLayout()); btn1 = new Button("Press me"); btn2 = new Button("Don't Press me"); frame.add(btn1); frame.add(btn2); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { ExGui ex = new ExGui(); ex.go(); } } |
布局管理器
BorederLayout
FlowLayout
GridLayout
CardLayout
GridBagLayout
FlowLayout的使用,流式布置,按钮的位置会随窗口的大小改变
public class MyFlow { private Frame frame; private Button btn1,btn2,btn3; public void go() { frame = new Frame("Flow Layout"); frame.setLayout(new FlowLayout()); btn1 = new Button("btn1"); btn2 = new Button("btn2"); btn3 = new Button("btn3"); frame.add(btn1); frame.add(btn2); frame.add(btn3); frame.setSize(100,100); frame.setVisible(true); } public static void main(String[] args) { MyFlow flow = new MyFlow(); flow.go(); } } |
BorderLayout的使用,固定按钮到某一面上(东,南,西,北,中)
public class ExGui2 extends Frame { private Frame frame; private Button bn,bs,bw,be,bc; public void go() { frame = new Frame("Border Layout"); bn = new Button("north"); bs = new Button("south"); bw = new Button("west"); be = new Button("east"); bc = new Button("center"); frame.add(bn, BorderLayout.NORTH); frame.add(bs, BorderLayout.SOUTH); frame.add(bw, BorderLayout.WEST); frame.add(be, BorderLayout.EAST); frame.add(bc, BorderLayout.CENTER); frame.setSize(200,200); frame.setVisible(true); } public static void main(String[] args) { ExGui2 ex = new ExGui2(); ex.go(); } } |
GridLayout的使用,用表格对按钮进行定位。会忽略组件的大小,使它们有相同的大小
public class GridEx { private Frame frame; private Button b1,b2,b3,b4,b5,b6; public void go() { frame = new Frame("Grid Layout"); frame.setLayout(new GridLayout(3,2)); b1 = new Button("1"); b2 = new Button("2"); b3 = new Button("3"); b4 = new Button("4"); b5 = new Button("5"); b6 = new Button("6"); frame.add(b1); frame.add(b2); frame.add(b3); frame.add(b4); frame.add(b5); frame.add(b6); frame.setSize(200,200); frame.setVisible(true); } public static void main(String[] args) { GridEx ex = new GridEx(); ex.go(); } } |
CardLayout的使用
package com.anllin.awt; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class CardLayoutTest { public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setVisible(true); } } class MyFrame extends JFrame { JPanel Pleft = new JPanel(); JPanel Pright = new JPanel(); CardLayout car; public MyFrame() { this.setBounds(80, 60, 600, 300); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Container c = this.getContentPane(); c.setLayout(new BorderLayout()); Pleft.setBackground(Color.red); Pright.setBackground(Color.blue); c.add(Pleft, BorderLayout.CENTER); c.add(Pright, BorderLayout.EAST); Pright.setLayout(new FlowLayout()); JButton[] b = new JButton[10]; for (int i = 0; i < 10; i++) { b[i] = new JButton("第" + i + "个"); Pleft.add(b[i]); } car = new CardLayout(); Pleft.setLayout(car); JButton b1 = new JButton("next"); class MyEvent implements ActionListener { public void actionPerformed(ActionEvent
e) { car.next(Pleft); } } b1.addActionListener(new MyEvent()); Pright.add(b1); } } |
GridBagLayout的使用
综合使用
public class ExGui3 { private Frame frame; private Panel panel; private Button b1, b2, b3, b4; public void go() { frame = new Frame("Complex Layout"); b1 = new Button("west"); b2 = new Button("hello"); frame.add(b1,BorderLayout.WEST); frame.add(b2,BorderLayout.CENTER); panel = new Panel(); b3 = new Button("world"); b4 = new Button("welcome"); panel.add(b3); panel.add(b4); frame.add(panel,BorderLayout.NORTH); frame.setSize(200,200); frame.setVisible(true); } public static void main(String[] args) { ExGui3 ex = new ExGui3(); ex.go(); } } |
AWT事件模型
事件—描述发生了什么的对象。(比如ActionEvent)
事件源—事件的产生器(比如Button)
事件处理器—接收事件、解释事件并处理用户交互的方法(比如ActionHandler)
事件监听器—实现了监听器接口的类。一个监听器对象是一个实现了专门的监听器的类的实例。(ActionListeneràActonPerformed())
委托模型
优点:
--事件不会被意外地处理
--有可能创建并使用适配器(adapter)类对事件动作进行分类。
--委托模型有利于把工作分布到各个类中。
Public class TestButton { public static void main(String[] args) { Frame frame = new Frame("Test Button"); Button button = new Button("Press me"); button.addActionListener(new
ButtonHandler()); frame.add(button,BorderLayout.CENTER); frame.setSize(200, 200); frame.pack(); frame.setVisible(true); } } class ButtonHandler implements ActionListener { @Override public void actionPerformed(ActionEvent
e) { String label = e.getActionCommand(); System.out.println(label); } } |
几类具有典型代表意义的事件:
MouseEvent—鼠标的相关事件,如鼠标移动等
WindowEvent—窗口的相关事件,如窗口关闭等
ActionEvent—组件本身的相关事件,如被点击等。
public class TwoListen implements MouseMotionListener,MouseListener { private Frame frame; private TextField textField; public void go() { frame = new Frame("Two Listeners Example"); frame.add(new Label("click"),BorderLayout.NORTH); textField = new TextField(30); frame.add(textField,BorderLayout.SOUTH); frame.addMouseMotionListener(this); frame.addMouseListener(this); frame.addMouseListener(new
MyMouseListener()); frame.addWindowListener(new WindowHandle()); frame.setSize(300,200); frame.setVisible(true); } public static void main(String[] args) { TwoListen listen = new TwoListen(); listen.go(); } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub String str = "The mouse has left the frame"; } @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub String str = "x:" + e.getX() + ",y:" + e.getY(); this.textField.setText(str); } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } } class MyMouseListener implements MouseListener { @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub String str = "The Mouse has entered the Frame"; System.out.println(str); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub String str = "The Mouse has Exited the Frame"; System.out.println(str); } } class WindowHandle implements WindowListener { @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.exit(0); } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent
e) { // TODO Auto-generated method stub } @Override public void
windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent
e) { // TODO Auto-generated method stub } @Override public void
windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } } |
事件适配器(Adapter)
--让你只对感兴趣的事件进行监听。
public class TwoAdapter extends WindowAdapter { private Frame frame; private TextField textField; public void go() { frame = new Frame("Two Listeners Example"); frame.add(new Label("click"),BorderLayout.NORTH); textField = new TextField(30); frame.add(textField,BorderLayout.SOUTH); frame.addMouseListener(new MyAdapter()); frame.setSize(300,200); frame.setVisible(true); } public static void main(String[] args) { TwoListen listen = new TwoListen(); listen.go(); } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.exit(0); } } class MyAdapter extends MouseAdapter { @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub String str = "The Mouse has entered the Frame"; System.out.println(str); } @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub String str = "x:" + e.getX() + ",y:" + e.getY(); System.out.println(str); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub String str = "The Mouse has Exited the Frame"; System.out.println(str); } } |
MouseAdapter的底层实现
它实现了MouseListener,
MouseWheelListener, MouseMotionListener接口,但是是对接口的空实现,这样当我们去继承它时,就可以只对我们感兴趣的方法进行重写。
public abstract class MouseAdapter implements MouseListener,
MouseWheelListener, MouseMotionListener { /** * {@inheritDoc} */ public void mouseClicked(MouseEvent e)
{} /** * {@inheritDoc} */ public void mousePressed(MouseEvent e)
{} /** * {@inheritDoc} */ public void mouseReleased(MouseEvent e)
{} /** * {@inheritDoc} */ public void mouseEntered(MouseEvent e)
{} /** * {@inheritDoc} */ public void mouseExited(MouseEvent e) {} /** * {@inheritDoc} * @since 1.6 */ public void
mouseWheelMoved(MouseWheelEvent e){} /** * {@inheritDoc} * @since 1.6 */ public void mouseDragged(MouseEvent e){} /** * {@inheritDoc} * @since 1.6 */ public void mouseMoved(MouseEvent e){} } |