栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

GUI编程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

GUI编程

目录

GUI编程

GUI介绍AWT

1.AWT介绍:2.组件和容器

1.frame2.回顾封装3.面板Panel4.布局管理器5.练习6.监听事件7.多个按钮,共享一个事件8.输入框 TextField 监听9.简易计算器,组合+内部类回顾复习!10.画笔 Paint11.鼠标监听12.窗口监听13.键盘监听 Swing

1.窗口、面板2.弹窗 Dialog3.标签4.面板5.按钮6.列表7.文本框 总结

1.AWT2.Swing

GUI编程 GUI介绍
    GUI的核心技术:Swing AWT
      因为界面不美观需要jre环境
    为什么学GUI
      可以写出自己心中想要的一些小工具工作时候,也可能需要维护到swing界面,概率极小!了解MVC架构,了解监听
AWT 1.AWT介绍:
    包含了很多类和接口!GUI !元素:窗口,按钮,文本框java.awt
2.组件和容器 1.frame
import java.awt.*;

//GUI的第一个界面
public class Testframe {
    public static void main(String[] args) {
        //frame,JDK,看源码!
        frame frame = new frame("我的第一个Java图像界面窗口");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色  Color
        frame.setBackground(new Color(8, 229, 211));
        //弹出的初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
    }
}
2.回顾封装
import java.awt.*;

public class Testframe2 {
    public static void main(String[] args) {
        //展示多个窗口 new
        Myframe myframe1 = new Myframe(100, 100, 200, 200, Color.blue);
        Myframe myframe2 = new Myframe(300, 100, 200, 200, Color.yellow);
        Myframe myframe3 = new Myframe(100, 300, 200, 200, Color.red);
        Myframe myframe4 = new Myframe(300, 300, 200, 200, Color.magenta);
    }
}

class Myframe extends frame{
    static int id=0;//可能存在多个窗口,外面需要一个计数器
    public Myframe(int x,int y,int w,int h,Color color){
        super("Myframe+"+(++id));
        setBackground(color);
        setBounds(x,y,w,h);
        //需要设置可见性
        setVisible(true);
    }
}
3.面板Panel

解决了关闭事件

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

//Panel 可以看成是一个空间,但不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        frame frame = new frame();
        Panel panel = new Panel();
        //设置布局
        frame.setLayout(null);
        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(40,161,35));
        //Panel 设置坐标,相对于frame
        panel.setBounds(50,50,400,400);;
        panel.setBackground(new Color(193,15,60));
        //frame.add(panel)
        frame.add(panel);
        frame.setVisible(true);
        //监听事件,监听窗口关闭事件,System.exit(0)
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}
4.布局管理器

流式布局

import java.awt.*;

public class TestFlowLayout {
    public static void main(String[] args) {
        frame frame = new frame();
        //组件-按钮
        Button button1 = new Button("Button1");
        Button button2 = new Button("Button1");
        Button button3 = new Button("Button1");

        //设置为流式布局
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));往左
        //frame.setLayout(new FlowLayout(FlowLayout.RIGHT));往右
        frame.setLayout(new FlowLayout());
        frame.setSize(200,200);
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
    }
}

东西南北中

import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        frame frame = new frame("TestBorderLayout");

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

表格布局 Grid

import java.awt.*;

public class TestGridLayout {
    public static void main(String[] args) {
        frame frame = new frame("TestGridLayout");

        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);

        frame.pack();//java函数
        frame.setVisible(true);
    }
}
5.练习

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DtOiM3KR-1646570739858)(G:JavaSETypora笔记imgGUI练习1.png)]

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

