GUI编程简介

组件

       窗口

  弹窗

  面板

  文本框

  列表框

  按钮

  图片

  监听事件

  鼠标

  键盘事件

  破解工具

1 简介

GUI:Swing  AWT

  1 界面不美观

  2 需要jre环境

学习理由

  1 可以写出自己心中想要的的一些小工具

  2 工作时也可能维护到Swing界面

  3 了解MVC,了解监听!

2 AWT

    2.1 AWT介绍

     1  包含很多类和接口!GUI !

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

        3  java.awt 

          4

            

 

  2.2 组件和容器

    1 单个窗口


package com.guo.Lesson01;

import java.awt.*;
public class TestFrame {
    public static void main(String[] args) {
        Frame frame = new Frame("我的第一个图形界面窗口!");
        //设置窗口可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(111,23,255));
        //弹出的初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
    }
}

运行结果:

       2 多个窗口

package com.guo.Lesson01;

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        //ctrl+d 快速复制下一行
      MyFrame frame01 =  new MyFrame(100,100,200,200,Color.BLACK);
      MyFrame frame02 =  new MyFrame(300,100,200,200,Color.BLUE);
      MyFrame frame03 =  new MyFrame(100,300,200,200,Color.YELLOW);
      MyFrame frame04 =  new MyFrame(300,300,200,200,Color.RED);
    }
}
class MyFrame extends Frame{
    static int id = 0;
    public MyFrame(){

    };
    public MyFrame(int x,int y,int w,int h,Color color){
        super("MyFrame"+(++id));
        setBounds(x,y,w,h);
        setBackground(color);
        setVisible(true);
    };


}

运行结果:

   3 Panel 面板

package com.guo.Lesson01;

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();
        MyPanel panel1 = new MyPanel(50,50,100,50,new Color(193, 15 ,60));
        MyPanel panel2 = new MyPanel(50,100,100,50,new Color(193, 100 ,100));
        MyPanel panel3 = new MyPanel(50,150,100,50,new Color(150, 150 ,150));
        MyPanel panel4 = new MyPanel(50,200,100,50,new Color(127, 0 ,123));
        //设置布局 *******添加panel的话一定要先设置为null******
        frame.setLayout(null);
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(40, 163, 15));
        //设置panel位置及背景颜色
        // panel.setBounds(50,50,400,50);
        // panel.setBackground(new Color(193, 15 ,60));
        //讲panel添加到frame上
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.add(panel4);
        frame.setVisible(true);
        //监听事件,监听窗口关闭时间system.exit(0);
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
        });
    }
}
class MyPanel extends Panel{
     static  int id  = 0;
     public MyPanel(int x,int y ,int w,int h ,Color color){
         setBounds(x,y,w,h);
         setBackground(color);
     }
}

运行结果:

  2.3 布局管理器 

         流式布局

      

package com.guo.Lesson01;

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

public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        Button button = new Button("button");
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        frame.setBounds(300,300,500,500);
        panel.setBounds(100,100,300,300);
        panel.setBackground(Color.CYAN);
        //设置流式布局
        frame.setLayout(null);
        //panel.setLayout(new FlowLayout());
        //panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        frame.setVisible(true);
        frame.add(panel);
        panel.add(button);
        panel.add(button1);
        panel.add(button2);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

 

 

         东西南北中

package com.guo.Lesson01;

import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        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("North");
        East.setBackground(Color.CYAN);
        West.setBackground(Color.LIGHT_GRAY);
        South.setBackground(Color.PINK);
        North.setBackground(Color.MAGENTA);
        Center.setBackground(Color.yellow);
        frame.setVisible(true);
        frame.setSize(300,300);
        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);
    }
}

 

 

         表格布局

package com.guo.Lesson01;

import java.awt.*;

