java基础16 GUI编程(Swing和AWT)

一、组件

GUI编程(图形 用户 界面编程)
窗口
弹窗
面板
文本框
列表框
按钮
图片
监听事件
鼠标
键盘事件

二、简介

Gui的核心技术:Swing AWT
因为界面不美观
需要jre环境

可以写出自己心目中的小工具

三、AWT(抽象的窗口工具)

包含了很多类和接口
元素:
窗口,按钮,文本框
组件存放在容器中
在这里插入图片描述

四、Frame

package chap2;

import java.awt.*;

public class Demo1 {
    public static void main(String[] args) {
        Frame frame = new Frame("java界面窗口");

//        需要设置可见性
        frame.setVisible(true);
//      设置窗口大小
        frame.setSize(200,500);
//        设置背景颜色
        frame.setBackground(new Color(25, 25, 203));
//        弹出的初始位置
        frame.setLocation(600,600);
//        设置大小固定
        frame.setResizable(false);
        
    }
}

五、Panel

package chap2;

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

public class Demo2 {
//    Panel可以看做是一个空间,但是不能单独存在
    public static void main(String[] args) {
        Frame frame=new Frame("Myframe");
//        布局的概念
        Panel panel=new Panel();
//        设置布局
        frame.setLayout(null);
//        坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(32, 194, 129));
//      设置相对于frame的坐标
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(173, 171, 25));
        frame.add(panel);

        frame.setVisible(true);

//        监听事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

六、事件监听

package chap2;

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 Demo3 {
    public static void main(String[] args) {
        Frame frame=new Frame("开始-停止");
        Button button1=new Button("start");
        Button button2=new Button("stop");
        button2.setActionCommand("button2-stop");
        Mymonitor mymonitor=new Mymonitor();

        button1.addActionListener(mymonitor);
        button2.addActionListener(mymonitor);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

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

        frame.pack();
        frame.setVisible(true);


    }
}

class Mymonitor implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("msg:"+e.getActionCommand());
    }
}

七、 文本框监听

package chap2;

import sun.awt.WindowClosingListener;

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 Demo4 {
    public static void main(String[] args) {
        new MyFrame();
    }


}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);

        textField.addActionListener(new MyActionListener());
//        设置编码
        textField.setEchoChar('*');
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        pack();
        setVisible(true);
    }
}
class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField= (TextField) e.getSource();
        String text = textField.getText();
        System.out.println(text);
    }
}

八、Paint

package chap2;

import java.awt.*;

public class Demo5 {
    public static void main(String[] args) {
        new MyPaint().loadPaint();
    }
}
class MyPaint extends Frame{
    public void loadPaint(){
        setBounds(200,200,600,500);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
            g.setColor(Color.red);
            g.drawOval(100,100,100,100);
            g.fillOval(50,50,50,50);
    }
}

九、鼠标监听

package chap2;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;

public class Demo6 {
    public static void main(String[] args) {
        new MyFrame1("画图");
    }
}
class MyFrame1 extends Frame{
    ArrayList ponits;

    public MyFrame1(String title){
        super(title);
        setBounds(200,200,400,300);

        ponits=new ArrayList();

        this.addMouseListener(new MyMouseListener());
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setVisible(true);

    }

    @Override
    public void paint(Graphics g) {
        Iterator iterator=ponits.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    public void addPoint(Point point){
        ponits.add(point);
    }


    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame1 myFrame1 = (MyFrame1) e.getSource();

            addPoint(new Point(e.getX(),e.getY()));
            myFrame1.repaint();
        }
    }


}

十、窗口监听

package chap2;

import sun.awt.WindowClosingListener;

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

public class Demo7 {
    public static void main(String[] args) {
        new Myframetwo();
    }
}
class Myframetwo extends Frame{
    public Myframetwo(){
        setBackground(Color.GRAY);
        setBounds(200,200,400,300);
        setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);
            }

            @Override
            public void windowActivated(WindowEvent e) {
                setBackground(Color.yellow);
            }
        });
    }
}

十一、键盘监听

package chap2;

import javafx.animation.KeyFrame;

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

public class Demo8 {
    public static void main(String[] args) {
        new KeyFrame1();
    }
}

class KeyFrame1 extends Frame{
    public KeyFrame1(){
        setBackground(Color.GRAY);
        setBounds(200,200,400,300);
        setVisible(true);
        this.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                int keycode=e.getKeyCode();
                if (keycode==KeyEvent.VK_UP){
                    setBackground(Color.BLUE);
                }else {
                    setVisible(false);
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
    }
}

十二、Jframe

package chap2;

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

public class Demo9 {