public class Practice {
    public static void main(String[] args) {
        frame frame = new frame("练习");
        Panel p1=new Panel();
        Panel p2=new Panel();

        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");
        Button btn7 = new Button("btn7");
        Button btn8 = new Button("btn8");
        Button btn9 = new Button("btn9");
        Button btn10 = new Button("btn10");

        frame.setLayout(new GridLayout(2,3));
        frame.add(btn1);
        frame.add(p1);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(p2);
        frame.add(btn10);
        p1.setLayout(new GridLayout(2,1));
        p1.add(btn2);
        p1.add(btn3);
        p2.setLayout(new GridLayout(2,2));
        p2.add(btn6);
        p2.add(btn7);
        p2.add(btn8);
        p2.add(btn9);

        frame.setSize(200,200);
        frame.setVisible(true);

        //监听事件,监听窗口关闭事件,System.exit(0)
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}
6.监听事件
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 TextActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        frame frame = new frame();
        Button button = new Button();
        //因为,addActionListener()需要一个 ActionListener,所以我们需要构造一个 ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();

        windowClos(frame);//关闭窗口
        frame.setVisible(true);

    }
    //关闭窗体的事件
    private static void windowClos(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("aaa");
    }
}
7.多个按钮,共享一个事件
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 TestActionTwo {
    public static void main(String[] args) {
        //两个按纽,实现同一个监呀
        //开始    停止
        frame frame = new frame();
        Button button1 = new Button("start");//开始
        Button button2 = new Button("stop");//停止
        //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值!
        // 可以多个按钮只写一个监听类
        button2.setActionCommand("stop-2");//停止-2

        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

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

        frame.pack();
        windowClos(frame);
        frame.setVisible(true);
    }
    //关闭窗体的事件
    private static void windowClos(frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//监听事件
class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand()  获取按钮信息
        System.out.println("msg==>"+e.getActionCommand());
        if (e.getActionCommand().equals("start")){
            
        }else if (e.getActionCommand().equals("stop-2")){

        }
    }
}
8.输入框 TextField 监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestText01 {
    public static void main(String[] args) {
        //启动!
        new Myframe();
    }
}
class Myframe extends frame{
    public Myframe(){
        TextField textField = new TextField();
        add(textField);
        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);
        //设置替换编码
        textField.setEchoChar('*');

        setVisible(true);
        pack();
    }
}
class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField)e.getSource();//获得一些资源,返回的一个对象
        System.out.println(field.getText());//获得输入框的文本
        field.setText("");//清空
    }
}
9.简易计算器,组合+内部类回顾复习!

oop原则:组合,大于继承


目前代码:

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

public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}
//计算器类
class Calculator extends frame{
    public Calculator(){
        //3 个文本框
        TextField num1 = new TextField(10);//字符数
        TextField num2 = new TextField(10);//字符数
        TextField num3 = new TextField(20);//字符数
        //1 个按钮
        Button button = new Button("=");

        button.addActionListener(new MyCalculatorListener(num1,num2,num3));
        //1 个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        setVisible(true);
        pack();
    }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    //获取三个变量
    private TextField num1,num2,num3;
    public MyCalculatorListener(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));
        //3.清除前两个框
        num1.setText("");
        num2.setText("");
    }
}

优化:完全改造为面向对象写法

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

public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadframe();
    }
}
//计算器类
class Calculator extends frame{
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadframe(){
        //3 个文本框
        num1 = new TextField(10);//字符数
        num2 = new TextField(10);//字符数
        num3 = new TextField(20);//字符数
        //1 个按钮
        Button button = new Button("=");
        //1 个标签
        Label label = new Label("+");

        button.addActionListener(new MyCalculatorListener(this));

        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        setVisible(true);
        pack();
    }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    //获取计算器这个对象,在一个类中组合另外一个类;
    Calculator calculator=null;
    public MyCalculatorListener(Calculator calculator){
        this.calculator=calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1=Integer.parseInt(calculator.num1.getText());
        int n2=Integer.parseInt(calculator.num2.getText());
        //2.将这个值+法运算后,放到第三个框
        calculator.num3.setText(""+(n1+n2));
        //3.清除前两个框
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}

优化:内部类

更好的包装

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

public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadframe();
    }
}
//计算器类
class Calculator extends frame{
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadframe(){
        //3 个文本框
        num1 = new TextField(10);//字符数
        num2 = new TextField(10);//字符数
        num3 = new TextField(20);//字符数
        //1 个按钮
        Button button = new Button("=");
        //1 个标签
        Label label = new Label("+");

        button.addActionListener(new MyCalculatorListener());

        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        setVisible(true);
        pack();
    }
    //监听器类
    //内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法!
    private class MyCalculatorListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            int n1=Integer.parseInt(num1.getText());
            int n2=Integer.parseInt(num2.getText());
            //2.将这个值+法运算后,放到第三个框
            num3.setText(""+(n1+n2));
            //3.清除前两个框
            num1.setText("");
            num2.setText("");
        }
    }
}
10.画笔 Paint
import java.awt.*;

