太强了!所有GUI编程笔记里面我愿称你为最强,建议先收藏再看!

简介

Gui的核心技术:Swing AWT

因为界面不美观。

需要jre环境!

为什么我们要学习?

   1. 可以写出自己心中想要的一些小工具
   2. 工作时候,也可能需要维护到swing界面,概率极小!
   3. 了解MVC架构,了解监听!

AWT

组件和容器

Frame

public class newawttest {
    public static void main(String[] args) {
        //创建一个Frame类
        Frame frame = new Frame("这是个测试");

        //设置窗口可见化
        frame.setVisible(true);

        //设置窗口尺寸
        frame.setSize(400,300);

        //设置背景颜色
        frame.setBackground(new Color(203, 33, 33));

        //设置初始位置
        frame.setLocation(200,200);

        //设置大小固定
        frame.setResizable(false);

    }


}

import java.awt.*;

public class MyawtText_2 {
    public static void main(String[] args) {
        Newawt newawt1 = new Newawt(100,100,200,200,Color.BLUE);
        Newawt newawt2 = new Newawt(300,100,200,200,Color.YELLOW);
        Newawt newawt3 = new Newawt(100,300,200,200,Color.RED);
        Newawt newawt4 = new Newawt(300,300,200,200,Color.PINK);
    }
}

class Newawt extends Frame {
    static int id = 0;
    public Newawt(int x,int y,int w,int h,Color color) {
        super("窗口"+(++id));
        setVisible(true);
        setBounds(x,y,w,h);
        setBackground(color);

    }
}

面板Panel

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

public class newawttest {
    public static void main(String[] args){
        Frame frame=new Frame();
        Panel panel=new Panel();
        Panel panel2 = new Panel();
        //设置布局
        frame.setLayout(null);
        //坐标
        frame.setBounds(300,300,500,500);
//        frame.setBounds(0,0,0,0);
        frame.setBackground(new Color(40,161,35));

        panel2.setBounds(0,0,200,200);
        panel2.setBackground(new Color(255, 241, 190));
        frame.add(panel2);
        //paneL 设置坐标,相对于frame
        panel.setBounds(0,0,300,300);
        panel.setBackground(new Color(193,15,60));
        //frame.add(panel) ;
        frame.add(panel);


        //窗口可视化
        frame.setVisible(true);

        //适配器模式,把原有的接口换成继承类
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

}

布局管理器

流式布局

import java.awt.*;


public class newawttest {
    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");
        //设置流式布局
        frame.setLayout(new FlowLayout());        //默认居中
//        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
//        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//        frame.setLayout(new FlowLayout(FlowLayout.LEADING));
//        frame.setLayout(new FlowLayout(FlowLayout.TRAILING));


        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setSize(400,400);
        frame.setVisible(true);


    }
}

东西南北中

import java.awt.*;


public class newawttest {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //按钮
        Button west = new Button("West");
        Button East = new Button("East");
        Button North = new Button("North");
        Button South = new Button("South");
        Button Center = new Button("Center");

        frame.add(west,BorderLayout.WEST);
        frame.add(East,BorderLayout.EAST);
        frame.add(North,BorderLayout.NORTH);
        frame.add(South,BorderLayout.SOUTH);
        frame.add(Center,BorderLayout.CENTER);

        frame.setVisible(true);
        frame.setSize(400,400);
        
    }
}

表格布局

import java.awt.*;


public class newawttest {
   public static void main(String[] args) {
       Frame frame = new Frame();
       //按钮
       Button but1 = new Button("but1");
       Button but2 = new Button("but2");
       Button but3 = new Button("but3");
       Button but4 = new Button("but4");
       Button but5 = new Button("but5");
       Button but6 = new Button("but6");


       frame.setLayout(new GridLayout(2,3));

       frame.add(but1);
       frame.add(but2);
       frame.add(but3);
       frame.add(but4);
       frame.add(but5);
       frame.add(but6);

       frame.pack();   //Java语法,自动分布大小
       frame.setVisible(true);


   }
}

练习题

import java.awt.*;


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

        frame.setVisible(true);
        frame.setBounds(400,300,300,300);
