/*
* @(#)SampleAWT.java 1.00 17/06/27
*
* Copyright (c) 2017 Jacky.Dai
* Address:.......
* All rights reserved.
*
* Your
* NOTES
* HERE
*/
//package test;
//import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* Class description goes here.
*
* @version 1.00 27 June 2017
* @author Jacky_Dai
*/
public class SampleAWT implements MouseListener, MouseMotionListener
{
/** mainFrame - This is the main frame for user */
private Frame mainFrame;
/**
* titleLabel - thi is a sampele label
* to display the title
*/
private Label titleLabel;
/** testButton - sample Button for test */
private Button testButton;
/** userTextField - sample TextField for test */
private TextField userTextField;
private final static int TEXT_FIELD_SIZE = 40;
public void DisplayUI()
{
mainFrame = new Frame("SampleAWT main frame");
// 加上窗口监听器,其中主要实现了退出功能
mainFrame.addWindowListener(new SampleWindowListener());
// 加一个标签
titleLabel = new Label("Titile - This is a label.");
mainFrame.add(titleLabel, BorderLayout.NORTH);
// 加一个按钮
testButton = new Button("Test Button");
mainFrame.add(testButton, BorderLayout.WEST);
// 设置按钮事件监听器,按钮按下时会向控制台输出信息
//testButton.addActionListener(new MyButtonListener());
// 加一个文本编辑框
userTextField = new TextField(TEXT_FIELD_SIZE);
mainFrame.add(userTextField, BorderLayout.SOUTH);
// 加上鼠标动作监听器,因为类本身实现了这两个接口,所以参数是this
mainFrame.addMouseListener(this);
mainFrame.addMouseMotionListener(this);
// 可以添加多个监听器,所以还可以定义其他的,如MyMouseListener类,然后加上
// 设置刚好能容纳这些控件的尺寸
mainFrame.pack();
// 设置为可见
mainFrame.setVisible(true);
}
public static void main(String[] args)
{
SampleAWT window = new SampleAWT();
window.DisplayUI();
}
//
//鼠标监听器MouseListener
//监听鼠标事件MouseEvent。
//相应事件和处理方法
//鼠标事件 处理方法
//MOUSE_CLICKED MouseClicked (MouseEvent) 鼠标点击(单或双)
//MOUSE_PRESSED MousePressed (MouseEvent) 鼠标按下
//MOUSE_RELEASED MouseReleased(MouseEvent) 鼠标松开
//MOUSE_ENTERED MouseEntered (MouseEvent) 鼠标进入(某组件区域)
//MOUSE_EXITED MouseExited (MouseEvent) 鼠标离开(某组件区域)
//
//Handle Events of MouseListener
@Override
public void mouseClicked(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e)
{
}
@Override
public void mouseExited(MouseEvent e)
{
this.userTextField.setText("the mouse has exited.");
}
@Override
public void mousePressed(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent e)
{
}
//鼠标监听器MouseMotionListener
//对于鼠标的移动和拖放,另外用鼠标运动监听器MouseMotionListener。
//因为许多程序不需要监听鼠标运动,把两者分开可简化程序。
//相应事件和处理方法
//鼠标事件 处理方法
//MOUSE_MOVED MouseMoved (MouseEvent) 鼠标在移动
//MOUSE_DRAGGED MouseDragged(MouseEvent) 鼠标被拖动
//
//Handle Events of s MouseMotionListener
@Override
public void mouseDragged(MouseEvent e)
{
}
@Override
public void mouseMoved(MouseEvent e)
{
String str = "x = " + e.getX() + ", y = " + e.getY();
this.userTextField.setText(str);
System.out.println(str);
}
}
class SampleWindowListener implements WindowListener
{
@Override
public void windowActivated(WindowEvent e)
{
//当窗口从非激活到激活状态时监视器调用该方法
}
@Override
public void windowClosed(WindowEvent e)
{
//当窗口关闭时
}
@Override
public void windowClosing(WindowEvent e)
{
//窗口正被关闭
System.out.println("windowClosing");
// 退出
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent e)
{
//激活到非激活状态
}
@Override
public void windowDeiconified(WindowEvent e)
{
//当窗口撤销图标化时
}
@Override
public void windowIconified(WindowEvent e)
{
//当窗口图标化时
}
@Override
public void windowOpened(WindowEvent e)
{
//当窗口打开时
}
}
javac -encoding UTF-8 SampleAWT.java
jar cvf SampleAWT .jar SampleAWT.class SampleWindowListener.class
修改Jar包 META-INF\MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.8.0_131 (Oracle Corporation)
Main-Class: SampleAWT
jar SampleAWT
AWT的布局管理器
AWT中主要有四种布局管理器:FlowLayout、GridLayout、BorderLayout和CardLayout。
下面给出这四种布局管理器的源码:
package com.guan.visualTest.frameTest;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class YourFrame extends Frame{
private static final long serialVersionUID = 1L;
Panel borderLayoutPanel;
Panel cardLayoutPanel;
Panel flowLayoutPanel;
Panel gridLayoutPanel;
private void generateGridLayoutPanel() {
gridLayoutPanel = new Panel();
gridLayoutPanel.setLayout(new GridLayout(2,2));
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
gridLayoutPanel.add(button1);
gridLayoutPanel.add(button2);
gridLayoutPanel.add(button3);
gridLayoutPanel.add(button4);
}
private void generateFlowLayoutPanel() {
flowLayoutPanel = new Panel();
flowLayoutPanel.setLayout(new FlowLayout());
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((Button)e.getSource()).setLabel("welcome ");
}
});
flowLayoutPanel.add(button1);
flowLayoutPanel.add(button2);
flowLayoutPanel.add(button3);
flowLayoutPanel.add(button4);
flowLayoutPanel.add(button5);
}
private void generateBorderLayoutPanel() {
borderLayoutPanel = new Panel();
borderLayoutPanel.setLayout(new BorderLayout());
Button button1 = new Button("South");
Button button2 = new Button("West");
Button button3 = new Button("East");
Button button4 = new Button("North");
Button button5 = new Button("Center");
borderLayoutPanel.add(button1,BorderLayout.SOUTH);
borderLayoutPanel.add(button2,BorderLayout.WEST);
borderLayoutPanel.add(button3,BorderLayout.EAST);
borderLayoutPanel.add(button4,BorderLayout.NORTH);
borderLayoutPanel.add(button5,BorderLayout.CENTER);
}
private void genrateCardLayoutPanel() {
cardLayoutPanel = new Panel();
final CardLayout cl = new CardLayout();
cardLayoutPanel.setLayout(cl);
Button button1 = new Button("black");
Button button2 = new Button("red");
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.next(cardLayoutPanel);
}
};
button1.addActionListener(al);
button2.addActionListener(al);
cardLayoutPanel.add(button1,"1");
cardLayoutPanel.add(button2,"2");
}
public YourFrame(String panelName) {
super("panelName");
generateBorderLayoutPanel();
generateFlowLayoutPanel();
generateGridLayoutPanel();
genrateCardLayoutPanel();
setLayout(new GridLayout(2,2));
add(borderLayoutPanel);
add(flowLayoutPanel);
add(gridLayoutPanel);
add(cardLayoutPanel);
setSize(800, 800);
setLocation(100,100);
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
public static void main(String[] args) {
YourFrame yourFrame = new YourFrame("welcome");
yourFrame.setVisible(true);
}
}
[img]http://s13.sinaimg.cn/middle/616e189f49664ea241c1c&690?_=5720116[/img]