public class TestPanint {
    public static void main(String[] args) {
        new MyPaint().loadframe();
    }
}
class MyPaint extends frame{
    public void loadframe(){
        setBounds(200,200,600,400);
        setVisible(true);
    }
    //画笔
    @Override
    public void paint(Graphics g) {
        //画笔,需要有颜色,画笔可以画画
        //g.setColor(Color.red);
        //g.drawOval(100,100,100,100);//空心的圆
        g.fillOval(100,100,100,100);//实心的圆

        g.fillRect(200,200,200,200);//矩形

        //养成习惯,画管用完,将他还原到最初的颜色
    }
}
11.鼠标监听
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 Myframe("画笔");
    }
}
//自己的类
class Myframe extends frame{
    //画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    ArrayList points;
    public Myframe(String title){
        super(title);
        //设置窗体位置和大小
        setBounds(200,200,400,300);
        //存鼠标点击的点
        points=new ArrayList<>();

        setVisible(true);
        //鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());
    }

    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator=points.iterator();
        while (iterator.hasNext()){
            Point point=(Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    //添加一个点到界面上
    public void addPaint(Point point){
        points.add(point);
    }
    //适配器模式
    private class MyMouseListener extends MouseAdapter{
        //鼠标 按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            Myframe frame=(Myframe) e.getSource();
            //这个我们点击的时候,就会在界面上产生一个点!画
            //这个点就是鼠标的点;
            frame.addPaint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都需要重新画一遍
            frame.repaint();//刷新
        }
    }
}
12.窗口监听
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindow {
    public static void main(String[] args) {
        new Windowframe();
    }
}
class Windowframe extends frame{
    public Windowframe(){
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
        //addWindowListener(new MyWindowListener());
        //匿名内部类
        this.addWindowListener(new WindowAdapter() {
            //关闭窗口
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("你点击了X");
                System.exit(0);
            }
            //激活窗口
            @Override
            public void windowActivated(WindowEvent e) {
                //改变标题
                Windowframe source=(Windowframe) e.getSource();
                source.setTitle("你被激活了");

                System.out.println("windowActivated");
            }
        });
    }
}
13.键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

//键盘监听
public class TestKeyListener {
    public static void main(String[] args) {
        new Keyframe();
    }
}
class Keyframe extends frame{
    public Keyframe(){
        setBounds(1,2,300,400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘下的键是哪一个,当前的码
                int keyCode=e.getKeyCode();//不需需要去记录这个数值,直接使用静态属性VK_xxx
                System.out.println(keyCode);
                if (keyCode==KeyEvent.VK_UP){
                    System.out.println("你按下了上键");
                }
                //根据按下不同操作,产生不同结果;
            }
        });
    }
}
Swing 1.窗口、面板
import javax.swing.*;
import java.awt.*;

public class JframeDemo {
    //init() 初始化
    public void init(){
        //顶级窗口
        Jframe jframe = new Jframe("这是一个Jframe窗口");
        jframe.setVisible(true);
        jframe.setBounds(100,100,200,200);
        jframe.setBackground(Color.cyan);
        //设置文字 JLabel
        JLabel jLabel = new JLabel("欢迎来到Jframe窗口");
        jframe.add(jLabel);
        //让文本标签居中 设置水平对齐
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器
        Container contentPane = jframe.getContentPane();
        contentPane.setBackground(Color.yellow);
        //关闭事件
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JframeDemo().init();
    }
}

