GUI(图形用户编程)编程

AWT

组件

窗口,面板,按钮,鼠标,键盘监听事件之类的

gui的核心技术

Swing 和 AWT

  1. 因为界面不美观
  2. 需要jre环境

为什么还要学

  1. 可以写自己用的小工具
  2. 可能在工作中维护Swing界面(概率极小,老公司
  3. 了解MVC架构,了解监听!!

第一个Frame弹窗

public class TestFrame {   
    public static void main(String[] args) {      
        Frame frame = new Frame("我的第一个java图像窗口");       
        
        //设置可见性       
        frame.setVisible(true);     
        //设置窗口大小      
        frame.setSize(400,400);     
        //设置背景颜色       
        frame.setBackground(new Color(2,16,4));    
        //弹出的初始位置      
        frame.setLocation(200,200);    }

把上面的封装起来

public class myTestFrame {  
    public static void main(String[] args) {     
        MyFrame myFrame1 = new MyFrame(100, 200, 400, 400, Color.CYAN);      
        MyFrame myFrame2 = new MyFrame(300, 200, 400, 400, Color.yellow);      
        MyFrame myFrame3 = new MyFrame(100, 300, 400, 400, Color.gray);      
        MyFrame myFrame4 = new MyFrame(300, 300, 400, 400, Color.blue);    
    }
}  


class MyFrame extends Frame {     
    static int id=0;        
    public MyFrame(int x, int y, int w, int h, Color col) {     
        super("myframe" +(++id));           
        setBackground(col);        
        setBounds(x,y,w,h);//长,宽,高位置都有了       
        setVisible(true);        }}

panel面板的使用

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class PanelStudy {    
    public static void main(String[] args) {     
        Frame frame1 = new Frame();       
        Panel panel = new Panel();      
        frame1.setLayout(null);       
        frame1.setBounds(200,200,500,500);   
        frame1.setBackground(new Color(255, 243, 117));     
        panel.setBounds(100,100,300,300);      
        panel.setBackground(new Color(148, 255, 81));        
        
        frame1.add(panel);       
        frame1.setVisible(true);         
        
        //监听事件,实行鼠标点击关闭(监听适配器)    
        frame1.addWindowListener(new WindowAdapter() {        
            @Override          
            public void windowClosing(WindowEvent e) {      
                super.windowClosing(e);               
                //                
                System.exit(0);           
                // System.out.println(0);   可以输出东西:意外!!   、   
            }
        }        );    }

FlowLayout 流式布局

默认为中,从左到右

Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
frame1.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel.add(button3);

BorderLayout 东西南北中布局

public class BorderLayoutTest {    
    public static void main(String[] args) {   
        BorderLayout borderLayout1 = new BorderLayout();   
        Frame frame = new Frame();        
        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(north,BorderLayout.NORTH);   
        frame.add(south,BorderLayout.SOUTH);  
        frame.add(center,BorderLayout.CENTER);    
        
        frame.setVisible(true);        
        frame.setSize(200,200);       
        
        //监听关闭       
        frame.addWindowListener(new WindowAdapter() {   
            @Override           
            public void windowClosing(WindowEvent e) {       
                super.windowClosing(e);             
                System.exit(0);            }        });

GridLayout(2,1)栅格布局

做出如下的图像

不知道为什么两边的按钮如此的小

主要是面板的嵌套,在初始化时就要设置

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QQXd78Py-1636275665085)(C:\Users\74771\AppData\Roaming\Typora\typora-user-images\1632741693874.png)]

public class Tset1 {  
    public static void main(String[] args) {    
        //面板初始化
        Frame frame = new Frame();      
        Panel panel1 = new Panel(new BorderLayout());       
        Panel panel2 = new Panel(new BorderLayout());    
        Panel panel3 = new Panel(new GridLayout(2,1)); 
        Panel panel4 = new Panel(new GridLayout(2,2));      
        
        frame.setBounds(500,500,1000,1000);       
        frame.setBackground(Color.CYAN);      
        
        Button bt1 = new Button();      
        Button bt2 = new Button();      
        Button bt3 = new Button();      
        Button bt4 = new Button();      
        //添加
        frame.setLayout(new GridLayout(2,1));   
        frame.add(panel1);      
        frame.add(panel2);       
        
        //上半部分,主要是面板的几种布局:流式,东西南北中,阑珊
        panel1.add(bt1,BorderLayout.WEST);     
        panel1.add(panel3,BorderLayout.CENTER);     
        panel1.add(bt2,BorderLayout.EAST);    
        panel3.add(bt3,BorderLayout.NORTH);     
        panel3.add(bt4,BorderLayout.SOUTH);  
        
        //下半部分
        panel2.add(new Button(),BorderLayout.WEST);   
        panel2.add(new Button(),BorderLayout.EAST);     
        panel2.add(panel4,BorderLayout.CENTER);      
        panel4.add(new Button());    
        panel4.add(new Button());    
        panel4.add(new Button());      
        panel4.add(new Button());      
        
        frame.setVisible(true);       
        frame.addWindowListener(new WindowAdapter() {        
            @Override           
            public void windowClosing(WindowEvent e) {           
                super.windowClosing(e);              
                System.exit(0);            }        });    }

事件监听

重写一个class监听多个按钮,不用匿名内部类

public class listenStudy {   
    public static void main(String[] args) {    
        Frame frame = new Frame();        
        Button button1 = new Button("start");  
        Button button2 = new Button("stop");       
        frame.add(button1,BorderLayout.SOUTH);      
        frame.add(button2,BorderLayout.NORTH);      
        
        //两个按钮实现同一个监听,匿名内部类,      
        // 太麻烦了,应该再写一个类,明天吧,今天图书馆快要闭关了。2021.9.27      
        //重点是把匿名内部类分离出来,像在另一个类class中创建,写在下面就OK了        
        // 明天见。
               button1.addActionListener(new ActionListener() {//        
                   @Override//           
                   
                   public void actionPerformed(ActionEvent e) {//                
                       System.out.println(e.getActionCommand());//     
                   }//        });    
                    button2.addActionListener(new ActionListener() {//       
                       @Override//          
                       public void actionPerformed(ActionEvent e) {//                
                           System.out.println(e.getActionCommand());//        
                       }//        });      
                        
                        myListen mylisten = new myListen();         
                        button1.addActionListener(mylisten);        
                        button2.addActionListener(mylisten);      
                        
                        frame.pack();       
                        frame.setVisible(true);      
                        windowClose(frame);    
                    }   
                                              
                                              //关闭
                           public static void windowClose(Frame frame){           
                               frame.addWindowListener(new WindowAdapter() {     
                                   @Override          
                                   public void windowClosing(WindowEvent e) {                
                                       System.exit(0);            
                                       super.windowClosing(e);            }        });    }}
                                              
                                              //再实现一个监听类就OK了
                   class myListen implements ActionListener {    
                       @Override   
                       public void actionPerformed(ActionEvent e) {            
                           if(e.getActionCommand()=="start"){            
                               System.out.println("66666666");      
                           }else {           
                               System.out.println("12345678");        }  
                       }}

代码格式多个类

public class Test1 {  
    public static void main(String[] args) {       
        new myFrame();       
        //都在其他类实现   
    }}

class myFrame extends Frame{//接口,还是继承要弄明白    
    public myFrame() {        
        TextField textField = new TextField();       
        add(textField);       
        
        //必须new一个监听对象,自己写监听类    
        MyListen myListen1 = new MyListen();     
        textField.addActionListener( myListen1);         
        //设置替换编码       
        textField.setEchoChar('*');     
        pack();       
        setVisible(true);    }}

class MyListen implements ActionListener{    
    @Override   
    public void actionPerformed(ActionEvent e) {    
        TextField field =  (TextField) e.getSource();   
        System.out.println(field.getText());       
        //重置文本框内容        
        field.setText("");    }}

计算器

public class Calc {  
    public static void main(String[] args) {      
        new Calculator();    }
}

class Calculator extends Frame{  
    public Calculator(){     
        TextField textField1 = new TextField(10);       
        TextField textField2 = new TextField(10);     
        TextField textField3 = new TextField(20);    
        Label label = new Label("+");      
        Button button = new Button("=");    
        setLayout(new FlowLayout());       
        add(textField1);      
        add(label);        
        add(textField2);    
        add(button);      
        add(textField3);  
        
        //挂监听,直接监文本内容,文本框就不用挂监听了
        button.addActionListener(new myListen2(textField1,textField2,textField3));       
        pack();        
        setVisible(true);    }}

class  myListen2 implements ActionListener{   
    private TextField num1,num2,num3;  
    public  myListen2(TextField num1,TextField num2,TextField num3) {  
        this.num1 = num1;      this.num2 = num2;      this.num3 = num3;    } 
    
    //必须在自动的这个类里实现,上面的只是接受数据   
    @Override   
    public void actionPerformed(ActionEvent e) {  
        int n1 = Integer.parseInt(num1.getText()) ;    
        int n2 = Integer.parseInt(num2.getText()) ;       
        num3.setText(""+(n1+n2));    }}

更加面向对象

public class Calc {
    public static void main(String[] args) {
        new Calculator();
    }
}

class Calculator extends Frame{
    TextField num1,num2,num3;
    public Calculator(){

         num1 = new TextField(10);
         num2 = new TextField(10);
         num3 = new TextField(20);

        Label label = new Label("+");
        Button button = new Button("=");
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        //直接把计算器这个类丢进去,this
       button.addActionListener(new myListen2(this));

        pack();
        setVisible(true);
    }
}
class  myListen2 implements ActionListener{

     Calculator cal = null;
     public  myListen2(Calculator cal) {
      this.cal = cal;

    }

    //必须在自动的这个类里实现,上面的只是接受数据
    @Override
    public void actionPerformed(ActionEvent e) {

        int n1 = Integer.parseInt(cal.num1.getText()) ;
        int n2 = Integer.parseInt(cal.num2.getText()) ;
        cal.num3.setText(""+(n1+n2));
    }
}

内部类写才是yyds

最大好处,畅通的访问外部类的属性和方法。更好包装,省代码,但不能外用
public class Calc {
    public static void main(String[] args) {
        new Calculator();
    }
}

class Calculator extends Frame{
    TextField num1,num2,num3;
    public Calculator(){

         num1 = new TextField(10);
         num2 = new TextField(10);
         num3 = new TextField(20);

        Label label = new Label("+");
        Button button = new Button("=");
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

       button.addActionListener(new myListen2());
       
        pack();
        setVisible(true);
    }
    private  class  myListen2 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {

            int n1 = Integer.parseInt(num1.getText()) ;
            int n2 = Integer.parseInt(num2.getText()) ;
            num3.setText(""+(n1+n2));
        }
    }
}

鼠标监听

public class mouseListen {

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

class myFrame2 extends Frame{

    //存鼠标的点
    ArrayList points = new ArrayList();
    public myFrame2(){
        setBounds(200,200,400,400);
           this.addMouseListener(new MouseListener());
           setVisible(true);

    }


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

    private class MouseListener extends MouseAdapter{
        @Override
        public void mouseClicked(MouseEvent e) {
            //e 是object类,转换为frame类
            //Frame myframe  = (Frame) e.getSource();
            //自带点的位置的类
          points.add(new Point(e.getX(),e.getY()));
           repaint();

              //new Point(e.getX(),e.getY());
        }
    }

窗口监听

内部类变为匿名内部类

内部类

public class windowListen {
    public static void main(String[] args) {

         new myWindow();
    }
}
class myWindow extends Frame {
    public myWindow() {
        setBounds(100,100,200,200);
        setBackground(Color.cyan);
        setVisible(true);
        addWindowListener(new myWindowListen());

    }

    private class myWindowListen extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false);
            System.exit(0);
        }
    }
}

匿名内部类

public class windowListen {
    public static void main(String[] args) {

         new myWindow();
    }
}
class myWindow extends Frame {
    public myWindow() {
        setBounds(100,100,200,200);
        setBackground(Color.cyan);
        setVisible(true);
        
       // addWindowListener(new myWindowListen());
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);
                System.exit(0);
            }
        });

    }
}

键盘监听

监听用匿名内部类,省事,省代码

public class keyListen {
    public static void main(String[] args) {
//     int i=1111;
//        System.out.println( Integer.toString(i,16));

        new myKeyListener();

    }
}
class  myKeyListener extends Frame {
    public myKeyListener() {
        setBounds(100,300,400,400);
        setBackground(Color.cyan);
        setVisible(true);
        
        
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(0x26==e.getKeyCode()){
                    System.out.println("冷月葬花魂");
                }
                else{
                    System.out.println("今夕何夕");
                }
            }
        });

    }
}

