Java的GUI

分为Swing和AWT

AWT是Swing的前身
AWT是抽象的窗口工具
在java.awt包下在这里插入图片描述

Frame弹窗

import java.awt.*;
import java.util.Scanner;
import java.awt.color.*;
import javax.swing.JFrame;
public class test {
    public static void main(String[] args) {
        Frame frame=new Frame("我的第一个第一个Java图像界面接口");//目前这个窗口在内存里面,所以需要设置可见性
        frame.setVisible(true);//设置可见性,窗口默认没有大小,即长宽,所以我们要设置一个大小
        frame.setSize(400,400);//设置窗口大小
        frame.setBackground(Color.BLACK);//设置颜色
        frame.setLocation(200,200);//设置位置
        frame.setResizable(false);//设置大小固定,true为可以改变窗口大小,false不行
        frame.setBounds(200,200,400,400);//设置窗口大小和位置
    }
}

Panel面板

不能单独存在,要放在Frame里面,可以内嵌进去,可以认为是一个空间
就是在panel的弹窗里面再内嵌一个窗口

frame.add(panel);//这样子放进去

东西之后都是放到panel上面的,而不是放到frame上,frame是唯一的

窗口监视器:实现点击x即可关闭窗口

public static void main(String[] args) {
        Frame frame = new Frame("bo");
        frame.setBackground(Color.pink);
        Panel panel = new Panel();
        frame.setLayout(null);
        frame.setBounds(200,200,400,400);
        panel.setBounds(100,100,200,200);
        panel.setBackground(Color.blue);
        frame.add(panel);
        frame.setVisible(true);
        //监听事件,监听窗口关闭事件  System.exit(0)
        //使用适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                //结束程序
                System.exit(0);
            }
        });

    }

布局管理器

流式布局

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,即按钮居中,即靠上居中,默认的水平和垂直间隙是5个单位
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));//靠左
        //流式布局为什么从第一排开始,是因为他是流式布局,所以是从一开始从第一排第一个开始布局的,然后不断递增

        //把按钮添加进去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setSize(400,400);
        frame.setLocation(300,300);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });

    }

东南西北中
在这里插入图片描述
在这里插入图片描述

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.setLocation(300, 300);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });

    }

表格布局

public static void main(String[] args) {
        Frame frame = new Frame();

        //组件---按钮
        Button botton1 = new Button("botton1");
        Button botton2 = new Button("botton2");
        Button botton3 = new Button("botton3");
        Button botton4 = new Button("botton4");
        Button botton5 = new Button("botton5");
        Button botton6 = new Button("botton6");
        frame.setLayout(new GridLayout(3,2));//3行2列
        //把按钮添加进去
        frame.add(botton1);
        frame.add(botton2);
        frame.add(botton3);
        frame.add(botton4);
        frame.add(botton5);
        frame.add(botton6);

        frame.setSize(400, 400);
        frame.setLocation(300, 300);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
        //frame.pack();//自动填充,如果你没有设置弹窗大小,可以用这个来自动填充
    }

在这里插入图片描述

习题1

public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setBounds(300,300,400,400);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));//先将大的frame分为两个
        Panel p1 = new Panel(new BorderLayout());//对于上面的frame,我们设置在东西南北中的面板
        Panel p2 = new Panel(new GridLayout(2,1));//对于上面的frame,我们设置表格布局
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2, 2));

        //上部分
        p1.add(new Button("b1"),BorderLayout.WEST);
        p1.add(new Button("b2"),BorderLayout.EAST);
        p2.add(new Button("b3"));
        p2.add(new Button("b4"));
        p1.add(p2,BorderLayout.CENTER);//放中间

        //下部分 
        p3.add(new Button("b5"),BorderLayout.WEST);
        p3.add(new Button("b6"),BorderLayout.EAST);
        p4.add(new Button("b7"));
        p4.add(new Button("b8"));
        p4.add(new Button("b9"));
        p4.add(new Button("b10"));
        p3.add(p4,BorderLayout.CENTER);
        frame.add(p1);
        frame.add(p3);//add进p1和p3即可
    }

在这里插入图片描述

事件监听

当某个事件发生的时候,要干什么,一般和按钮配合使用

public class test {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button("bot1");
        //按下按钮,触发一些事件