标签居中:

//让文本标签居中 设置水平对齐
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
2.弹窗 Dialog

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

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

//主窗口
public class DialogDemo extends Jframe {
    public DialogDemo(){
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //Jframe 放东西,容器
        Container contentPane = this.getContentPane();
        //绝对布局
        contentPane.setLayout(null);
        //按钮
        JButton jButton = new JButton("点击弹出一个对话框");//创建
        jButton.setBounds(30,30,200,50);
        //点击这个按钮的时候,弹出一个弹窗
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();
            }
        });

        contentPane.add(jButton);
    }

    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 contentPane = this.getContentPane();
        contentPane.setLayout(null);

        contentPane.add(new Label("带你学Java"));
    }
}
3.标签

label

new JLabel("xxx");

图标ICON

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

//图标,需要实现类,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(15,15);
        //图标放在标签,也可以放在按钮上!
        JLabel jLabel = new JLabel("icontest",iconDemo,SwingConstants.CENTER);

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

        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

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

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

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

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

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

    public static void main(String[] args) {
        new ImageIconDemo();
    }
}
4.面板

JPanel

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 Panel1 = new JPanel(new GridLayout(1,3));

        Panel1.add(new JButton("1"));
        Panel1.add(new JButton("1"));
        Panel1.add(new JButton("1"));

        container.add(Panel1);
        container.add(Panel1);
        container.add(Panel1);

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

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

JScrollPanel

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

