62.【GUI编程】

发布于:2022-10-14 ⋅ 阅读:(397) ⋅ 点赞:(0)


Java.Swing与Java.Awt

(一)、AWT

1.Frame (初阶窗体)

使用简单的封装技术,实现多线程的效果.

基本思想:
因为构造函数,也就是构造器。再被调用类的时候,会被默认启动一次。因为我们
一次性启动很多次类所以达到了我们所需要的多线程(Runable)效果.

知识掌握:
调用类之后,构造器会自动被执行。

JavaBean类

package KuangStudy;
import java.awt.*;
public class Student extends Frame {  //继承的是awt包中的Frame
    static int number=0;
    public Student(){}
public Student(int x,int y){  //定义窗体的长和宽
    super("MyFrame "+(++number));
    Color c=new Color(121, 25, 25);
    this.setBackground(c);
    this.setLocationRelativeTo(null);
    setSize(x,y);  
    setVisible(true);
}
}

Test类

package KuangStudy;
import java.awt.*;
public class Test {
    public static void main(String[] args) {
     Student student=new Student(300,300);
     Student student1=new Student(300,300);
     Student student2=new Student(300,300);
     Student student3=new Student(300,300);
     Student student4=new Student(300,300);
}
}

在这里插入图片描述

2.Panel and Frame(适配器)

窗体和画板的结合,画板要内嵌到窗体中去

基本思路:
我们在内嵌画板的时候,需要使用到add()添加的操作。继承分为(继承中)与
(继承完毕)。继承中,子类是不能代替父类的。继承完毕,那么子类就可以代替
父类。比如本次内嵌的操作中,add()的需要是panel.

知识掌握:
继承:(继承中)(继承完毕)
适配器: (new WindoeAdapter),适配器不需要注册监听.

JavaBean:

package KuangStudy;

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

public class Student extends Frame {  //继承的是awt包中的Frame
public Student(){

    //窗体
    Color c=new Color(121, 25, 25);
    this.setLayout(null);//流布局
    setBounds(300,300,500,500);//位置,长度
    this.setBackground(c);

    //画板
    Panel panel=new Panel();
    panel.setBounds(350,350,250,250);//位置,长度
    panel.setBackground(new Color(0,0,0));
    this.add(panel);  //画板添加到窗体中去
    panel.setVisible(true);
    this.setVisible(true);
    
    //接口的适配器 进行退出
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {  //窗口点击关闭的时候,我们需要做什么?
            System.exit(0);
        }
    });
}
}

Test:

package KuangStudy;
import java.awt.*;
public class Test {
    public static void main(String[] args) {
    Student student=new Student();
}
}

在这里插入图片描述

3.Frame_Layout(窗体和布局)

按钮、翻滚页等按钮的添加,进行添加

基本思路:窗体内内嵌按钮对象,

知识掌握: 流布局(FlowLayout())、表格布局(GirdLayout())、
			窗体布局(BorderLayout())、以及按钮类的创建Button();-
package KuangStudy;
import java.awt.*;
public class Test {
    public static void main(String[] args) {
     //窗体
    Frame frame=new Frame();
    frame.setSize(300,300);
    //按钮
      Button button_A=new Button("A");
      Button button_B=new Button("B");
      Button button_C=new Button("C");
      Button button_D=new Button("D");
     //按钮布局
     frame.setLayout(new GridLayout(4,1));
     //按钮进行添加窗体
     frame.add(button_A);
     frame.add(button_B);
     frame.add(button_C);
     frame.add(button_D);
     frame.pack();  //帮助我们自动优化
     frame.setVisible(true);
     frame.setLocationRelativeTo(null);
}
}

在这里插入图片描述

4.Frame_Layout (作业题)

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

