GUI编程学习

GUI编程

GUI的核心技术:Swing和AWT

不流行的原因:

  • 界面不美观
  • 需要jre环境!

AWT

介绍

  1. 包含了很多类和接口,GUI。
  2. 元素:窗口,按钮,文本框
  3. java.awt

组件与容器

JFrame
package com.yaohua;

import javax.swing.*;
import java.awt.*;

public class Demo01 {
    public static void main(String[] args) {
        //JFrame
        JFrame frame=new JFrame("第一个窗口练习");
        //设置窗体大小
        frame.setSize(400,500);
        //设置大小固定
        frame.setResizable(false);
        //设置背景颜色
        frame.getContentPane().setBackground(new Color(82, 81, 81));
        //设置可见性
        frame.setVisible(true);
        //设置窗口居中
        frame.setLocationRelativeTo(null);
        //设置点击关闭按钮,程序退出,窗口关闭
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

注意:直接调用setBackground(Color.red)这个方法后,你的确设置了JFrame的背景颜色,而你看到的却不是直接的JFrame,而是JFrame.getContentPane()。而JFrame上的contentPane默认是Color.WHITE的,所以,无论你对JFrame怎么设置背景颜色,你看到的都只是contentPane。

解决办法:三个

  1. 在完成初始化,调用getC ontentPane()方法得到一个contentPane容器,然后将其设置为不可见,即setVisible(false)。这样,你就可以看到JFrame的庐山真面貌啦!核心代码:this.getContentPane().setVisible(false)
  2. 将contentPane的颜色设置为你想要的颜色,而不是对JFrame本身设置,核心代码:this.getContentPane().setBackground(Color.red);
  3. 为JFrame添加一个Panel或者JLabel等其他组件,设置其颜色为你想要的颜色,然后将其覆盖JFrame窗口即可。
Panel
  1. 创建MyFrame,封装窗体构建细节
package com.yaohua;

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {
    public MyFrame(String title, int w, int h, Color color){
        super(title);
        super.setSize(w,h);
        setResizable(false);
        getContentPane().setBackground(color);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(null);
    }
}

  1. 创建panel对象,并将其加入MyFrame中
package com.yaohua;

import java.awt.*;

public class Demo02 {
    public static void main(String[] args) {
        MyFrame myFrame=new MyFrame("第一个窗口",400,500,new Color(88, 138, 189));
        //创建面板
        Panel panel=new Panel();
        //设置坐标,相对于myFrame
        panel.setBounds(5,5,385,200);
        //设置背景颜色
        panel.setBackground(Color.RED);

        myFrame.add(panel);
    }
}
布局管理器
  • 流式布局
package com.yaohua;

import javax.swing.*;
import java.awt.*;

public class TestFlowLayout {
    public static void main(String[] args) {
        JFrame frame=new JFrame("流式布局");
        JButton button=new JButton("开始");
        JButton button2=new JButton("结束");
        JButton button3=new JButton("快进");
        frame.setLocationRelativeTo(null);

        //frame.setLayout(new FlowLayout());
        //frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        frame.add(button);
        frame.add(button2);
        frame.add(button3);


        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(400,300);
        frame.setVisible(true);
    }
}
  • 东西南北中
package com.yaohua;

import javax.swing.*;
import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        JFrame frame=new JFrame("东西南北中");
        frame.setLayout(new BorderLayout());
        frame.setSize(400,300);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JButton button=new JButton("东邪");
        JButton button2=new JButton("西毒");
        JButton button3=new JButton("中神通");
        JButton button4=new JButton("南帝");
        JButton button5=new JButton("北丐");


        frame.add(button,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.add(button3,BorderLayout.CENTER);
        frame.add(button4,BorderLayout.SOUTH);
        frame.add(button5,BorderLayout.NORTH);

        frame.setVisible(true);
    }
}
  • 表格布局
package com.yaohua;

import javax.swing.*;
import java.awt.*;

public class TestGridLayout {
    public static void main(String[] args) {
        JFrame frame=new JFrame("表格布局");
        frame.setLayout(new GridLayout(3,2));
        frame.setSize(400,300);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JButton bt1=new JButton("我本楚狂人");
        JButton bt2=new JButton("凤歌笑孔丘");
        JButton bt3=new JButton("天上一片月");
        JButton bt4=new JButton("家乡百十猿");
        JButton bt5=new JButton("至今思项羽");
        JButton bt6=new JButton("不肯过江东");

        frame.add(bt1);
        frame.add(bt2);
        frame.add(bt3);
        frame.add(bt4);
        frame.add(bt5);
        frame.add(bt6);

        frame.setVisible(true);
    }
}
事件监听
package com.yaohua;

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {
    public MyFrame(String title, int w, int h, Color color){
        super(title);
        super.setSize(w,h);
        setResizable(false);
        getContentPane().setBackground(color);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(null);
    }
}

package com.yaohua;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestActionEvent {
    public static void main(String[] args) {
        MyFrame frame=new MyFrame("事件监听",400,500, Color.BLUE);
        JButton button=new JButton("开始");
        button.setBounds(10,10,60,30);
        button.addActionListener(e -> System.out.println("点击按钮~"));

        frame.add(button);

    }
}
输入框事件监听
package com.yaohua;

import javax.swing.*;
import java.awt.*;


public class TextFiled {
    public static void main(String[] args) {
        MyFrame frame=new MyFrame("文本框",300,200, Color.WHITE);
        JTextField textField=new JTextField();
        textField.setText("朱子萌");
        textField.setBounds(5,5,50,20);
        textField.addActionListener(e -> System.out.println(e.getActionCommand()));
        frame.add(textField);
    }
}

画笔
package com.yaohua;

import javax.swing.*;
import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
       MyPaint myPaint= new MyPaint();
       myPaint.loadFrame();
    }
}