//        frame.pack();
        frame.setBackground(Color.BLUE);
        frame.setLayout(new GridLayout(2, 1));

        //面板
        Panel panel1 = new Panel(new GridLayout(2,1));
        Panel panel2 = new Panel(new GridLayout(2,2));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new BorderLayout());

        //上
        panel3.add(new Button("East-1"),BorderLayout.EAST);
        panel3.add(new Button("West-1"),BorderLayout.WEST);
        panel1.add(new Button("top"));
        panel1.add(new Button("down"));
        panel3.add(panel1,BorderLayout.CENTER);

        //下
        panel4.add(new Button("East-2"),BorderLayout.EAST);
        panel4.add(new Button("West-2"),BorderLayout.WEST);
        panel2.add(new Button("top-1"));
        panel2.add(new Button("top-1"));
        panel2.add(new Button("down-1"));
        panel2.add(new Button("down-2"));
        panel4.add(panel2,BorderLayout.CENTER);

        frame.add(panel3);
        frame.add(panel4);
        

    }
}

事件监听

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.EventListener;


public class newawttest {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame();
        Button button = new Button("button");

        //因为addActionListener()需要一个ActionListener,所以需要构造一个ActionListener。
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

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

        frame.setVisible(true);

        ExitWindows(frame);

        

    }



    private static void ExitWindows(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("Hello,World!");
    }
}

多个按钮共享一个事件

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 newawttest {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        // 可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值!
        // 可以多个按钮只写一个临听类
        button1.addActionListener(new MyAction());
        button2.addActionListener(new MyAction());

        button2.setActionCommand("This is stop!");

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

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

        ExitWindows(frame);

    }



    private static void ExitWindows(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) {
        System.out.println("输出:msg=>"+e.getActionCommand());//获得按钮信息
    }
}

输入框 TextField 监听

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

public class TextFieldaaa {
    public static void main(String[] args) {
        //启动
        new MyFrame();
        new newawttest().ExitWindows(new MyFrame());

    }
}

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

        //监听这个文本框输入的文字
        MyAction2 myAction2 = new MyAction2();
        //按下Enter就会触发输入框的事件
        textField.addActionListener(myAction2);

        textField.setEchoChar('*');     //设置替换编码
        pack();
        setVisible(true);
    }
}

class MyAction2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();      //获得一些资源,返回一个对象
        System.out.println(field.getText());                //得到输入框文本
        field.setText("");                  //每次输入完清空
    }
}

简易计算器,组合和内部类

//初代码
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 TextCalc {
    public static void main(String[] args) {
        new CalcLater();
    }
}
class CalcLater extends Frame {
    public CalcLater(){
        //三个文本框
        TextField field1 = new TextField(10);
        TextField field2 = new TextField(10);
        TextField field3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalcLaterListener(field1,field2,field3));
        //一个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());

        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);

        setVisible(true);
        pack();

//        ExitWindows(new CalcLater());

    }

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

class MyCalcLaterListener implements ActionListener {
    private TextField num1,num2,num3;

    public MyCalcLaterListener(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));
        //将前两个数清空
        num1.setText("");
        num2.setText("");
    }
}

//改造完全面向对象
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 TextCalc {
    public static void main(String[] args) {
        new CalcLater().LoadFrame();
    }
}
class CalcLater extends Frame {

    TextField num1,num2,num3;
    Button button;
    Label label;