public class Test {
    public static void main(String[] args) {
     //窗体
    Frame frame=new Frame();
    frame.setSize(300,300);
    //按钮
      Button button_A=new Button("A");
      Button button_B=new Button("B");
      Button button_C=new Button("C");
      Button button_D=new Button("D");
      Button button_E=new Button("E");
      Button button_F=new Button("F");
      Button button_G=new Button("G");
      Button button_H=new Button("H");
      Button button_I=new Button("I");
      Button button_J=new Button("J");

      //第一个画板
      Panel panel_one=new Panel();
      panel_one.setLayout(new GridLayout(1,2));
      panel_one.add(button_A);
      panel_one.add(button_B);
      //第二个画板
      Panel panel_two=new Panel();
      panel_two.setLayout(new GridLayout(2,2));
      panel_two.add(button_C);
      panel_two.add(button_D);
      panel_two.add(button_E);
      panel_two.add(button_F);

        //第一次整合
        Panel panel_five=new Panel();
        panel_five.setLayout(new BorderLayout());
        panel_five.add(button_G,BorderLayout.WEST);
        panel_five.add(button_H,BorderLayout.EAST);
        panel_five.add(panel_one,BorderLayout.CENTER);
        //第二次整合
        Panel panel_six=new Panel();
        panel_six.setLayout(new BorderLayout());
        panel_six.add(button_I,BorderLayout.WEST);
        panel_six.add(button_J,BorderLayout.EAST);
        panel_six.add(panel_two,BorderLayout.CENTER);
        //窗体整合
        frame.setLayout(new BorderLayout());
        frame.add(panel_five,BorderLayout.NORTH);
        frame.add(panel_six,BorderLayout.CENTER);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
        });
     frame.setLocationRelativeTo(null);
}
}

在这里插入图片描述

5.EventListener(事件监听)

实现事件监听的操作:

基本思想:
implements (监听事件),设置监听,实现监听,实现监听的操作。

知识掌握:
实现监听:  事件源.add(设置监听的对象);
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test {
    public static void main(String[] args) {

      Frame frame=new Frame();
      frame.setSize(300,300);
      frame.setLocationRelativeTo(null);

      Button button=new Button("请");
      frame.add(button);
      MyActionListener myActionListener=new MyActionListener();
      button.addActionListener(myActionListener);
     frame.addWindowListener(new WindowAdapter() {
         @Override
         public void windowClosing(WindowEvent e) {
             if(JOptionPane.showConfirmDialog(null,"您确定要退出么?")==0){
                System.exit(0);
             }
         }
     });

      frame.setVisible(true);
}
    }
 class MyActionListener implements ActionListener {
    public MyActionListener() {
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("点我干嘛");
        JOptionPane.showMessageDialog(null, "别点我");
    }
}

+

6.TextField and TextArea(文本文框)

文本文档以及事件操作:

基本思想: 
标签 :Label=new Label();
文本文框: TextField =new TextField();
按钮:Button =new Button();
对文本问框进行加密: textField_user.setEchoChar('*');
获得文本框中的的内容:TextField textField=(TextField) (e.getSource());



JavaBean类

package KuangStudy;

import javax.swing.*;
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 Student extends Frame implements ActionListener {
    //按钮
    Button button_login=new Button("登入");
    Button button_cancel=new Button("退出");
    //文本文框
    TextField textField_user=new TextField();
    TextField textField_password=new TextField();
        public Student(){
            super.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    if(JOptionPane.showConfirmDialog(null,"您确定要退出此界面?")==0){
                        System.exit(0);
                    }
                }
            });
            this.setSize(300,300);
            this.setBackground(Color.BLUE);
            this.setLocationRelativeTo(null);
            //标签
            Label label_user=new Label("账号");
            Label label_password=new Label("密码");
            //第一次整合
            Panel panel_one=new Panel();
            panel_one.setLayout(new GridLayout(2,2));
            panel_one.add(label_user);
            panel_one.add(textField_user);
            panel_one.add(label_password);
            panel_one.add(textField_password);

            //进行第二次整合
            Panel panel_two=new Panel();
            panel_two.setLayout(new FlowLayout());
            panel_two.add(button_login);
            panel_two.add(button_cancel);
            //进行画板的整合
            this.setLayout(new BorderLayout());
            this.add(panel_one,BorderLayout.CENTER);
            this.add(panel_two,BorderLayout.SOUTH);
            //设置替换编码
            textField_user.setEchoChar('*');
            //进行监听
            getTextField_user().addActionListener(this);
        }

    @Override
    public void actionPerformed(ActionEvent e) {
            //
           TextField textField=(TextField) (e.getSource());
        System.out.println(textField.getText());
        }

    public Button getButton_login() {
        return button_login;
    }

    public Button getButton_cancel() {
        return button_cancel;
    }

    public TextField getTextField_user() {
        return textField_user;
    }

    public TextField getTextField_password() {
        return textField_password;
    }
}

Test类:

package KuangStudy;
public class Test {
    public static void main(String[] args) {
    Student student=new Student();
    student.setVisible(true);
}
}

在这里插入图片描述

7.calc 计算器的操作