        //因为addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();//ActionListener即事件监听器,因为按钮是一个事件
        button.addActionListener(myActionListener);
        button.setActionCommand("abc");//设置按钮的信息,即这个按钮是什么东西,如果这一行没有定义,则走默认的值,即他的名字
        frame.add(button, BorderLayout.CENTER);
        frame.setVisible(true);
        frame.setBounds(300, 300, 400, 400);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

class MyActionListener implements ActionListener {//ActionListener是一个接口,我们要实现他
//事件监听都可以用这个
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("booo");
        System.out.println(e.getActionCommand());//获得按钮信息,并且我们让他打印输出
    }
}

输入框事件TextField监听

public class test {
    public static void main(String[] args) {
        //以后主方法只管启动,尽量一行代码
        new Myframe();
    }
}

class Myframe extends Frame {
    Myframe() {
        TextField textField = new TextField();//当行文本框,不能换行
        add(textField);//把文本框放进去

        //监听这个文本框输入的文字
        MyActionLisener myActionLisener = new MyActionLisener();
        //按下回车就会触发这个输入框的事件
        textField.addActionListener(myActionLisener);

        //设置替换编码,即用一些其他的字符来代替要在控制台输出的
        textField.setEchoChar('*');//用*来代替我们在文本框输入的内容,但是输出在控制台的仍然是你所要表达的内容
        pack();
        setVisible(true);
    }
}

class MyActionLisener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();//获得一些资源,返回一个对象,返回Object类的对象,所以我们要强转
        System.out.println(field.getText());//获得输入框的文本,并且打印到控制台
        field.setText("");//即我们回车后设置的字符串,我们想让他一回车就清空
    }
}


简易计算器,组合+内部类

组合:一个类里面写上另外一个类的成员,这样子我们就可以在这个类里面用另外一个类
oop原则:组合>继承

内部类:在类里面还有一个类,可以直接访问外部类

public class test {
    public static void main(String[] args) {
        new calu();
    }
}

//计算器类
class calu extends Frame {
    public calu() {
        //3个文本框
        TextField textField1 = new TextField(10);//最大填充字符数
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(20);
        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyActionListener(textField1,textField2,textField3));//按钮监听
        //1个标签:无含义,只是显示在Frame上给别人看
        Label label = new Label("+");

        //我们要实现的是1+2=3,即从左到右,所以是流式布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);
        pack();
        setVisible(true);
    }
}

//监听器类
class MyActionListener implements ActionListener {
    //获取3个变量
    private TextField num1, num2, num3;

    public MyActionListener(TextField num1, TextField num2, TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获取加数和被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());
        //2.将求得的和放入第三个框
        num3.setText("" + (n1 + n2));
        //清除前两个框
        num1.setText("");
        num2.setText("");
    }
}

画笔Paint

public class test {
    public static void main(String[] args) {
        new Mypaint().LoadFrame();//重写的方法会自动调用,即多态
    }
}

class Mypaint extends Frame {
    public void LoadFrame() {
        setBounds(200,200,400,400);
        setVisible(true);
    }//基本加载Frame

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawOval(100,100,200,200);//画一个空心的圆
        g.fillOval(50 ,50,50,50);//画一个实心的圆
        //养成习惯,画笔用完,将他还原为最初的颜色,即我们不写他(setcolor),所以是黑色
    }
}

鼠标监听

在这里插入图片描述

public class test {
    public static void main(String[] args) {
        new MyFrame("Draw");
    }
}

//鼠标监听类
class MyFrame extends Frame {
    //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点,因为我们鼠标画画本质上是进行点的描绘
    ArrayList points;

    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();//集合迭代器,即取每个元素
        while (iterator.hasNext()) {
            Point point = (Point) iterator.next();//给他强转为点的元素,即我们获取了这个点,同时指针不断往后移动
            g.setColor(Color.black);
            g.fillOval(point.x, point.y, 10, 10);//我们获取了当前鼠标的位置,我们要画一个点,这里我们用实心圆来模拟一个点,因为我们设置的长宽很少,所以相当于一个点
        }
    }

    public MyFrame(String title) {
        super(title);//给frame类传递名字,即调用父类的构造方法
        setBounds(200, 200, 400, 400);
        //存鼠标点击的点,用集合
        points = new ArrayList<>();

        //鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouseListener());//直接调用这个类即可

        setVisible(true);
    }//构造方法

    //添加一个点到界面上,即把MyMouseListener的点添加到paint里面
    public void addPaint(Point point) {
        points.add(point);
    }

    private class MyMouseListener extends MouseAdapter {
        //鼠标按下,弹起,按住不放
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            MyFrame myFrame = (MyFrame) e.getSource();//强转为MyFrame
            //这个点就是鼠标的点
            myFrame.addPaint(new Point(e.getX(), e.getY()));//加进去
            //每次点击鼠标都需要重画一次
            myFrame.repaint();//相当于每次都刷新一次页面,即每次重新执行paint的方法

        }//点击,我们在点击的时候就要给他画一个东西
    }
}