    public static void main(String[] args) {
        new Demo9().init();
    }
    public void init(){
        JFrame frame=new JFrame();
        frame.setBounds(300,300,400,500);
        frame.setVisible(true);
//        设置文字Jlabel
        JLabel label=new JLabel("hello");

        label.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(label);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}

十三、JDialog弹窗

package chap2;

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

public class Demo10 {
    public static void main(String[] args) {
        new DiaTest();
    }

}
class DiaTest extends JFrame {
    public DiaTest(){
        setVisible(true);
        setSize(400,500);

//        Jframe容器 存放东西
        Container container=this.getContentPane();
//      绝对布局
        container.setLayout(null);

        JButton button=new JButton("点击弹出一个对话框");
        button.setBounds(100,100,200,50);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });
        container.add(button);

    }
}
class MyDialog extends JDialog{

    public MyDialog() {
        this.setVisible(true);
        this.setBounds(100,100,200,200);

        Container container = this.getContentPane();
        container.setLayout(null);

        container.add(new JLabel("学习java"));

    }
}

十四、Icon

package chap2;

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

// 图标是个弹窗 需要实现类 jFrame继承
public class Demo11 extends JFrame implements Icon {
    private int width;
    private int height;
    public static void main(String[] args) {
        new Demo11().init();

    }

    public Demo11(int width, int height){
        this.width = width;
        this.height = height;
    }

    public Demo11(){
    }

    public void init(){
        Demo11 icon=new Demo11(15,15);
        JLabel icontest = new JLabel("icontest", icon, SwingConstants.CENTER);

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

//        this.setBounds(100,100,300,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);



    }

    @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 chap2;

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

public class Demo12 extends JFrame{


    public Demo12(){
        JLabel label=new JLabel("ImageIcon");
        URL url=Demo12.class.getResource("tx.jpg");
        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);
    }

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

十五、面板

package chap2;

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

public class JpanelDemo extends JFrame {

    public JpanelDemo(){
        Container container=this.getContentPane();

        container.setLayout(new GridLayout(2,1,10,10));

        JPanel jPanel1 = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(1,2));
        JPanel jPanel3 = new JPanel(new GridLayout(2,1));
        JPanel jPanel4 = new JPanel(new GridLayout(3,2));

        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("1"));
        jPanel2.add(new JButton("1"));
        jPanel2.add(new JButton("1"));
        jPanel2.add(new JButton("1"));
        jPanel3.add(new JButton("1"));
        jPanel3.add(new JButton("1"));
        jPanel3.add(new JButton("1"));
        jPanel4.add(new JButton("1"));
        jPanel4.add(new JButton("1"));
        jPanel4.add(new JButton("1"));

        this.setVisible(true);

        container.add(jPanel1);
        container.add(jPanel2);
        container.add(jPanel3);
        container.add(jPanel4);

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

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

十六、单选框

package chap2;

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

public class JbuttonDemo02 extends JFrame {

    public static void main(String[] args) {
        new JbuttonDemo02();
    }
    public JbuttonDemo02(){
        Container container=this.getContentPane();
        URL url=JbuttonDemo02.class.getResource("tx.jpg");
        Icon icon=new ImageIcon(url);

        JRadioButton radioButton1=new JRadioButton("JRadioButton01");
        JRadioButton radioButton2=new JRadioButton("JRadioButton02");
        JRadioButton radioButton3=new JRadioButton("JRadioButton03");

        ButtonGroup buttonGroup=new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        buttonGroup.add(radioButton3);

        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.SOUTH);

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


    }
}

十七、复选框

package chap2;

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

public class JbuttonDemo03 extends JFrame{
    public static void main(String[] args) {
        new JbuttonDemo03();
    }
    public JbuttonDemo03(){
        Container container=this.getContentPane();
        URL url=JbuttonDemo02.class.getResource("tx.jpg");
        Icon icon=new ImageIcon(url);

        JCheckBox checkBox1=new JCheckBox("checkbox1");
        JCheckBox checkBox2=new JCheckBox("checkbox2");

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

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


    }
}

十八、下拉框

package chap2;

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

public class TestComboboxDemo extends JFrame {
    public static void main(String[] args) {
        new TestComboboxDemo();
    }
    public TestComboboxDemo(){
        Container container=this.getContentPane();
        JComboBox jComboBox=new JComboBox();
        jComboBox.addItem(null);
        jComboBox.addItem("1");
        jComboBox.addItem("2");
        jComboBox.addItem("3");

        container.add(jComboBox);

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


    }
}

十九、滚动文本框

package chap2;

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

public class JscrollDemo extends JFrame {
    public static void main(String[] args) {
        new JscrollDemo();
    }
    public JscrollDemo(){
        Container container=this.getContentPane();
        JTextArea jTextArea=new JTextArea(20,50);
        jTextArea.setText("Hello");

        JScrollPane jScrollPane=new JScrollPane(jTextArea);
        container.add(jScrollPane);

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

二十、密码文本框

ackage chap2;

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

public class JPasswordFieldDemo extends JFrame {
    public static void main(String[] args) {
        new JPasswordFieldDemo();
    }

    public JPasswordFieldDemo(){
        Container container=this.getContentPane();

        JPasswordField jPasswordField=new JPasswordField();
        jPasswordField.setEchoChar('*');

        container.add(jPasswordField);

        this.setVisible(true);
        this.setSize(500,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值