基本思路:
字符串转整形:   intger.parseint(字符串A)  把字符串转换成数字
对文本框的内容进行清空的操作 :  textField.setText("");

JavaBean类

package KuangStudy;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Student extends Frame {
    //3个文本框
    TextField textField_one=new TextField(10);  //文本框最多储存的长度
    TextField textField_two=new TextField(10);
    TextField textField_three=new TextField(10);
    //1个按钮
    Button button=new Button("=");
    public Student(){
     this.setLocationRelativeTo(null);
     //一个标签
     Label label=new Label("+");
     
     设置监听,文本文框的内容设置为构造器,便于提取
     button.addActionListener(new MyImplement(textField_one,textField_two,textField_three));
     //进行窗体布局
     this.setLayout(new FlowLayout());
     this.add(textField_one);
     this.add(label);
     this.add(textField_two);
     this.add(button);
     this.add(textField_three);
     pack();
    }
    public TextField getTextField_one() {
        return textField_one;
    }
    public TextField getTextField_two() {
        return textField_two;
    }
    public TextField getTextField_three() {
        return textField_three;
    }
    public Button getButton() {
        return button;
    }
}

接口类:

package KuangStudy;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyImplement implements ActionListener {
    private TextField num1,num2,num3;
    构造器进行获取数据
    public MyImplement(TextField num1,TextField num2,TextField num3){
        this.num1=num1;
        this.num2=num2;
        this.num3=num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    //获得加数
       int a=Integer.parseInt(num1.getText());
       int b=Integer.parseInt(num2.getText());
        //求和
        num3.setText(""+(a+b));
        //清屏
        num1.setText("");
        num2.setText("");
    }
}

测试类:

package KuangStudy;
public class Test {
    public static void main(String[] args) {
    Student student=new Student();
    student.setVisible(true);
}
}

在这里插入图片描述

8.calc 计算机的操作(组合类)

基本思路 : 
清空的操作: setText()
字符串转阿拉布数字 Integr.parseint(字符串)

JavaBean类

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

public class Student extends Frame implements ActionListener {
    //3个文本框
    TextField textField_one=new TextField(10);  //文本框最多储存的长度
    TextField textField_two=new TextField(10);
    TextField textField_three=new TextField(10);
    //1个按钮
    Button button=new Button("=");
    public Student(){
     this.setLocationRelativeTo(null);
     //一个标签
     Label label=new Label("+");
     //进行窗体布局
     this.setLayout(new FlowLayout());
     this.add(textField_one);
     this.add(label);
     this.add(textField_two);
     this.add(button);
     this.add(textField_three);
     pack();
     //进行设置监听:
        getButton().addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
       int a=Integer.parseInt(textField_one.getText());
       int b=Integer.parseInt(textField_two.getText());
       textField_three.setText(""+(a+b));
       textField_one.setText("");
       textField_two.setText("");
    }
    public TextField getTextField_one() {
        return textField_one;
    }
    public TextField getTextField_two() {
        return textField_two;
    }
    public TextField getTextField_three() {
        return textField_three;
    }
    public Button getButton() {
        return button;
    }
}

Test类:

package KuangStudy;
public class Test {
    public static void main(String[] args) {
    Student student=new Student();
    student.setVisible(true);
}
}

在这里插入图片描述

9.Frame下的Paint

基本思想:
1.paint()的覆盖
2.画板添加到框体中去
package KuangStudy;
import java.awt.*;
public class Test {
    public static void main(String[] args) {
        Frame frame=new Frame("傻子");
        frame.setSize(700,500);
        Student student=new Student();
        frame.setBackground(Color.CYAN);
        frame.add(student);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
}
}
package KuangStudy;
import java.awt.*;
public class Student extends Panel {
    public void paint(Graphics g){
        g.fillOval(200,200,100,100);
        Color c=new Color(255,5,5);
        g.setColor(c);
    }
}

在这里插入图片描述

10.画板下的鼠标事件,键盘事件,窗口事件

1.键盘事件:KeyListener
2.鼠标事件:MouseListener
3.窗口WindowListener
package KuangStudy;
import java.awt.*;
public class Test {
    public static void main(String[] args) {
        Frame frame=new Frame("傻子");
        frame.setSize(700,500);
        Student student=new Student();
        frame.setBackground(Color.CYAN);
        frame.add(student);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
}
}
package KuangStudy;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Student extends Panel  {
    public Student (){
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("您点击了"+e.getX()+","+e.getY());
            }
        });
    }
    public void paint(Graphics g){
        g.fillOval(200,200,100,100);
        Color c=new Color(255,5,5);
        g.setColor(c);
    }
    }