class MyPaint extends JFrame{

    public void loadFrame(){
       setSize(600,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.RED);
        g.drawOval(50,50,100,100);
        g.setColor(Color.GREEN);
        g.drawRect(150,100,100,100);
    }
}
鼠标监听
package com.yaohua;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouseListener {
    public static void main(String[] args) {
        new MyMouseFrame("画图");
    }
}

class MyMouseFrame extends JFrame{
    //存鼠标点击的点
    ArrayList points;
    public MyMouseFrame(String title){
        super(title);
        setSize(600,400);
        //存点的集合
        points=new ArrayList<>();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());

        setVisible(true);
    }



    @Override
    public void paint(Graphics g) {
        Iterator iterator = points.iterator();
        //迭代对集合里的坐标进行绘制
        while (iterator.hasNext()){
            Point point= (Point) iterator.next();
            g.setColor(Color.RED);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    //适配器模式
    private class MyMouseListener extends MouseAdapter{
        //鼠标按下事件
        @Override
        public void mousePressed(MouseEvent e) {
            MyMouseFrame frame = (MyMouseFrame) e.getSource();
            frame.points.add(new Point(e.getX(),e.getY()));
            //每次点击鼠标都需要重画一次
            frame.repaint();
        }
    }
}

窗口监听
package com.yaohua;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindowListener {
    public static void main(String[] args) {
        new MyWindowFrame("窗口事件");
    }
}

class MyWindowFrame extends JFrame{
    public MyWindowFrame(String title){
        super(title);
        setSize(600,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("窗口被激活了!");
            }
        });

        setVisible(true);

    }

}
键盘监听
package com.yaohua;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame("键盘");
    }
}
class KeyFrame extends JFrame{
    public KeyFrame(String title){
        super(title);
        setSize(600,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode=e.getKeyCode();
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("你按了上键!");
                }
            }
        });
        setVisible(true);
    }
}

贪吃蛇

帧:如果时间片足够小,就是动画,一秒30帧,60帧,连起来就是动画,拆开就是静态的图片!

键盘监听

定时器,Timer

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值