AWT
窗口(Frame)
单Frame窗口
package com.zheng.yi01;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("第一个窗口");
//窗口弹出的位置(位置:Location)
frame.setLocation(200,200);
//设置窗口大小(大小:Size)
frame.setSize(400,400);
//设置背景颜色(背景:Background)
frame.setBackground(new Color(85,150,68));
//窗口大小固定(调整大小:Resizable)
frame.setResizable(false);
//窗口可见(可见的:Visible)
frame.setVisible(true);
}
}
运行:
多Frame窗口
package com.zheng.yi01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.YELLOW);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.MAGENTA);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.RED);
}
}
class MyFrame extends Frame {
//多窗口计数器
static int id = 0;
public MyFrame(int x,int y,int w,int h,Color color){
//调用父类构造器
super("MyFrame "+id);
//设置窗口大小弹出的位置(范围:Bounds)
setBounds(x, y, w, h);
setBackground(color);
setVisible(true);
}
}
运行:
内嵌Panel面板
package com.zheng.yi01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局(布局:Layout)
frame.setLayout(null);
frame.setBounds(300, 300, 600, 600);
frame.setBackground(new Color(85,150,68));
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(105,39,0));
//panel内嵌在frame里面
frame.add(panel);
frame.setVisible(true);
//解决窗口点击X无法关闭问题
//(监听者:Listener) 监听窗口关闭事件:System.exit(0)
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
运行:
布局管理器(Layout)
流式布局(流式布局:FlowLayout)
package com.zheng.yi01;
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
//设置按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//按钮左中右分布
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setSize(400,400);
frame.setVisible(true);
}
}
运行:
边界布局(边界布局:BorderLayout)
package com.zheng.yi01;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
//按钮东西南北中分布
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}
}
运行:
表格网状布局(表格网状布局:GridLayout)
package com.zheng.yi01;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
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");
Button button6 = new Button("button6");
//二行三列
frame.setLayout(new GridLayout(2,3));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
//Java函数,自动最优布局
frame.pack();
frame.setSize(400,400);
frame.setVisible(true);
}
}
运行:
边界加网状布局练习
package com.zheng.yi01;
import java.awt.*;
public class Demo01awt {
public static void main(String[] args) {
//主面板
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
//四个面板
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2,2));
//上面左右中间夹着上下
panel1.add(new Button("East-1"),BorderLayout.EAST);
panel1.add(new Button("West-1"),BorderLayout.WEST);
//夹着的上下
panel2.add(new Button("p1_s"));
panel2.add(new Button("p1_x"));
panel1.add(panel2,BorderLayout.CENTER);
//下面左右中间夹着田字
panel3.add(new Button("East-2"),BorderLayout.EAST);
panel3.add(new Button("West-2"),BorderLayout.WEST);
//中间的田字
for (int i = 0; i < 4; i++) {
panel4.add(new Button("tian"));
}
panel3.add(panel4,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
frame.setLocation(200,200);
frame.setSize(400,400);
frame.setBackground(new Color(85,150,68));
frame.setVisible(true);
}
}
运行:
监听事件(EventListener)
动作监听(ActionListener)
package com.zheng.yi02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionListener {
public static void main(String[] args) {
Frame frame = new Frame("开始");
Button button1 = new Button("start");
Button button2 = new Button("stop");
button1.setActionCommand("button1-nb");
button2.setActionCommand("button2-nb");
button1.addActionListener(new MyActionListener());
button2.addActionListener(new MyActionListener());
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了" + e.getActionCommand());
System.out.println("多个按钮共享一个事件");
}
}
文本窗输字(文本框TextField)
package com.zheng.yi02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestTestField {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//设置替换编码
textField.setEchoChar('*');
textField.addActionListener(new MyActionListener3());
pack();
setVisible(true);
}
}
class MyActionListener3 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//.getSource()获得一些资源,如键盘输入
TextField field = (TextField) e.getSource();
//获得输入框中的文本
System.out.println(field.getText());
//向输入框中输入文本
field.setText(" ");
}
}
文本窗涂画(画笔:paint)
package com.zheng.yi03;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyFrame().ffMyFrame();
}
}
class MyFrame extends Frame{
public void ffMyFrame(){
setBounds(300,300,600,600);
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
//圆
g.drawOval(100,100,100,100);
//实心圆
g.fillOval(100,100,100,100);
}
}
鼠标监听(MouseListener)
package com.zheng.yi03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame2("画图变画点");
}
}
class MyFrame2 extends Frame{
ArrayList array;
public MyFrame2(String title){
super(title);
setBounds(300,300,500,500);
//存鼠标的点击的点
array = new ArrayList<>();
//鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
//监听鼠标事件
Iterator iterator = array.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x,point.y,100,100);
}
}
//集合里面存放监听到的点
public void addPaint(Point point){
array.add(point);
}
private class MyMouseListener extends MouseAdapter{
@Override
//鼠标点击
public void mouseReleased(MouseEvent e) {
//super.mouseReleased(e);
MyFrame2 myFrame2 = (MyFrame2) e.getSource();
//窗口调用存了点之后的集合方法,输入点x,y
myFrame2.addPaint(new Point(e.getX(),e.getY()));
//画笔只会画一次所以每次点击鼠标都需要重新画一次
myFrame2.repaint();
}
}
}
窗口监听(WindowListener)
package com.zheng.yi03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowListener {
public static void main(String[] args) {
new MyFrame3();
}
}
class MyFrame3 extends Frame{
public MyFrame3() {
setBackground(Color.cyan);
setBounds(300,300,500,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
//关闭窗口
public void windowClosing(WindowEvent e) {
System.out.println("关闭Closing");
System.exit(0);
}
@Override
//鼠标移开窗口隐藏
public void windowActivated(WindowEvent e) {
MyFrame3 myFrame3 = (MyFrame3) e.getSource();
myFrame3.setTitle("窗口激活了");
System.out.println("激活Activated");
}
});
}
}
键盘监听(KeyListener)
package com.zheng.yi03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new MyFrame4();
}
}
class MyFrame4 extends Frame{
public MyFrame4(){
setBounds(200,200,500,500);
setVisible(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
}
@Override
public void keyPressed(KeyEvent e) {
//获取键盘敲打
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}else if (keyCode==KeyEvent.VK_DOWN){
System.out.println("你按下了下键");
}else if (keyCode==KeyEvent.VK_LEFT){
System.out.println("你按下了左键");
}else if (keyCode==KeyEvent.VK_RIGHT){
System.out.println("你按下了右键");
}
}
});
}
}