(一)基础知识介绍
1.1AWT介绍
- 包含了很多接口和类,GUI:图形用户界面
- 元素:窗口,按钮,文本框
- java.awt包
- AWT中有一些重要的类:Component(组件):button,TextArea(文本域),lablel(标签)等。
- Contain(容器),可以将组件通过add()加入
- 容器下又包含Window和Panel(面板)
(二) 组件与容器
2.1 Frame窗体
package GuiTeast;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("我的窗口");
frame.setVisible(true);
frame.setSize(400,400);
frame.setBackground(new Color(0, 239, 254));
//初始位置
frame.setLocation(200,200);
//设置窗口不可拉伸,大小固定
frame.setResizable(false);
}
}
- 运行结果:
- 问题:窗口无法关闭
- 解决:
//监听窗口关闭事件
//适配器模式adapter,选中适合的那个WindowAdapter中的windowClosing方法
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//退出
System.exit(0);
}
});
- 封装类`
package GuiTeast;
import java.awt.*;
public class TestFrame2 {
//展示多个窗口
public static void main(String[] args) {
Frame frame = new Frame();
MyFrame myFrame1= new MyFrame(100,100,200,200,Color.red);
MyFrame myFrame2= new MyFrame(300,100,200,200,Color.yellow);
MyFrame myFrame3= new MyFrame(100,300,200,200,Color.green);
MyFrame myFrame4= new MyFrame(300,300,200,200,Color.black);
}
}
//封装
class MyFrame extends Frame{
static int id=0;//可能存在多个窗口,设置一个计数器
//继承后要重写构造器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
setBounds(x,y,w,h);
setVisible(true);
setBackground(color);
}
}
- 运行结果:
2.2 Panel面板
- panel不可以单独存在,是在容器中的。
package GuiTeast;
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();
//设置布局
frame.setLayout(null);
//窗体坐标设置,面板panel放内部
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(0, 239, 254));
//panel设置坐标,相对Frame位置
panel.setBounds(50,50,300,300);
panel.setBackground(Color.green);
//将面板放进Frame
frame.add(panel);
frame.setVisible(true);
//监听窗口关闭事件
//适配器模式adapter,选中适合的那个WindowAdapter中的windowClosing方法
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//退出
System.exit(0);
}
});
}
}
- 运行结果:
-
2.3布局管理器
- 流式布局:从左到右
package GuiTeast;
import java.awt.*;
import java.awt.event.WindowAdapter;
//流式布局
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());
//靠左开始排
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,200);
frame.setVisible(true);//设置可见
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
//设置弹窗关闭,适配器模式,对于参数,需要什么new什么。
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//窗口关闭需要结束程序
System.exit(0);
}
});
}
}
- 东南西北中
(可以通过嵌套来设置更美观)
package GuiTeast;
import com.sun.media.sound.SF2SoundbankReader;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
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.setVisible(true);
frame.setSize(400,400);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
- 表格布局
package GuiTeast;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button button1 = new Button("btn1");
Button button2 = new Button("btn2");
Button button3 = new Button("btn3");
Button button4 = new Button("btn4");
Button button5 = new Button("btn5");
Button button6 = new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.setVisible(true);
frame.pack();//java函数,自动选择最佳布局
frame.setSize(400,400);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
2.4事件监听
- 事件监听:当某事情发生时干什么?
package Demo13;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ActionEvent {
//按下按钮,触发事件
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
MyActionListener myActionListener = new MyActionListener();//(2)
button.addActionListener(myActionListener);//(3)
//参数需要一个接口,因为addActionListener需要一个ActionListener,所以我们需要构造一个ActionListener
frame.add(button,BorderLayout.CENTER);
frame.setVisible(true);
//(5)直接调用方法windowClose方法
windowClose(frame);
}
//写一个关闭窗口的事件(4)
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener{
//(1)鼠标放在 ActionListener并按住alt+ins选中implement method
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
}
}
- 两个(多个)按钮共用一个监听器
package Demo13;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ActionEvent02 {
public static void main(String[] args) {
//两个按钮实现同一个监听
//开始 停止
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();//自适应
frame.setVisible(true);
frame.setSize(400,400);
//2可以显示定义触发事件后显示的东西,不显示定义会默认
button1.setActionCommand("Button-start");
button2.setActionCommand("button-stop");
//3
MyMonitor myMonitor = new MyMonitor();
//4
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
windowClose(frame);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//1
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//触发事件后的操作
System.out.println("按钮被点击了:"+e.getActionCommand());
}
}
2.5输入框 TextField 监听
package Demo13;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
//main()中一般只写启动
new MyFrame();
}
}
class MyFrame extends Frame{
//构造器
public MyFrame(){
TextField textField = new TextField();
add(textField);//由于已经继承了Frame所以不用写成frame.add()
setVisible(true);
pack();
//监听此文本框输入的文字
MyActionListener02 myActionListener02 = new MyActionListener02();
textField.addActionListener(myActionListener02);
//设置替换编码,类似于密码输入只显示*,但是回车后,控制面板显示的是具体输入内容
textField.setEchoChar('*');
}
}
class MyActionListener02 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field=(TextField) e.getSource();//获取一些资源,返回一个对象
System.out.println(field.getText());//获取输入框的文本field.getText()并输出
field.setText("");//回车后变为空,类似于发消息
}
}
2.6鼠标监听`
package Demo15;
//实现鼠标监听
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
public class Test_mouseLi {
public static void main(String[] args) {
new MyFrame("drawing");
}
}
class MyFrame extends Frame{
ArrayList points;//全局
public MyFrame(String title) {
super(title);
setBounds(200,200,600,600);
this.setVisible(true);
//存鼠标的点
points=new ArrayList<>();
this.addWindowListener(new WindowAdapter() {//由于知识new而没真正给名字,为匿名 内部类
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//添加鼠标监听,相对于窗口
this.addMouseListener(new MyMouseListener());//参数需要一个MouseListener,由于他是各接口所以在下面定义内部类并实现接口
}
@Override
public void paint(Graphics g) {
//迭代器
Iterator iterator=points.iterator();
while (iterator.hasNext()){
Point point=(Point)iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x,point.y,10,10);
}
}
//添加点到界面
public void addPaint(Point point){
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame =(MyFrame) e.getSource();
//这里点击时就会产生一个点
//这个点就是鼠标的点
myFrame.addPaint( new Point(e.getX(),e.getY()));
repaint();
}
}
}
2.7键盘监听
package Demo15;
import java.awt.*;
import java.awt.event.*;
public class keyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame() {
setBounds(300,300,400,400);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
int keyCode=e.getKeyCode();
if (keyCode==KeyEvent.VK_UP){//VK_xxxx表示每个案件静态属性
System.out.println("你按下了上键");
}
}
});
}
}
(三) Swing
3.1窗口JFrame与面板JPanel
- Swing可以画图,下拉列表等
- 使用Swing要创建容器Cantainer,AWT中不用
package Demo15;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init(){
//顶级窗口
JFrame frame = new JFrame("这是一个JFrame窗口");
frame.setVisible(true);
frame.setBounds(100,100,400,400);
//容器的概念Container
Container contentPane = frame.getContentPane();
contentPane.setBounds(100,100,200,200);
contentPane.setBackground(Color.blue);
//关闭事件,JFrame不用写监听事件,直接用默认default的关闭操作
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
- 标签居中
package Demo15;
import javax.swing.*;
import java.awt.*;
public class MyJframe2 {
public static void main(String[] args) {
MyJframe myJframe = new MyJframe();
myJframe.init();
}
}
class MyJframe extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(100,100,500,500);
JLabel jLabel = new JLabel("欢迎来到JFrame");
this.add(jLabel);
//文本标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//获得容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.yellow);
}
}
3.2弹窗JDialog
- 游戏开始界面可以写一个游戏弹窗,”游戏规则“,点击后出现弹窗介绍游戏规则
package Demo15;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public void DialogDemo(){
this.setVisible(true);
this.setSize(800,800);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame放东西,需要容器
Container container = this.getContentPane();
//绝对布局,写死
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出对话框");
button.setBounds(30,30,200,50);//绝对定位布局
button.setVisible(true);
//点击按钮时候,弹出弹窗,监听事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDiolog();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDiolog extends JDialog {
public MyDiolog() {
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//写弹窗可以不用加,默认有
//容器
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("游戏规则"));
}
}
3.3标签
- Icon:图标
package Demo15;
import javax.swing.*;
import java.awt.*;
//图标Icon是一个接口
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
//如果存在有参构造,必须要有无参构造
public IconDemo() {}
//有参构造
public IconDemo(int width,int height){
this.width=width;
this.height=height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15,15);
//图标Icon放在标签上,按钮上等
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
//容器,存组件
Container container=getContentPane();
container.add(label);
this.setBounds(300,300,600,600);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
//画图标
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
- 把图片作为图标:图片标签
package Demo15;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIcon1 extends JFrame {
public ImageIcon1(){
JLabel label = new JLabel("ImageIcon");
//1.获取图片地址,同级目录可通过此方式
URL url = ImageIcon1.class.getResource("0.png");
//2.图片与图标绑定=图标2
ImageIcon imageIcon = new ImageIcon(url);
//3.将图标2加到label
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
//4.创建容器并添加label
Container container=getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,600,600);
}
public static void main(String[] args) {
new ImageIcon1();
}
}
3.4面板
- JPanel面板
package Demo16;
import javax.swing.*;
import java.awt.*;
public class JPanel01 extends JFrame {
public JPanel01(){
//创建container
Container container=this.getContentPane();
container.setLayout(new GridLayout(2,2,10,10));//Gridlayout是表格布局,后面两个参数是面板间距
//向container内部装东西
JPanel jPanel = new JPanel(new GridLayout(1,3));
JPanel jPanel2 = new JPanel(new GridLayout(1,2));
JPanel jPanel3= new JPanel(new GridLayout(2,1));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
container.add(jPanel);
container.add(jPanel2);
container.add(jPanel3);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new JPanel01();
}
}
- JScroll:滚动条
package Demo16;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100,100,500,500);
//1
Container container=this.getContentPane();
//2文本域,此可换行
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("你好");
//3Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
//4加入
container.add(jScrollPane);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5按钮
- 将图片放按钮上实现图片按钮
package Demo15;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container container=this.getContentPane();
//图片变图标
URL resource = JButtonDemo01.class.getResource("0.png");
Icon Icon = new ImageIcon(resource);
//图标放在按钮
JButton button = new JButton();
button.setIcon(Icon);
button.setToolTipText("图片按钮");//鼠标放在按钮有提示
container.add(button);
this.setVisible(true);
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
- 单选框
- 多选框
3.6列表
3.7游戏
- 帧:如果时间片足够小,就是动画,一秒30帧,拆开就是静态图
- 键盘监听
- 定时器Timer类,以毫秒为单位,1000毫秒=1秒,实现ActionListener接口并实现actionPerformed方法
- 定时器用法 Timer timer=new Timer(100,this);//100毫秒刷新一次
- 在构造器调用timer.start();
- 4步:1.定义数据,布尔类型isstart,isfail2.画上去,画笔中画图,或字3.监听事件,(键盘,事件)
- 在init()中写好初始数据,在键盘监听中if判断如果游戏结束,或死亡调用init()初始化数据
- 失败判定:在事件监听中写if(…){isfail=true;}
- 积分:1.定义int score;2.在init()中定义score=0;3.画积分g.setColor(Color red);g.setFont(参数);g.drawString(“分数”+score,坐标X,Y)4.监听事件