寒假笔记07

GUI

Frame
在这里插入代码片package GUI;

import java.awt.*;

// GUI第一个界面
public class TestFrame {

    public static void main(String[] args) {
        // frame 的源码
        Frame frame = new Frame("第一个窗口");

        //需要设置可见属性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(Color.BLACK);
        //弹出位置
        frame.setLocation(500,500);
        // 设置固定的位置
        frame.setResizable(false);
    }

}

Panel
package GUI;

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

public class TestPanel {
    public static void main(String[] args) {
        Frame MyFrame = new Frame();

        Panel panel = new Panel();
        MyFrame.setLayout(null);

        //Frame
        MyFrame.setBounds(300,300,500,500);
        MyFrame.setBackground(new Color(1,1,1));
        //pamel
        panel.setBounds(50,50,300,300);
        panel.setBackground(new Color(2,25,255));

        MyFrame.add(panel);

        MyFrame.setVisible(true);
        //  监听事件 监听窗口关闭事件
        // 适配器模式
        MyFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

东西南北中

		Button north = new Button("north");
        Button south = new Button("south");
        Button west = new Button ("west");
        Button east = new Button("east");
        Button center = new Button("center");

        frame.add(east,BorderLayout.EAST);
        frame.add(north,BorderLayout.NORTH);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(center,BorderLayout.CENTER);

流式布局

		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);
网格布局
// 网格布局
        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");
        
        frame.setLayout(new GridLayout(3,2));
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

事件监听

public class Text01 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button("btn1");
        Button button1= new Button("btn2");

        //
        button1.setActionCommand("button1");

        // 监听
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        button1.addActionListener(myActionListener);
        frame.add(button,BorderLayout.NORTH);
        frame.add(button1,BorderLayout.SOUTH);
        windowClose(frame);
        frame.setVisible(true);
        frame.pack();
    }

    // 关闭事件
    private static  void windowClose (Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("加油"+ e.getActionCommand());
    }
}

文本框监听
public class Text02 {
    public static void main(String[] args) {
        MyFrame01 frame =new MyFrame01();
        windowClose(frame);
    }
    private static  void windowClose (Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyFrame01 extends Frame{
    public  MyFrame01() {
        TextField textField = new TextField();
        add(textField);

        // 监听文本框
        MyActionListener myActionListener = new MyActionListener();
        // 回车结束
        textField.addActionListener(myActionListener);
        // 设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
    private static  void windowClose (Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class   MyActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();
        System.out.println(field.getText());
        field.setText("");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值