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

28次阅读

共计 21124 个字符,预计需要花费 53 分钟才能阅读完成。

简介

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 架构实战电子书等等!

正文完
 0