public class TestGrid {
    public static void main(String[] args) {
        Frame frame  = new Frame();
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        frame.setLayout(new GridLayout(3,2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        //窗口自动填充,无需设置大小 JAVA函数!
        frame.pack();
        frame.setVisible(true);
    }
}

 总结:

  1 frame 是一个顶级窗口。

       2 panel 不能单独显示,需要放置在容器中

       3 大小,颜色,可见性,监听! 

       4 布局管理器

     流式布局

    东西南北中

    表格

  2.4 事件监听

    事件监听:监听当某个事件发生时,进行相应动作

 

package com.guo.Lesson01;

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 TestListener {
    public static void main(String[] args) {
        Frame frame =  new Frame("frame");
        Button button = new Button("button");
        MyActionListener ActionListener = new MyActionListener();
        button.addActionListener(ActionListener);
        frame.setBounds(500,500,300,300);
        frame.setVisible(true);
        frame.add(button);
        WindowClose(frame);
    }
    private static void WindowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//事件监听 注意implements与extends的区别
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮进行了点击动作!");
    }
}

 

多个按钮共享一个事件

package com.guo.Lesson01;

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 TestAction2 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认值
        //可以多个按钮只写一个监听类,减少重复操作
        button2.setActionCommand("new_stop");
        MyAction myAction = new MyAction();
        button1.addActionListener(myAction);
        button2.addActionListener(myAction);
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);
        frame.setVisible(true);
        frame.setBounds(300,300,400,400);
        WindowClose(frame);
    }
    private static void WindowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyAction implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand 获取按钮信息
        System.out.println("按钮被点击了:"+e.getActionCommand());
        if(e.getActionCommand().equals("start")){
            System.out.println("this is start!");
        }
    }
}

   2.5 文本框TextField监听

  

package com.guo.Lesson01;

import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;

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

public class TestTextField {
    public static void main(String[] args) {
        new MyFrameS();
    }
}
class  MyFrameS extends Frame {
    public MyFrameS() {
        TextField textField = new TextField();
        textField.addActionListener(new MyAction01());
        textField.setEchoChar('*');
        setVisible(true);
        add(textField);
        pack();
    }
}
class MyAction01 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource(); //e.getSource() 获取资源,返回Object类型
        System.out.println(textField.getText());
        textField.setText("");
    }
}

   2.6 简易计算器

    OOP原则:组合,大于继承!

    基础代码: 

package com.guo.Lesson01;

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

public class TestCal {
    public static void main(String[] args) {
            new add();
    }

}
class  add extends Frame{
    public  add(){
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);
        Label label = new Label("+");
        Button button = new Button("=");
        button.addActionListener(new MyListener(num1,num2,num3));
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}
class MyListener implements ActionListener{
    private TextField num1;
    private TextField num2;
    private TextField num3;
    public MyListener(TextField num1,TextField num2,TextField num3){
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       int X = Integer.parseInt(num1.getText());
       int Y = Integer.parseInt(num2.getText());
       num3.setText(String.valueOf(X+Y));
       num1.setText("");
       num2.setText("");
    }
}

      面向对象:

package com.guo.Lesson01;

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

public class TestCalNew {
    public static void main(String[] args) {
        new addNew().LoadAddNew();
    }
}
class  addNew extends Frame {
    //属性
    TextField num1;
    TextField num2;
    TextField num3;
    //方法
    public void LoadAddNew(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Label label = new Label("+");
        Button button = new Button("=");
        button.addActionListener(new MyListeners(this));
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}
class MyListeners implements ActionListener {
    private addNew addNew;
    public MyListeners(addNew addNew){
        this.addNew = addNew;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int X = Integer.parseInt(addNew.num1.getText());
        int Y = Integer.parseInt(addNew.num2.getText());
        addNew.num3.setText(String.valueOf(X+Y));
        addNew.num1.setText("");
        addNew.num2.setText("");
    }
}
  内部类,最优选择
package com.guo.Lesson01;

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

public class TestCalNewInner {
    public static void main(String[] args) {
        new addNewInner().LoadAddNew();
    }
}
class  addNewInner extends Frame {
    //属性
    TextField num1;
    TextField num2;
    TextField num3;
    //方法
    public void LoadAddNew(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Label label = new Label("+");
        Button button = new Button("=");
        button.addActionListener(new MyListenersInner());
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
    //内部类 最大的好处是可以畅通无阻的访问外部类的属性和方法
    private class MyListenersInner implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int X = Integer.parseInt(num1.getText());
            int Y = Integer.parseInt(num2.getText());
            num3.setText(String.valueOf(X+Y));
            num1.setText("");
            num2.setText("");
        }
    }
}

  2.7 画笔 

package com.guo.Lesson02;