Swing

JLable 标签

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

}
class MyFrame extends JFrame{
    public void init(){
        this.setBounds(100,100,200,400);
        this.setVisible(true);

        JLabel label = new JLabel("白也诗无敌",SwingConstants.CENTER);
       this.add(label);

        //获得一个容器
        Container con = this.getContentPane();
        con.setBackground(Color.YELLOW);
       //关闭事件
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

标签+图片

URL url = imageDemo.class.getResource(“1630545325593.jpg”);

获得当前class中的图片

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

public class imageDemo extends JFrame{
    public imageDemo() {

        JLabel label = new JLabel();
        //读图片
        URL url = imageDemo.class.getResource("1630545325593.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

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

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

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

}

弹窗

  1. 关于Swing 必需要有容器 //JFrame 放东西,容器
    Container contentPane = this.getContentPane();
  2. 绝度布局
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class diaLog extends JFrame {


    public diaLog() {
       this.setVisible(true);
       this.setBounds(100,100,200,200);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


       //JFrame 放东西,容器
        Container contentPane = this.getContentPane();

        // 绝度布局
       // contentPane.setLayout(null);


        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(100,100,50,50);

        button.addActionListener(new ActionListener() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                new myDialog();
            }
        });
        contentPane.add(button);

    }

    public static void main(String[] args) {
        new diaLog();
    }
    }
class myDialog extends JDialog{
    public myDialog() {
        this.setVisible(true);
        this.setBounds(200,200,200,200);

        Container contentPane1 = this.getContentPane();
      //  contentPane1.setLayout(null);
        contentPane1.add(new JLabel("不知日月"));
    }
}

按钮

图片按钮

public class jButton extends JFrame {
    public jButton() {
        Container contentPane = getContentPane();
        
        //获得图片且变为图标
        URL resource = jButton.class.getResource("1630545325593.jpg");
        ImageIcon imageIcon = new ImageIcon(resource);

        //将图片加到按钮上
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);

        contentPane.add(jButton);

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

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

多选框按钮

JCheckBox jCheckBox2 = new JCheckBox(“BBB”);

public class JButtonDemo extends JFrame {

    public JButtonDemo() {
        Container contentPane = getContentPane();

        //
        JCheckBox jCheckBox1 = new JCheckBox("AAA");
        JCheckBox jCheckBox2 = new JCheckBox("BBB");

        contentPane.add(jCheckBox1,BorderLayout.NORTH);
        contentPane.add(jCheckBox2,BorderLayout.SOUTH);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(100,100);


    }

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

列表与下拉列表

public class ComboboxDemo01 extends JFrame {

    public ComboboxDemo01() {
        Container contentPane = getContentPane();

        JComboBox jComboBox = new JComboBox();
        jComboBox.addItem("元迎叹惜");
        jComboBox.addItem("妙黛");
        jComboBox.addItem("王熙凤");

        contentPane.add(jComboBox);



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

    }

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















public class ComboboxDemo02 extends JFrame {

    public ComboboxDemo02() {
        Container contentPane = getContentPane();

        Vector objects = new Vector();

        JList jList = new JList(objects);

        objects.add("liBai");
        objects.add("duFu");

        contentPane.add(jList);




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

    }

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

文本框

密码文本框与此差不多

password罢了

public class TextDemo extends JFrame {

    public TextDemo() {
        Container contentPane = getContentPane();

        JTextField jTextField = new JTextField("12345");

        contentPane.add(jTextField);

面板

public TextDemo() {
        Container contentPane = getContentPane();

       //文本域
        JTextArea jTextArea = new JTextArea(20,50);
        jTextArea.setText("123445");

        //Scroll面板可以滚动

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




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

    }

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