    //加载计算器
    public void LoadFrame(){
        //三个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //一个按钮
        button = new Button("=");
        button.addActionListener(new MyCalcLaterListener(this));
        //一个标签
        label = new Label("+");

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

        setVisible(true);
        pack();

        this.ExitWindows(this);
    }

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

//监听类
class MyCalcLaterListener implements ActionListener {
    CalcLater calcLater = null;
    public MyCalcLaterListener(CalcLater calcLater) {
        this.calcLater = calcLater;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //传三个数进来
        int n1 = Integer.parseInt(calcLater.num1.getText());
        int n2 = Integer.parseInt(calcLater.num2.getText());
        //将运算结果放到第三个框
        calcLater.num3.setText(""+(n1+n2));
        //将前两个数清空
        calcLater.num1.setText("");
        calcLater.num2.setText("");
    }
}

内部类最大的好处,就是可以畅通无阻地访问外部类的属性和方法

内部类写法↓

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 CalcLater extends Frame {

    public static void main(String[] args) {
        new CalcLater().LoadFrame();
    }
    
    TextField num1,num2,num3;
    Button button;
    Label label;

    //加载计算器
    public void LoadFrame(){
        //三个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //一个按钮
        button = new Button("=");
        button.addActionListener(new MyCalcLaterListener());
        //一个标签
        label = new Label("+");

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

        setVisible(true);
        pack();

        this.ExitWindows(this);

    }

    private class MyCalcLaterListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //传三个数进来
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            //将运算结果放到第三个框
            num3.setText(""+(n1+n2));
            //将前两个数清空
            num1.setText("");
            num2.setText("");
        }
    }
    public void ExitWindows(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

画笔

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

public class TestMyPrints {
    public static void main(String[] args) {
        new MyPrints().LoadFrame();
    }
}

class MyPrints extends Frame{

    public void LoadFrame(){
        setVisible(true);
        setBounds(200,200,600,500);
        
    }
    
    @Override
    public void paint(Graphics g) {
        //super.paint(g);

        //设置画笔颜色
        g.setColor(Color.CYAN);
        //填充一个实心圆
        g.fillOval(100,100,100,100);

        //画笔用完还原为最初的颜色
        g.setColor(Color.BLACK);     

    }

}

鼠标监听

//画板实例
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;

public class MorseListenerTest {


    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

class MyFrame extends Frame{
    //需要画笔,需要监听鼠标,需要集合存点
    ArrayList points ;

    public MyFrame(String title){
        super(title);
        setBounds(200,200,600,400);
        setVisible(true);

        //存鼠标的点
        points = new ArrayList();
        //鼠标监听器
        this.addMouseListener(new MyListener());
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    //把点添加进去
    public void addPoint(Point point){
        points.add(point);
    }


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

    //鼠标监听
    private class MyListener extends MouseAdapter{
        //鼠标点击

        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame = (MyFrame)e.getSource();
            //这里我们点击的时候就会产生一个点
            myFrame.addPoint(new Point(e.getX(),e.getY()));

            //每次点击鼠标需要重画一遍
            myFrame.repaint();
        }
    }
}

窗口监听

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

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

class MyFrame extends Frame{
    public MyFrame(){

        setVisible(true);
        setBackground(Color.CYAN);
        setBounds(200,200,400,400);


    }

    private class MyWindowsListenter extends WindowAdapter{
        //关闭窗口
        @Override
        public void windowClosing(WindowEvent e) {
            super.windowClosing(e);
        }
        //窗口被激活
        @Override
        public void windowActivated(WindowEvent e) {
            super.windowActivated(e);
        }
    }


}


键盘监听


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(){
        setBackground(Color.blue);
        setBounds(100,100,200,400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //获取输入键的编码
                System.out.println(e.getKeyCode());
            }
        });
    }
}

Swing

窗口,面板

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

        //获得一个容器
        Container contentPane = jFrame.getContentPane();
        contentPane.setBackground(Color.YELLOW);

        JLabel jLabel = new JLabel("欢迎来到笨蛋的窗口界面");
        //设置文本居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        jFrame.add(jLabel);
        //关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();
    }
}

弹窗

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.setBounds(100,100,700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

        contentPane.setBackground(Color.lightGray);

        //按钮
        JButton button = new JButton("这是个按钮");
        button.setBounds(30,30,100,100);
        button.setBackground(Color.BLUE);
        //点击这个按钮的时候弹出一个弹窗
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDiolog();
            }
        });
        contentPane.add(button);
    }

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

}


class MyDiolog extends JDialog{
    public MyDiolog() {
        this.setVisible(true);
        this.setBounds(100,100,300,300);
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//        默认有关闭事件

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

        JLabel label = new JLabel("这是个弹窗");
        label.setBounds(10,10,400,400);
        container.add(label);
    }
    
}

标签

package com.Miotsuki.SwingTest;

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

public class ImageIconDemo extends JFrame{
    public ImageIconDemo(){


        //获取图片地址
        JLabel label = new JLabel("");
        URL url = ImageIconDemo.class.getResource("蛋糕.PNG");
        ImageIcon imageIcon = new ImageIcon(url);

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

        Container container = this.getContentPane();
        container.setBackground(new Color(196, 183, 255));
        container.add(label);

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

    }

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

面板

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


public class JPanelDemo2 extends JFrame {
    public JPanelDemo2() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2, 1, 10, 10));
        //后面的参数的意想,间原
        JPanel panel1 = new JPanel(new GridLayout(1, 3));
        JPanel panel2 = new JPanel(new GridLayout(1, 2));
        JPanel panel3 = new JPanel(new GridLayout(2, 1));
        JPanel panel4 = new JPanel(new GridLayout(3, 2));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));

        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

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

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

JScrollPanel

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

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container contentPane = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 20);
        textArea.setText("欢迎使用:");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        contentPane.add(scrollPane);
        
        this.setVisible(true);
        this.setBounds(100,100,300,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

}

按钮

普通按钮

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