在这里插入图片描述

11.利用画板操作模拟画板

基本思路:
1.如果想要多个数据同时存储在画板上,那么我们就需要使用数组的操作
进行储存位置。
2.ArrayList : 进行集合的储存操作
3.MouseListener 鼠标事件

JavaBean

package KuangStudy;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
public class Student extends Panel  {
    ArrayList<Integer> arrayList_x=new ArrayList();
    ArrayList <Integer>arrayList_y=new ArrayList();
    int x;
    int y;
    public Student (){
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                x=e.getX();
                y=e.getY();
                arrayList_x.add(x);
                arrayList_y.add(y);
                System.out.println("您点击了"+e.getX()+","+e.getY());
                repaint();
            }
        });
    }
    public void paint(Graphics g){
        Color c=new Color(255,5,5);
        g.setColor(c);
        for (int i = 0; i <arrayList_x.size(); i++) {
            g.fillOval(arrayList_x.get(i),arrayList_y.get(i),10,10);
        }
    }
}

Test

package KuangStudy;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Test {
    public static void main(String[] args) {
        Frame frame=new Frame("傻子");
        frame.setSize(700,500);
        Student student=new Student();
        frame.setBackground(Color.CYAN);
        frame.add(student);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
}
}

在这里插入图片描述

(二)、SWING

1.JFrame(高阶画板)

基本思路:
1.窗体关闭
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
2.把标签的位置放到中间
label_one.setHorizontalAlignment(SwingConstants.CENTER);
package KuangStudy;
import javax.swing.*;
public class first extends JFrame {
    public first(){
        this.setSize(300,300);
        this.setLocationRelativeTo(null);
        this.setTitle("JFrame");
        this.setVisible(true);
        JLabel label_one=new JLabel("欢迎来到我的世界");
        this.add(label_one);
        //把标签的位置放到中间
        label_one.setHorizontalAlignment(SwingConstants.CENTER);
        //关闭事件的操作
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

2.JDialog(对话框)

基本思路:
1.弹窗 JDialog 也是一个窗口,也需要设置相应的大小,宽高,展现
(有默认的关闭事件,不用再次添加)
JDialog jDialog_one=new JDialog();
2.按钮: 按钮也能进行位置和大小的设置
 button_dialog.setBounds(30,30,50,50);
package KuangStudy;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class first extends JFrame {
    public first(){
        this.setSize(300,300);
        this.setLocationRelativeTo(null);
        this.setTitle("JFrame");
        this.setVisible(true);
        //把其添加到窗体中
        JLabel label_one=new JLabel("欢迎来到我的世界ya");
        this.add(label_one);
        //把文本放到中间的位置
        label_one.setHorizontalAlignment(SwingConstants.CENTER);
        //关闭事件的操作
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JButton button_dialog=new JButton("点我弹窗");
        //为按钮设置长和宽以及位置
        button_dialog.setBounds(30,30,50,50);
        this.add(button_dialog);

        //添加按钮事件的适配器
        button_dialog.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog jDialog_one=new JDialog();
                jDialog_one.setBounds(100,100,100,100);
               jDialog_one.add(label_one);
                jDialog_one.setVisible(true);
                //JOptionPane.showMessageDialog(null,"yes");
            }
        });
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

3.ICON(标签_图标)

基本思路:
1. Icon 是一个接口
2. 具体实现:需要在第一个方法中,填写腰围和什么图形
3. 声明图标以及水平线的位置.
4. JLabel jLabel_one=new JLabel("是我呀",first_one(实现接口的对象),SwingConstants.CENTER);
5. 这里没有定义x,y的具体位置
package KuangStudy;
import javax.swing.*;
import java.awt.*;

public class first extends JFrame implements Icon {
    private  int width;
    private int height;
    public first(){

    }
    public first(int width,int height){
        this.width=width;
        this.height=height;
    }
    //运用方法对其进行初始化的操作
    public void init(){
        //设置宽度
       first first_one=new first(50,50);
       //设置图片标签
       JLabel jLabel_one=new JLabel("是我呀",first_one,SwingConstants.CENTER);
       this.add(jLabel_one);
    }
    //进行绘画
    @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 KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
        first_one.init();
        first_one.setSize(500,500);
        first_one.setLocationRelativeTo(null);
        first_one.setVisible(true);
}
}