import java.awt.*;

public class TestFrameDraw {
    public static void main(String[] args) {
        new draw().LoadFrame();
    }
}
class draw extends Frame{

    public void LoadFrame(){
        setVisible(true);
        setBounds(300,300,600,400);
    }

    @Override
    public  void paint(Graphics g){
        g.setColor(Color.RED);
        g.drawOval(100,100,100,100);
        g.setColor(Color.yellow);
        g.fillRect(100,200,200,100);
    }

}

   2.8 鼠标监听

package com.guo.Lesson02;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
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(400,400,600,400);
        //存储鼠标的点击坐标
        points = new ArrayList();
        setVisible(true);
        //鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());
    }

    @Override
    public  void paint(Graphics g){
        //画画,监听鼠标的事件
        Iterator it = points.iterator();
        while (it.hasNext()){
            Point point = (Point) it.next();
            g.setColor(Color.yellow);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    //添加一个点到界面上
    public void addPoint(Point point) {
        points.add(point);
    }

    //适配器模式
    private class MyMouseListener extends MouseAdapter{
        //鼠标按下,弹起,按住不放
        @Override
        public void mouseClicked(MouseEvent e) {
           MyFrame frame = (MyFrame) e.getSource();
           //点击鼠标的时候会产生一个坐标,在窗口记录鼠标点击的坐标
           //frame.addPoint(new Point(e.getX(),e.getY()));
            frame.points.add(new Point(e.getX(),e.getY()));
            //每次点击鼠标重新画一次
            frame.repaint();
        }
    }
}

   2.9 窗口监听

package com.guo.Lesson02;

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

public class WindowListener {
    public static void main(String[] args) {
        new MyFrame2("窗口监听事件");
    }
}
class MyFrame2 extends Frame{
    public MyFrame2(String title){
        super(title);
        setBounds(400,400,600,400);
        setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            //匿名内部类,推荐
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible(true); //设置窗口不可见
                System.exit(130);
            }
            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("窗口已经激活!");
            }

        });
    }

}

   2.10 键盘监听

 

package com.guo.Lesson02;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyListener {
    public static void main(String[] args) {
            new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(100,100,100,100);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
               char c =  e.getKeyChar();
                System.out.println(c);
                if (c == 'c'){
                    System.exit(0);
                }
            }
        });
    }

}

 3 Swing

  3.1 窗口、面板

package com.guo.Lesson03;

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

public class JFrameDemo {

    public static void main(String[] args) {
        new MyJFrame().init();
    }
}
class MyJFrame extends JFrame {
    //初始化
    public void init() {
        this.setVisible(true);
        this.setBounds(400,400,600,400);
        JLabel jLabel = new JLabel("这是一个文本!");
        this.add(jLabel);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //与Frame的区别在需要显示定义一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.yellow);
        //关闭事件
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

  3.2 弹窗

package com.guo.Lesson03;

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

//主窗口
public class JDialogDemo extends JFrame{

    public JDialogDemo(){
        this.setVisible(true);
        this.setBounds(500,500,600,300);
        //JFrame容器,放东西
        Container container =  this.getContentPane();
        //绝对布局
        container.setLayout(null);
        JButton jButton = new JButton("弹窗按钮");
        jButton.setBounds(100,100,200,100);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });
        container.add(jButton);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JDialogDemo();
    }
}
//弹窗窗口
class MyDialog extends JDialog{
    public MyDialog(){
        //弹窗自带窗口关闭功能
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        //使用容器
        Container container =  this.getContentPane();
        JLabel jLabel = new JLabel("我的第一个弹窗");
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(jLabel);
    }
}

  3.3 标签

    ICON

package com.guo.Lesson03;

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

public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        //获取图片地址
        JLabel jLabel = new JLabel("ImageIconDemo");

        URL url = ImageIconDemo.class.getResource("zm.jpg");
        ImageIcon imageIcon = new ImageIcon(url);

        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

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

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

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

 

 

  3.4 面板

    JPanel  

package com.guo.Lesson04;

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(3,1));
        jPanel1.add(new JButton("button1"));
        jPanel1.add(new JButton("button2"));
        jPanel1.add(new JButton("button3"));
        container.add(jPanel1);
        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

}

    JScrollPane

  

package com.guo.Lesson04;

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

