GUI编程

组件

窗口

弹窗

面板

文本框

列表框

按钮

图片

监听事件

鼠标

键盘监听

外挂

破解工具

简介

  • Gui的核心技术:Swing AWT

    • 因为界面不美观
    • 需要jre环境!
  • 为什么我们要学习?

    • 可以写出自己心中想要的一些小工具
    • 工作时候,也可能需要维护到swing界面,概率极小!
    • 了解MVC架构,了解监听!

AWT

Awt介绍

  • 包含了很多类和接口! GUI:图形用户界面

  • 元素:窗口,按钮,文本框

java.awt

在这里插入图片描述

组件和容器

Frame

在这里插入图片描述

  • 问题:发现窗口关闭不掉,停止java程序!
尝试回顾封装:

在这里插入图片描述

面板Panel

解决了关闭事件!

在这里插入图片描述

布局管理器

流式布局

东西南北中

表格布局

Swing

窗口、面板

public class JframeDemo {

    //init();初始化
    public void init(){
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setVisible(true);
        jf.setBounds(100,100,200,200);
        jf.setBackground(Color.cyan);

        //设置文字  JLabel
        JLabel label = new JLabel("欢迎来到JLabel");

        jf.add(label);

        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JframeDemo().init();
    }
}

标签居中

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

class MyJframe2 extends JFrame {
    public void init(){
        this.setBounds(10,10,200,300);
        this.setVisible(true);

        JLabel label = new JLabel("欢迎来到JLable");
        this.add(label);

        //文本标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //获取一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.BLUE);
    }
}

弹窗

JDialog,用来被弹出,默认就有关闭事件!

//主窗口
public class DialogDemo extends JFrame {

    public DialogDemo() {
        this.setVisible(true);
        this.setSize(700, 500);
        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.addActionListener(new ActionListener() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();
            }
        });

        container.add(button);
    }

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

//弹窗的窗口
class MyDialogDemo extends JDialog {
    public MyDialogDemo() {
        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("这是一个弹窗"));
    }
}

标签

label
new JLabel("xxx");
图标 ICON
//图标,需要实现类,Frame继承
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(20, 20);
        this.setBounds(200,200,200,200);
        //图标放在标签,也可以放在按钮
        JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);

        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;
    }
}
图片ICON
public class ImageIconDemo extends JFrame {

    public ImageIconDemo(){
        //获取图片的地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("wallhavend.png");

        ImageIcon imageIcon = new ImageIcon(url);//命名不要冲突了
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,200,200);
    }

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

面板

JPanel
public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎学习JAVA");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(200,200,500,500);
    }

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



JScrollPanel
public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container container = this.getContentPane();

        container.setLayout(new GridLayout(2,1,10,10));//后面的参数的意思是间距

        JPanel panel1 = new JPanel(new GridLayout(1,3));
        JPanel panel2 = new JPanel(new GridLayout(1,2));
        JPanel panel3 = new JPanel(new GridLayout(2,1));
        JPanel panel4 = new JPanel(new GridLayout(3,2));

        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));

        container.add(panel1);
        container.add(panel2); 
        container.add(panel3);
        container.add(panel4);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

按钮

图片按钮
public class JButtonDemo1 extends JFrame {

    public JButtonDemo1() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo1.class.getResource("wallhavend01.png");
        Icon icon = new ImageIcon(resource);

        //把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        //add
        container.add(button);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JButtonDemo1();
    }
}
单选按钮
public class JButtonDemo2 extends JFrame{
    public JButtonDemo2() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo1.class.getResource("wallhavend01.png");
        Icon icon = new ImageIcon(resource);

        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
        JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
        JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");

//        //由于单选框只能选择一个,分组
//        ButtonGroup group = new ButtonGroup();
//        group.add(jRadioButton1);
//        group.add(jRadioButton2);
//        group.add(jRadioButton3);

        container.add(jRadioButton1,BorderLayout.NORTH);
        container.add(jRadioButton2,BorderLayout.CENTER);
        container.add(jRadioButton3,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JButtonDemo2();
    }
}
复选按钮
public class JButtonDemo3 extends JFrame{
    public JButtonDemo3() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo1.class.getResource("wallhavend01.png");
        Icon icon = new ImageIcon(resource);

        //多选框
        JCheckBox checkBox1 = new JCheckBox("checkBox1");
        JCheckBox checkBox2 = new JCheckBox("checkBox2");
        JCheckBox checkBox3 = new JCheckBox("checkBox3");

        container.add(checkBox1,BorderLayout.NORTH);
        container.add(checkBox2,BorderLayout.CENTER);
        container.add(checkBox3,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

列表

下拉框
public class TestComboboxDemo1 extends JFrame {
    public TestComboboxDemo1() {
        Container container = this.getContentPane();

        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("正在上映");
        status.addItem("已下架");
        status.addItem("即将上映");

        container.add(status);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

}
列表框
public class TestComboboxDemo2 extends JFrame{
    public TestComboboxDemo2(){
        Container container = this.getContentPane();

        //生成列表的内容
//        String[] contents = {"1","2","3"};

        Vector contents = new Vector();
        //列表中需要放入内容
        JList jList = new JList(contents);

        contents.add("zhangsan");
        contents.add("lisi");
        contents.add("wangwu");

        container.add(jList);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

应用场景

  • 选择地区,或者一些单个的选项
  • 列表,展示信息,一般是动态扩容!

文本框

  • 文本框

    public class TestTextDemo1 extends JFrame {
        public TestTextDemo1(){
    
            Container container = this.getContentPane();
    
            JTextField textField1 = new JTextField("hello");
            JTextField textField2 = new JTextField("world",20);
    
            container.add(textField1,BorderLayout.NORTH);
            container.add(textField2,BorderLayout.SOUTH);
    
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500,500);
        }
    
        public static void main(String[] args) {
            new TestTextDemo1();
        }
    }
    
  • 密码框

    public class TestTextDemo2 extends JFrame {
        public TestTextDemo2(){
    
            Container container = this.getContentPane();
    
            //面板
            JPasswordField passwordField = new JPasswordField();
            passwordField.setEchoChar('*');
    
            container.add(passwordField);
    
            this.setVisible(true);
            this.setSize(500,500);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TestTextDemo2();
        }
    }
    
  • 文本域

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎学习JAVA");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(200,200,500,500);
    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值