public class JScrollDemo extends Jframe {
    public JScrollDemo(){
        Container contentPane = getContentPane();

        //文本域
        Jtextarea jtextarea = new Jtextarea(20,50);
        jtextarea.setText("欢迎学习java");

        //Scroll 面板
        JScrollPane jScrollPane = new JScrollPane(jtextarea);
        contentPane.add(jScrollPane);

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

    public static void main(String[] args) {
        new JScrollDemo();
    }
}
5.按钮

图片按钮

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

public class JButtonDemo01 extends Jframe {
    public JButtonDemo01(){
        Container contentPane = getContentPane();
        //将一个图片变为图标
        URL resource=JButtonDemo01.class.getResource("8.png");
        Icon icon=new ImageIcon(resource);
        //把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        //add
        contentPane.add(button);

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

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

单选按钮

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

public class JButtonDemo02 extends Jframe {
    public JButtonDemo02(){
        Container contentPane = getContentPane();

        //单选框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
        //由于单选框只能选择一个,分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

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

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

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

复选按钮

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

public class JButtonDemo03 extends Jframe {
    public JButtonDemo03(){
        Container contentPane = getContentPane();

        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");

        contentPane.add(checkBox01,BorderLayout.NORTH);
        contentPane.add(checkBox02,BorderLayout.SOUTH);

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

    public static void main(String[] args) {
        new JButtonDemo03();
    }
}
6.列表

下拉框

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

public class TestComboboxDemo01 extends Jframe {
    public TestComboboxDemo01(){
        Container contentPane = getContentPane();
        JComboBox jComboBox = new JComboBox();

        jComboBox.addItem(null);
        jComboBox.addItem("正在热映");
        jComboBox.addItem("已下架");
        jComboBox.addItem("即将上映");

        contentPane.add(jComboBox);

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

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

列表框

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

public class TestComboboxDemo02 extends Jframe {
    public TestComboboxDemo02(){
        Container contentPane = getContentPane();

        //生成列表的内容
        //String[] contents={"1","2","3"};
        //动态添加数据
        Vector contents = new Vector();
        //列表中需要放入内容
        JList jList = new JList(contents);

        contents.add("张三");
        contents.add("李四");
        contents.add("王五");

        contentPane.add(jList);

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

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

应用场景

选择地区,或者一些单个选项列表,展示信息,一般是动态扩容! 7.文本框

文本框

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

public class TestTextDemo01 extends Jframe {
    public TestTextDemo01(){
        Container contentPane = getContentPane();

        JTextField textField = new JTextField("hello");
        JTextField textField2 = new JTextField("world",20);

        contentPane.add(textField,BorderLayout.NORTH);
        contentPane.add(textField2,BorderLayout.SOUTH);

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

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

密码框

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

public class TestTextDemo02 extends Jframe {
    public TestTextDemo02(){
        Container contentPane = getContentPane();

        JPasswordField passwordField = new JPasswordField();//*****
        passwordField.setEchoChar('*');

        contentPane.add(passwordField);

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

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

文本域

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

public class JScrollDemo extends Jframe {
    public JScrollDemo(){
        Container contentPane = getContentPane();

        //文本域
        Jtextarea jtextarea = new Jtextarea(20,50);
        jtextarea.setText("欢迎学习java");

        //Scroll 面板
        JScrollPane jScrollPane = new JScrollPane(jtextarea);
        contentPane.add(jScrollPane);

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

    public static void main(String[] args) {
        new JScrollDemo();
    }
}
总结 1.AWT
frame frame = new frame();//构造一个最初不可见的 frame 新实例()
frame.setVisible(true);//需要设置可见性
frame.setSize(500,400);//设置窗口大小,宽度500,高度400
frame.setBackground(new Color(8, 229, 211));//设置背景颜色  Color
frame.setLocation(200,200);//弹出的初始位置
frame.setResizable(false);//设置大小固定
frame.setBounds(300,300,500,500);//坐标
frame.setLayout(null);//设置窗口固定
frame.pack();//填充
frame.addWindowListener(new WindowAdapter() {});//监听事件,windowClosing监听窗口关闭事件,System.exit(0)

Panel panel = new Panel();//面板
frame.add(panel);//添加面板到窗体里

frame.setLayout(new FlowLayout());//设置为流式布局
// FlowLayout.LEFT往左,FlowLayout.RIGHT往右
frame.add(panel,BorderLayout.CENTER);//东西南北中
// EAST东,WEST西,SOUTH南,NORTH北,CENTER中
frame.setLayout(new GridLayout(2,1));//表格布局,两行一列

Button button = new Button("Button");//组件-按钮

TextField textField = new TextField();//输入框
frame.add(textField);
textField.setEchoChar('*');//设置替换编码

Label label = new Label("+");//1 个标签

//继承重写paint画笔类
//g.fillOval(100,100,100,100);//实心的圆
//g.fillRect(200,200,200,200);//矩形
2.Swing
Jframe jframe = new Jframe("这是一个Jframe窗口");//顶级窗口
JLabel jLabel = new JLabel("欢迎来到Jframe窗口");//设置文字 JLabel
jframe.add(jLabel);//添加
jLabel.setHorizontalAlignment(SwingConstants.CENTER);//让文本标签居中 设置水平对齐
Container contentPane = jframe.getContentPane();//获得一个容器
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭事件
//按钮
JButton jButton = new JButton("点击弹出一个对话框");//创建
jButton.addActionListener(new ActionListener() {});//重写actionPerformed点击这个按钮的时候,弹出一个弹窗,//弹窗new一个自建一个继承JDialog类

//单选框
JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);

//多选框
JCheckBox checkBox01 = new JCheckBox("checkBox01");
contentPane.add(checkBox01,BorderLayout.NORTH);
//下拉框
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("正在热映");
//列表框
Vector contents = new Vector();
JList jList = new JList(contents);//列表中需要放入内容
contents.add("张三");
//文本框
JTextField textField = new JTextField("hello");
//密码框
JPasswordField passwordField = new JPasswordField();//*****
passwordField.setEchoChar('*');
//文本域
Jtextarea jtextarea = new Jtextarea(20,50);
jtextarea.setText("欢迎学习java");
JScrollPane jScrollPane = new JScrollPane(jtextarea);//Scroll 面板
contentPane.add(jScrollPane);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756043.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号