在这里插入图片描述

4.imageIcon (标签_图片)

基本思路:
1.获取地址: 类名.class.getResource()//获取地址
2.创建imageIcon : ImageIcon imageIcon=new ImageIcon(url);
3.设置成标签: 标签对象.setIcon();
4.水平居中: 标签对象.setHorizontalAlignment(SwingConstants.CENTER);
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class first extends JFrame  {
    public first(){
       JLabel label_one=new JLabel("吉士先生背景图片");
       //获取图片地址;
        URL url = first.class.getResource("imag.jpg");
        //从指定的URL创建一个ImageIcon。
        ImageIcon imageIcon=new ImageIcon(url);
        //把图片设置成标签
        label_one.setIcon(imageIcon);
        //标签水平线居中
        label_one.setHorizontalAlignment(SwingConstants.CENTER);
        //把标签添加到窗体中去
        this.add(label_one);
        //进行窗体的设置效果
        this.setSize(400,400);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

5.Container (容器)

基本思路:
1.返回此框架的 contentPane对象。 
 Container container=this.getContentPane();
2.设置间距:
container.setLayout(new GridLayout(2,1,10,10));
package KuangStudy;
import javax.swing.*;
import java.awt.*;

public class first extends JFrame  {
    public first(){
        Container container=this.getContentPane();//框体的容器
        container.setLayout(new GridLayout(2,1,10,10));
        JPanel jPanel_one=new JPanel(new GridLayout(1,3));
        JPanel jPanel_two=new JPanel(new GridLayout(1,2));
        JPanel jPanel_three=new JPanel(new GridLayout(2,3));
        JPanel jPanel_four=new JPanel(new GridLayout(3,2));
        jPanel_one.add(new JButton("1"));
        jPanel_one.add(new JButton("1"));
        jPanel_one.add(new JButton("1"));

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

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

        jPanel_four.add(new JButton("1"));
        jPanel_four.add(new JButton("1"));
        jPanel_four.add(new JButton("1"));
        jPanel_four.add(new JButton("1"));
        jPanel_four.add(new JButton("1"));
        jPanel_four.add(new JButton("1"));
        container.add(jPanel_one);
        container.add(jPanel_two);
        container.add(jPanel_three);
        container.add(jPanel_four);
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

6.ScorllPanel (滑动块_记事本)

基本思路:
1.文本域添加到滑动块中去
 JScrollPane jScrollPane_one=new JScrollPane(textArea_one);
2.滑动块添加到画板中去

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

public class first extends JFrame  {
    public first(){
        Container container=this.getContentPane();//框体的容器
        //文本域
        TextArea textArea_one=new TextArea(20,30);
        //设置默认的文字
        textArea_one.setText("欢迎来到我的世界! ");
        //把文本域添加到滚动块中去
        JScrollPane jScrollPane_one=new JScrollPane(textArea_one);
        //把滚动块添加到窗体中去
        container.add(jScrollPane_one);
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

7.ICON (按钮_图标)

基本思路:
1.导入图片 地址url
2.利用url生成图标
3.按钮设置成图标: button.setIcon(imag);
4.按钮添加到窗体.
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class first extends JFrame  {
    public first(){
        //框体的容器
        Container container=this.getContentPane();
        URL url = first.class.getResource("imag.jpg");
        //url导入变成ICon
        ImageIcon Icon_one=new ImageIcon(url);

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

        container.add(button);
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

·在这里插入图片描述

8.JRadioButton (单选按钮)

基本思路:
1.单选框: JRadioButton
2.分组: ButtonGroup
3.把单选按钮添加到分组中,进行分组
 buttonGroup_one.add(jRadioButton_one);
4.单选按钮添加到窗体
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class first extends JFrame  {
    public first(){
        //框体的容器
        Container container=this.getContentPane();
        //单选框
       JRadioButton jRadioButton_one=new JRadioButton("A 你是大傻瓜");
       JRadioButton jRadioButton_two=new JRadioButton("B 我是大傻瓜");
       JRadioButton jRadioButton_three=new JRadioButton("C 我俩都是大傻瓜");
       //由于单选框只能选择一个,所以我们要对其分组
        ButtonGroup buttonGroup_one=new ButtonGroup();
        //单选按钮分组添加
        buttonGroup_one.add(jRadioButton_one);
        buttonGroup_one.add(jRadioButton_two);
        buttonGroup_one.add(jRadioButton_three);
        //添加到窗体中
        container.add(jRadioButton_one,BorderLayout.NORTH);
        container.add(jRadioButton_two,BorderLayout.CENTER);
        container.add(jRadioButton_three,BorderLayout.SOUTH);

        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

9.JRadioButton(单选按钮_实现多选)

基本思路:
1.删除分组的操作。
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class first extends JFrame  {
    public first(){
        //框体的容器
        Container container=this.getContentPane();
        //单选框
       JRadioButton jRadioButton_one=new JRadioButton("A 你是大傻瓜");
       JRadioButton jRadioButton_two=new JRadioButton("B 我是大傻瓜");
       JRadioButton jRadioButton_three=new JRadioButton("C 我俩都是大傻瓜");
 /*      //由于单选框只能选择一个,所以我们要对其分组
        ButtonGroup buttonGroup_one=new ButtonGroup();
        buttonGroup_one.add(jRadioButton_one);
        buttonGroup_one.add(jRadioButton_two);
        buttonGroup_one.add(jRadioButton_three);
*/
        //添加到窗体中
        container.add(jRadioButton_one,BorderLayout.NORTH);
        container.add(jRadioButton_two,BorderLayout.CENTER);
        container.add(jRadioButton_three,BorderLayout.SOUTH);

        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

10.JcheckBox (多选框)

基本思路:
1.记住多选框的类: JcheckBox()
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class first extends JFrame  {
    public first(){
        //框体的容器
        Container container=this.getContentPane();
        //多选框
        JCheckBox jCheckBox_one=new JCheckBox("A 谁是傻子?");
        JCheckBox jCheckBox_two=new JCheckBox("B 吉士先生");
        JCheckBox jCheckBox_three=new JCheckBox("C 李明");
        //添加到窗体中去
        container.add(jCheckBox_one,BorderLayout.NORTH);
        container.add(jCheckBox_two,BorderLayout.CENTER);
        container.add(jCheckBox_three,BorderLayout.SOUTH);
        //窗体显示
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

11.JComBox (下拉框)

基本思路:
1.理解 JComBox
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class first extends JFrame  {
    public first(){
        //框体的容器
        Container container=this.getContentPane();
        //下拉框
        JComboBox jComboBox_one=new JComboBox();
        //下拉框添加
        jComboBox_one.addItem(null);
        jComboBox_one.addItem("正字热映...");
        jComboBox_one.addItem("已下架");
        jComboBox_one.addItem("即将上映");
        //添加到面板优化位置
        JPanel jPanel=new JPanel(new GridLayout(10,1));
        jPanel.add(jComboBox_one);
        pack();
        //添加到窗体
        container.add(jPanel);
        //窗体显示
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

12.JList (列表框)

基本思路:
1.了解 JList
2.了解 Vector 这个集合
3.JList 里面要添加集合/数组
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.Vector;

public class first extends JFrame  {
    public first(){
        //框体的容器
        Container container=this.getContentPane();
       //列表框
        Vector vector=new Vector();
        JList jList_one=new JList(vector);
        vector.add(1);
        vector.add(1);
        vector.add(1);
        vector.add(1);
        //添加到窗体
        container.add(jList_one);
        //窗体显示
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

13.JTextField (文本框)

基本思路:
1.JTextField (文本框)
2.
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.Vector;

public class first extends JFrame  {
    public first(){
        //框体的容器
        Container container=this.getContentPane();

        JTextField jTextField_one=new JTextField("hello");
        JTextField jTextField_two=new JTextField("world");

        jTextField_two.setBounds(50,50,100,100);
        container.add(jTextField_two);
        container.add(jTextField_one,BorderLayout.CENTER);

        //窗体显示
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

14.JPasswordText (密码框)

基本思路:
1.JPasswordText  密码框加密
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.Vector;

public class first extends JFrame  {
    public first(){
        //框体的容器
        Container container=this.getContentPane();

        JPasswordField jPasswordField_one=new JPasswordField("请输入密码");
        JPasswordField jPasswordField_two=new JPasswordField("请输入您的账号");

        container.add(jPasswordField_one,BorderLayout.NORTH);
        container.add(jPasswordField_two,BorderLayout.CENTER);

        //窗体显示
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
}
package KuangStudy;
public class Test {
    public static void main(String[] args) {
        first first_one=new first();
}
}

在这里插入图片描述

本文含有隐藏内容,请 开通VIP 后查看