GUI编程之AWT
Frame
写一个属于自己的第一个窗口
package com.frame.myFirstFrame;
import java.awt.*;
public class FirstFrame01 {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个窗口!"); //构造器里是窗口title
frame.setVisible(true); //窗口可视化
frame.setSize(200,200); //窗口大小
frame.setBackground(Color.GRAY);//窗口背景颜色
frame.setLocation(100,100);//窗口位置
frame.setResizable(false);//窗口是否可调节大写
}
}
封装实现多窗口
package com.frame.myFirstFrame;
import java.awt.*;
public class Frame02 extends Frame { //继承Frame
static int count = 0;
public Frame02(int w, int h, int x, int y, Color color){ //有参构造器里面去封装
super("我的第"+(++count)); //重写父类的构造器
setBackground(color); //调用父类方法,不用加类名
setResizable(false);
setBounds(x,y,w,h);//这个方法直接包含长宽高
setVisible(true);
}
}
////////////////////////////////////////
package com.frame.myFirstFrame;
import java.awt.*;
public class Application {
public static void main(String[] args) {
new Frame02(200,200,100,100, Color.black);
new Frame02(200,200,300,100, Color.white);
new Frame02(200,200,100,300, Color.yellow);
new Frame02(200,200,300,300, Color.blue);
}
}
Panel
基于上面的窗口,加入面板
package com.frame.myFirstFrame;
import java.awt.*;
public class Application {
public static void main(String[] args) {
Frame02 f1 = new Frame02(200, 200, 100, 100, Color.black);
Frame02 f2 = new Frame02(200, 200, 300, 100, Color.white);
Frame02 f3 = new Frame02(200, 200, 100, 300, Color.yellow);
Frame02 f4 = new Frame02(200, 200, 300, 300, Color.blue);
Panel panel = new Panel();
panel.setBackground(new Color(158, 160, 156));
panel.setBounds(20,100,60,60);
f1.add(panel);//.add()放入的是一个component panel是component的子类,所以可以放进去
}
}
//添加监听,可以关闭窗口
//适配器模式
f1.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//结束程序
}
});
布局管理器
- 流式布局
- 东西南北中
- 表格布局
frame.setLayout(new FlowLayout());//设置为流式布局
//流式布局三种:CENTER LEFT RIGHT
//默认为CENTER
//这样就设置成左了
frame.setLayout(new Flowlayout(FlowLayout.LEFT));
frame.add(east,BorderLayout.East);//把east按钮放在东边
//这里因为不会自动填充,所以需要每个按钮去设置他的布局
frame.setLayout(new GridLayout(3,2));//表格布局,三行两列
做一个作业:设置为如下布局:
package com.frame.myFirstFrame;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FlowLayout {
public static void main(String[] args) {
Frame frame = new Frame("作业布局");
frame.setVisible(true);
frame.setBounds(100,100,500,500);
frame.setBackground(new Color(26, 124, 124));
frame.setLayout(new GridLayout(2,1));//先分成两行一列,放面板
//四个面板
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new BorderLayout());
Panel p3 = new Panel(new GridLayout(2,1));
Panel p4 = new Panel(new GridLayout(2,2));
///上面的处理
p1.add(new Button("p1-east"),BorderLayout.EAST);
p1.add(new Button("p1-west"),BorderLayout.WEST);
p3.add(new Button("p1-p3-center1"));
p3.add(new Button("p1-p3-center2"));
p1.add(p3,BorderLayout.CENTER);
///下面的处理
p2.add(new Button("p2-east"),BorderLayout.EAST);
p2.add(new Button("p2-west"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
p4.add(new Button("p2-p4-center"+(++i)));
}
p2.add(p4);
frame.add(p1);
frame.add(p2);
//监听 关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
运行结果:
事件监听
package com.frame.myFirstFrame;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Application {
public static void main(String[] args) {
Frame frame = new Frame("start-stop");
frame.setBounds(100,100,500,500);
frame.setVisible(true);
Button b1 = new Button("start");
Button b2 = new Button("stop");
//b1.setActionCommand("1");
//b2.setActionCommand("2");
//这两行是setActionCommand
frame.setLayout(new GridLayout(2,1));
frame.add(b1);
frame.add(b2);
MonitorAction monitorAction = new MonitorAction();
b1.addActionListener(monitorAction);//要用addActionListener就必须要去实现接口ActionListener的方法
b2.addActionListener(monitorAction);
}
}
//////////////
package com.frame.myFirstFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MonitorAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();//如果没有setActionCommand,则e中存放的是Button的label
if (actionCommand.equals("start")){
System.out.println("监听开始!");
}
if (actionCommand.equals("stop")){
System.exit(0);
}
}
}
输入框事件监听
输入框:TextField
输入框中按下enter 就会触发这个输入框事件
package com.frame.myFirstFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyTextField {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame() throws HeadlessException {
setBackground(Color.GRAY);
setVisible(true);
TextField textField = new TextField();
Panel p1 = new Panel();
add(p1);
p1.add(textField);
MyTextMontor myTextMontor = new MyTextMontor();
textField.addActionListener(myTextMontor);
//设置为替换编码,密码输入常用
textField.setEchoChar('*');
}
}
class MyTextMontor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField getSource = (TextField) e.getSource();//e.getSource是获得一些资源,返回对象 这里强转为TxetField类方便下一步利用TextField的方法
System.out.println(getSource.getText());//.getText()是TextField的方法,可以得到文本框中的内容并Return为String类,然后传送到后台
}
}
一个简易的计算器
实现在文本框填写两个数字,点击 = 即可得到答案并且清空文本框
最初代码:
package com.frame.myFirstFrame;
import java.awt.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator {
public static void main(String[] args) {
new MyCalculator();
}
}
class MyCalculator extends Frame{
public MyCalculator() {
setVisible(true);
setBounds(100,100,300,300);
pack();
//文本框
TextField num1 = new TextField(10);//columns 文本框的长度
TextField num2 = new TextField(10);
TextField num3 = new TextField(11);
//标签类 用来放 +
Label label = new Label("+");
//按钮
Button button = new Button("=");
//把上面放进去
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
//写监听
MyCalcMontor myCalcMontor = new MyCalcMontor(num1, num2,num3);
button.addActionListener(myCalcMontor);
}
}
class MyCalcMontor implements ActionListener{
public TextField num0;
public TextField num1;
public TextField num3;
public MyCalcMontor(TextField num1, TextField num2, TextField num3) {
this.num0 = num1;
this.num1 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(this.num0.getText());//使用Integer的parseInt方法,将String转换为int
int b = Integer.parseInt(this.num1.getText());
num3.setText(""+(a+b)); //这里个人理解为什么在这里可以影响到框内的内容:原因是因为TextField这类的赋值,会把权限也赋予出去,也就是对那个框的权限赋予给了this.num0 this.num1 this.num3
num0.setText("");
num1.setText("");
}
}
优化代码,利用组合的方法:
package com.frame.myFirstFrame;
import java.awt.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalDemo01 {
public static void main(String[] args) {
new MyNewCal().LoadFrame();
}
}
class MyNewCal extends Frame {
TextField num1,num2,num3; //TextField是一个class 使用就要new
public void LoadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
setVisible(true);
setBounds(100,100,400,400);
pack();
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
MyNewMontor myNewMontor = new MyNewMontor(this);//把类丢进监控器类里面用
button.addActionListener(myNewMontor);
}
}
class MyNewMontor implements ActionListener {
MyNewCal myNewCal = null;//组合
public MyNewMontor(MyNewCal myNewCal) { //把类丢进来,就可以直接用这个类的所有东西
this.myNewCal = myNewCal;
}
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(myNewCal.num1.getText());
int b = Integer.parseInt((myNewCal.num2.getText()));
myNewCal.num3.setText(""+(a+b));
myNewCal.num1.setText("");
myNewCal.num2.setText("");
}
}
继续优化,利用内部类:
package com.frame.myFirstFrame;
import java.awt.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalDemo01 {
public static void main(String[] args) {
new MyNewCal().LoadFrame();
}
}
class MyNewCal extends Frame {
TextField num1,num2,num3;
public void LoadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
setVisible(true);
setBounds(100,100,400,400);
pack();
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
button.addActionListener(new MyNewMontor());
}
private class MyNewMontor implements ActionListener { //内部类,可以直接调用外部类所有东西,不用再组合,也不用构造器传参
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(num1.getText());
int b = Integer.parseInt((num2.getText()));
num3.setText(""+(a+b));
num1.setText("");
num2.setText("");
}
}
}
画笔
package com.frame.myFirstFrame;
import java.awt.*;
public class Paint {
public static void main(String[] args) {
new MyPaint().LoadFrame();//注意这里new的是LoadFrame
}
}
class MyPaint extends Frame{
public void LoadFrame(){ //这里要用这个方法才能让弹窗活下来
setVisible(true);
setBounds(100,100,400,400);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillOval(100,100,200,200);
}
}
鼠标监听
package com.frame.myFrame;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;
public class TestMouse {
public static void main(String[] args) {
new MyFrame("画图").LoadFrame();
}
}
class MyFrame extends Frame {
ArrayList points;//一个集合,用于存放所有的鼠标点的点
public MyFrame(String tittle) {
super(tittle);
points = new ArrayList(); //!!!!注意,这里要把实例化放在构造器里面,这样这个类就都可以用了!
}
public void LoadFrame(){
setVisible(true);
setBounds(100,100,400,400);
this.addMouseListener(new MyMouseListener());
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();//!!!注意,这里迭代器一定要指向下一个,也就是.next()必须要写!
g.fillOval(point.x,point.y,10,10);
}
}
public class MyMouseListener extends MouseAdapter { //MouseAdapter是一个适配器,帮我们完成了一些方法
@Override
public void mousePressed(MouseEvent e) { //这里的e存放了所有监听到的鼠标的内容
//MyFrame frame = (MyFrame) e.getSource(); **这句话暂时不知道有什么意义,去掉不影响**
addPoint(new Point(e.getPoint())); //存放到points集合 !!内部类可以直接调用外部类所有的东西
repaint();
}
}
public void addPoint(Point point) {
this.points.add(point);
}
}
血的教训!!!
一定要注意实例化。要用一个类,那么这个类必须要实例化!把实例化放在构造器中,相当于放在类的属性里面,就在骨子里面完成了实例化!
窗口监听
package com.frame;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WinListener {
public static void main(String[] args) {
new MyWindowListener().LoadFrame();
}
}
class MyWindowListener extends Frame {
public MyWindowListener() {
}
public void LoadFrame(){
setBackground(Color.cyan);
setBounds(100,100,400,400);
setVisible(true);
this.addWindowListener(new WindowAdapter() { //匿名内部类,更加方便简洁
@Override
public void windowClosing(WindowEvent e) { //关闭窗口监听
System.out.println("你关闭了这个窗口!");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {//焦点监听
MyWindowListener myWindowListener = (MyWindowListener) e.getSource();//e.getSource是获取资源,这里我们获取了窗口的所有资源,才能够改变窗口的title
myWindowListener.setTitle("你回来了!");
}
@Override
public void windowDeactivated(WindowEvent e) {//非焦点监听
MyWindowListener myWindowListener = (MyWindowListener) e.getSource();
myWindowListener.setTitle("你离开我了");
}
});
}
}
键盘监听
大同小异,同上!