public class JScrollPaneDemo extends JFrame {
    public JScrollPaneDemo(){
        Container container = this.getContentPane();
        //文本域
        TextArea textArea =  new TextArea(20,50);
        textArea.setText("欢迎学习JAVA");
        //Scroll面板
        JScrollPane jPanelDemo =  new JScrollPane(textArea);
        container.add(jPanelDemo);
        this.setVisible(true);
        this.setBounds(300,300,300,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

效果:

 

 

  3.5 按钮

    普通按钮     

package com.guo.Lesson04;

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

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01(){
        Container container =  this.getContentPane();
        //获取图片的资源路径
        URL resource = JButtonDemo01.class.getResource("zmm.jpg");
        //将图片转换成图标
        Icon icon = new ImageIcon(resource);

        JButton button = new JButton();
        //将图标添加到按钮上
        button.setIcon(icon);
        //设置按钮的悬浮提示信息
        button.setToolTipText("这是一个悬浮提示信息");
        button.setSize(300,200);
        //将按钮添加到容器内,否则无法显示
        container.add(button);
        this.setVisible(true);
        this.setBounds(400,400,600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

 

     单选按钮

package com.guo.Lesson04;

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

public class JButtonDemo02 extends JFrame {
    public JButtonDemo02(){
        Container container =  this.getContentPane();
        //单选按钮
        JRadioButton jRadioButton01 = new JRadioButton("jRadioButton01");
        JRadioButton jRadioButton02 = new JRadioButton("jRadioButton02");
        JRadioButton jRadioButton03 = new JRadioButton("jRadioButton03");
        //按钮分组,如果单选按钮不添加到按钮组里面,则无法实现单选功能!!!
        ButtonGroup group = new ButtonGroup();
        group.add(jRadioButton01);
        group.add(jRadioButton02);
        group.add(jRadioButton03);
        //将按钮添加到容器内
        container.add(jRadioButton01,BorderLayout.NORTH);
        container.add(jRadioButton02,BorderLayout.CENTER);
        container.add(jRadioButton03,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setBounds(400,400,600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

     复选按钮 

package com.guo.Lesson04;

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

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

        //复选框
        JCheckBox jCheckBox01 = new JCheckBox();
        JCheckBox jCheckBox02 = new JCheckBox();
        JCheckBox jCheckBox03 = new JCheckBox();

        //设置表格布局
        container.setLayout(new GridLayout(3,1));
        container.add(jCheckBox01);
        container.add(jCheckBox02);
        container.add(jCheckBox03);

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

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

  3.6 列表

    下拉列表

    

package com.guo.Lesson06;

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

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

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

        container.add(comboBox);
        this.setVisible(true);
        this.setBounds(300,300,600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

   列表框

package com.guo.Lesson06;

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

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

        String[] content = {"aaa","bbb","ccc"};
        JList jList = new JList(content);

        container.add(jList);
        this.setVisible(true);
        this.setBounds(300,300,600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

 应用场景

     下拉框:选择地区,或者一些单个选项

    列表框:展示信息,一般是动态扩容!

  3.7 文本框

  • 文本框
package com.guo.Lesson06;

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

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

        JTextField jTextField = new JTextField("上边文本框");
        JTextField jTextField2 = new JTextField("下边文本框");
        container.add(jTextField,BorderLayout.NORTH);
        container.add(jTextField2,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setBounds(300,300,600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new TextDemo01();
    }
}
  • 密码框

 

package com.guo.Lesson06;

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

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

        JPasswordField jPasswordField = new JPasswordField();
        //设置前台展示的字符用*代替
        jPasswordField.setEchoChar('*');
        container.add(jPasswordField,BorderLayout.NORTH);
        this.setVisible(true);
        this.setBounds(300,300,600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

 

  • 文本域

 

package com.guo.Lesson06;

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

public class TextDemo03 extends JFrame {
    public TextDemo03 (){
        Container container =  this.getContentPane();
        JTextArea jTextArea = new JTextArea("欢迎回来");
        container.add(jTextArea,BorderLayout.NORTH);
        this.setVisible(true);
        this.setBounds(300,300,600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

 

posted @ 2021-08-31 16:00  小牛哞哞哞  阅读(95)  评论(0)    收藏  举报