窗口监听

class bo {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame {
    public WindowFrame() {
        setVisible(true);
        pack();
        addWindowListener(new MyWindowListener());
    }

    //写一个内部类比较方便
    class MyWindowListener extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false);//隐藏窗口
            System.exit(0);//正常关闭
        }

        @Override
        public void windowActivated(WindowEvent e) {
            System.out.println("窗口激活");//即,若我们窗口本来没有被点击,我们鼠标点击窗口,会输出这个
        }
    }
}

键盘监听

class bo {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame {
    public KeyFrame() {
        setBounds(100, 100, 300, 300);
        setVisible(true);
        addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获取你当前按下了哪个键
                int keyCode = e.getKeyCode();//不需要记录这个数值,直接使用静态属性  VK_xxx
                if(keyCode==KeyEvent.VK_1) {//如果为1
                    System.out.println("你按下了1");
                }
            }
        });
    }
}

JFrame

class bo {
    //先有一个初始化方法,这样子使主方法就一行
    public void Init() {
        JFrame jFrame = new JFrame();
        jFrame.setVisible(true);
        jFrame.setBounds(100,200,300,300);

        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//JFrame的默认关闭,即退出

        //要把他放在容器内,因为在jframe中,我们不能直接设置颜色,容器里面的颜色才是他真是的颜色
        jFrame.getContentPane().setBackground(Color.red);//先获得一个容器,然后在容器内设置背景
        //我们对JFrame的操作都是在容器内的
        JLabel jLabel = new JLabel("xyb");
        jFrame.getContentPane().add(jLabel);

        //让我们的文本居中
        jLabel.setHorizontalAlignment(0);//0为居中
    }

    public static void main(String[] args) {
        new bo().Init();
    }
}

弹窗Dialog

//因为弹窗其实也是一个新的窗口,他不是一个莫名其妙的窗口

//主窗口
public class test extends JFrame {
    public test() {
        this.setVisible(true);
        this.setSize(600,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame 放东西,容器
        Container contentPane = this.getContentPane();
        //绝对布局
        contentPane.setLayout(null);//该怎么摆就怎么摆

        //按钮
        JButton jButton = new JButton("点击弹出对话框");
        jButton.setBounds(100,100,200,50);//直接用绝对布局,直接放在对应的位置

        //点击按钮,弹出弹窗,所以要一个监听事件
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDiglog();
            }
        });

        //加到容器,他就会自动定位
        contentPane.add(jButton);

    }

    public static void main(String[] args) {
        new test();
    }
}

//弹窗窗口,默认可以关闭窗口,不需要自己再写
class MyDiglog extends JDialog {
    public MyDiglog() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);

        Container contentPane = this.getContentPane();//获取容器
        contentPane.setLayout(null);
        Label label = new Label("ybb");
        label.setBounds(100,100,200,200);
        contentPane.add(label);
    }
}

Icon图标

可以放在Label里面的,还可以给插入图片

//图标需要实现Icon,还要继承类Frame
public class test extends JFrame implements Icon {
    private int width, heitht;

    public test() {
        //获取图片的地址
        //根据这个类获取类目录下的信息
        URL url = test.class.getResource("1.jpg.png");//即获取当前这个类的同级资源
        JLabel image = new JLabel("Image");
        ImageIcon imageIcon = new ImageIcon(url);
        image.setIcon(imageIcon);//放置图片
        image.setHorizontalAlignment(SwingConstants.CENTER);//居中
        Container contentPane = getContentPane();
        contentPane.add(image);
        setVisible(true);
        setBounds(300,300,800,800);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public test(int width, int heitht) {
        this.width = width;
        this.heitht = heitht;
    }

    public void init() {
        /*test test = new test(50, 50);
        //图标放在标签上,也可以放在按钮上
        JLabel lable = new JLabel("icon", test, SwingConstants.CENTER);//放在中间
        Container contentPane = getContentPane();//获取容器
        contentPane.add(lable);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);*/
        new test();
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x, y, width, heitht);//画一个实心圆
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.heitht;
    }

    public static void main(String[] args) {
        new test().init();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值