public class JButtonDemo1 extends JFrame {
    public JButtonDemo1(){
        //容器
        Container container = this.getContentPane();
        
        //获取图片位置
        URL resource = JButtonDemo1.class.getResource("蛋糕.png");
        Icon icon = new ImageIcon(resource);
        
        //设置一个按钮
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("这是个按钮,快按吧");
        
        container.add(button);
        setBounds(100,100,500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

单选按钮

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

public class JButtonDemo2 extends JFrame {
    public JButtonDemo2(){
        //容器
        Container container = this.getContentPane();
        //获取图片位置
        URL resource = JButtonDemo1.class.getResource("蛋糕.png");
        Icon icon = new ImageIcon(resource);

        JRadioButton button1 = new JRadioButton("JRadioButton1");
        JRadioButton button2 = new JRadioButton("JRadioButton2");
        JRadioButton button3 = new JRadioButton("JRadioButton3");

        ButtonGroup group = new ButtonGroup();
        group.add(button1);
        group.add(button2);
        group.add(button3);

        container.add(button1,BorderLayout.NORTH);
        container.add(button2,BorderLayout.CENTER);
        container.add(button3,BorderLayout.SOUTH);


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

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

复选按钮

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

public class JButtonDemo3 extends JFrame {
    public JButtonDemo3(){
        //容器
        Container container = this.getContentPane();
        //获取图片位置
        URL resource = JButtonDemo1.class.getResource("蛋糕.png");
        Icon icon = new ImageIcon(resource);

        //多选按钮
        JCheckBox checkBox1 = new JCheckBox("JCheckBox1");
        JCheckBox checkBox2 = new JCheckBox("JCheckBox2");

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


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

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

列表

下拉菜单

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

public class ComboBosDemo01 extends JFrame{
    public ComboBosDemo01(){
        //容器
        Container container = this.getContentPane();

        JComboBox comboBox = new JComboBox();
        comboBox.addItem(null);
        comboBox.addItem("联系人");
        comboBox.addItem("好友");
        comboBox.addItem("黑名单");

        container.add(comboBox);

        //可视化,窗口大小,关闭事件
        setVisible(true);
        setBounds(100,100,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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



列表框

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

public class ComboBosDemo02 extends JFrame {
    public ComboBosDemo02(){
        //容器
        Container container = this.getContentPane();

//        String[] contents = {"1","2","3"};

        Vector contents = new Vector();
        JList jlist = new JList(contents);

        contents.add("zhangsan");
        contents.add("lisi");
        contents.add("wangwu");


        container.add(jlist);
        //可视化,窗口大小,关闭事件
        setVisible(true);
        setBounds(100,100,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

文本框

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

public class TextTestDemo01 extends JFrame {
    public TextTestDemo01(){
        //容器
        Container container = this.getContentPane();
        //文本框
        JTextField textField = new JTextField("Hello",20);
        JTextField textField2 = new JTextField("World");
        //密码框
        JPasswordField passwordField = new JPasswordField();

        container.add(textField,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);
        container.add(passwordField,BorderLayout.CENTER);

        //可视化,窗口大小,关闭事件
        setVisible(true);
        setBounds(100,100,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

文本域

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

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container contentPane = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 20);
        textArea.setText("欢迎使用:");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        contentPane.add(scrollPane);
        
        this.setVisible(true);
        this.setBounds(100,100,300,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

} 
1(){
//容器
Container container = this.getContentPane();
//文本框
JTextField textField = new JTextField(“Hello”,20);
JTextField textField2 = new JTextField(“World”);
//密码框
JPasswordField passwordField = new JPasswordField();

    container.add(textField,BorderLayout.NORTH);
    container.add(textField2,BorderLayout.SOUTH);
    container.add(passwordField,BorderLayout.CENTER);

    //可视化,窗口大小,关闭事件
    setVisible(true);
    setBounds(100,100,500,500);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new TextTestDemo01();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
}


- 文本域

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

public class JScrollDemo extends JFrame {

public JScrollDemo(){
    Container contentPane = this.getContentPane();

    //文本域
    JTextArea textArea = new JTextArea(20, 20);
    textArea.setText("欢迎使用:");

    //Scroll面板
    JScrollPane scrollPane = new JScrollPane(textArea);
    contentPane.add(scrollPane);
    
    this.setVisible(true);
    this.setBounds(100,100,300,400);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

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

}

# 最后
在文章的最后作者为大家整理了很多资料!包括java核心知识点+全套架构师学习资料和视频+一线大厂面试宝典+面试简历模板+阿里美团网易腾讯小米爱奇艺快手哔哩哔哩面试题+Spring源码合集+Java架构实战电子书等等!

你可能感兴趣的:(java程序员后端)