URL:统一资源管理器
抽象的窗口工具
1.包含了很多类和接口
2.元素:窗口,按钮,文本框
3.java.awt
2.1.frame
2.2.面板Panel
3.布局管理器
2.3流式布局
- package com.qyx.www.lesson01;
-
- import java.awt.*;
-
- public class TestFlowLayout {
- 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.setSize(200,200);
-
- //把按钮添加上去
- frame.add(button1);
- frame.add(button2);
- frame.add(button3);
-
- frame.setVisible(true);
- }
- }
2.4东西南北中
- package com.qyx.www.lesson01;
-
- import java.awt.*;
-
- public class TestBorderLayout {
- public static void main(String[] args) {
- Frame frame = new Frame("TestBordLayout");
-
- 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(south,BorderLayout.SOUTH);
- frame.add(north,BorderLayout.NORTH);
- frame.add(center,BorderLayout.CENTER);
-
- frame.setSize(200,200);
- frame.setVisible(true);
- }
- }
- package com.qyx.www.lesson01;
-
- import java.awt.*;
-
- public class TestGridLayout {
- public static void main(String[] args) {
- Frame frame = new Frame("TestGridLayout");
-
- Button btn1 = new Button("btn1");
- Button btn2 = new Button("btn2");
- Button btn3 = new Button("btn3");
- Button btn4 = new Button("btn4");
- Button btn5 = new Button("btn5");
- Button btn6 = new Button("btn6");
-
- frame.setLayout(new GridLayout(3,2));
-
- frame.add(btn1);
- frame.add(btn2);
- frame.add(btn3);
- frame.add(btn4);
- frame.add(btn5);
- frame.add(btn6);
-
- frame.pack();//java函数
- frame.setVisible(true);
- }
- }
3.1作业题:
- package com.qyx.www.lesson01;
-
- import java.awt.*;
-
- public class TestButton {
- public static void main(String[] args) {
- Frame frame = new Frame();
- frame.setSize(400,300);
- frame.setLocation(300,400);
- frame.setBackground(Color.BLACK);
- frame.setVisible(true);
- frame.setLayout(new GridLayout(2,1));
-
- //4个面板
- Panel p1 = new Panel(new BorderLayout());
- Panel p2 = new Panel(new GridLayout(2, 1));
- Panel p3 = new Panel(new BorderLayout());
- Panel p4 = new Panel(new GridLayout(2, 2));
-
- //上面OK
- p1.add(new Button("East-1"),BorderLayout.EAST);
- p1.add(new Button("West-1"),BorderLayout.WEST);
- p2.add(new Button("p2-btn-1"));
- p2.add(new Button("p2-btn-2"));
- p1.add(p2,BorderLayout.CENTER);
-
- //下面
- p3.add(new Button("East-2"),BorderLayout.EAST);
- p3.add(new Button("West-2"),BorderLayout.WEST);
- //中间四个
- for (int i = 0; i < 4; i++) {
- p4.add(new Button("for-"+i));
- }
- p3.add(p4,BorderLayout.CENTER);
-
- frame.add(p1);
- frame.add(p3);
-
-
- }
- }
3.2.总结:
1.Frame是一个顶级窗口
2.Panel无法单独显示,必须添加到某个容器中
3.布局管理器
1.流式
2.东西南北中
3.表格
4.大小,定位,背景颜色,可见性,监听!
事件监听:当某个事情发生的时候,干什么?
- package com.qyx.www.lesson02;
-
- 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 TestActionEvent {
- public static void main(String[] args) {
- //按下按钮,触发一些事件
- Frame frame = new Frame();
- Button button = new Button();
- //因为:addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
- MyActionListener myActionListener = new MyActionListener();
- button.addActionListener(myActionListener);
-
- frame.add(button,BorderLayout.CENTER);
- frame.pack();
-
- windowClose(frame); //关闭窗口
- frame.setVisible(true);
- }
-
- //关闭窗口的事件
- private static void windowClose(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("666");
- }
- }
多个按钮,共享一个事件
- package com.qyx.www.lesson02;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- public class TestActionTwo {
- public static void main(String[] args) {
- //两个按钮 实现同一个监听
- //开始 停止
- Frame frame = new Frame("开始-停止");
- Button button1 = new Button("start");
- Button button2 = new Button("stop");
-
- //可以显示的定义出发会返回的命令,如果不显示定义,则会走默认的值
- //可以多个按钮只写一个监听类
- button2.setActionCommand("button2-stop");
-
- MyMonitor myMonitor = new MyMonitor();
-
- button1.addActionListener(myMonitor);
- button2.addActionListener(myMonitor);
-
- frame.add(button1,BorderLayout.NORTH);
- frame.add(button2,BorderLayout.SOUTH);
-
- frame.pack();
- frame.setVisible(true);
- }
- }
-
- class MyMonitor implements ActionListener{
-
- @Override
- public void actionPerformed(ActionEvent e) {
- //e.getActioncommand()获得按钮的信息
- System.out.println("按钮被点击了: msg=>"+e.getActionCommand());
- if (e.getActionCommand().equals("start")){
-
- }
-
- }
- }
- package com.qyx.www.lesson02;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- public class TestText01 {
- public static void main(String[] args) {
- //启动!
- new Myframe();
- }
- }
-
- class Myframe extends Frame{
- public Myframe(){
- TextField textField = new TextField();
- add(textField);
-
- //监听这个文本框输入的文字
- MyActionListener2 myActionListener2 = new MyActionListener2();
- textField.addActionListener(myActionListener2);
-
- //设置替换编码
- textField.setEchoChar('*');
-
- setVisible(true);
- pack();
- }
- }
-
- class MyActionListener2 implements ActionListener{
-
- @Override
- public void actionPerformed(ActionEvent e) {
- TextField field =(TextField) e.getSource();//获得一些资源,返回的一个对象
- System.out.println(field.getText());
- field.setText("");//null
- }
- }
OOP原则:组合,大于继承!
- package com.qyx.www.lesson02;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- public class TestCalc {
- public static void main(String[] args) {
- new Calculator();
- }
- }
-
- //计算器类
- class Calculator extends Frame{
- public Calculator(){
- //3个文本框
- TextField num1 = new TextField(10);//字符数
- TextField num2 = new TextField(10);//字符数
- TextField num3 = new TextField(20);//字符数
-
- //1个按钮
- Button button = new Button("=");
- button.addActionListener(new MyCalculatorListener(num1,num2,num3));
-
- //1个标签
- Label label = new Label("+");
-
- //布局
- setLayout(new FlowLayout());
-
- add(num1);
- add(label);
- add(num2);
- add(button);
- add(num3);
-
- pack();
- setVisible(true);
-
- }
- }
-
- //监听器类
- class MyCalculatorListener implements ActionListener{
-
- //获取三个变量
- private TextField num1,num2,num3;
-
- public MyCalculatorListener(TextField num1,TextField num2,TextField num3){
- this.num1 = num1;
- this.num2 = num2;
- this.num3 = num3;
-
- }
-
- @Override
- public void actionPerformed(ActionEvent e) {
-
- //1.获得加数和被加数
- int n1 = Integer.parseInt(num1.getText());
- int n2 = Integer.parseInt(num2.getText());
-
- //2.将这个值 + 法运算后,放到第三个框
- num3.setText(""+(n1+n2));
-
- //3.清除前两个框
- num1.setText("");
- num2.setText("");
- }
- }
- package com.qyx.www.lesson03;
-
- import java.awt.*;
-
- public class TestPaint {
- public static void main(String[] args) {
- new MyPaint().loadFrame();
- }
- }
-
- class MyPaint extends Frame{
-
- public void loadFrame(){
- setBounds(200,200,600,500);
- setVisible(true);
- }
- //画笔
- @Override
- public void paint(Graphics g){
- //super.paint(g);
- //画笔需要又颜色,画笔可以画画
- //g.setColor(Color.red);
- //g.drawOval(100,100,100,100);
- g.fillOval(100,100,100,100);//实心的圆
-
- //g.setColor(Color.GREEN);
- g.fillRect(150,200,150,200);
-
- //养成习惯,画笔用完,将他还原到最初的颜色
- }
- }
目的:想要实现鼠标画画
- package com.qyx.www.lesson03;
-
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- 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(200,200,400,300);
- //存鼠标的点击的点
- points = new ArrayList<>();
-
- setVisible(true);
- //鼠标监听器,正对这个窗口
- this.addMouseListener(new MyMouseListener());
- }
-
- @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);
- }
- }
-
- //添加一个点到界面上
- public void addPaint(Point point){
- points.add(point);
- }
-
- //适配器模式
- private class MyMouseListener extends MouseAdapter{
- //鼠标 按下,弹起,按住不放
- @Override
- public void mousePressed(MouseEvent e){
- MyFrame myFrame =(MyFrame) e.getSource();
- //这个我们点击的时候,就会再界面上产生一个点:画
- //这个点就说鼠标的点;
- myFrame.addPaint(new Point(e.getX(),e.getY()));
-
- //每次点击鼠标都需要重新画一遍
- myFrame.repaint();//刷新 30帧 60帧
-
- }
- }
- }
- package com.qyx.www.lesson03;
-
- import java.awt.*;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
-
- public class TestWindow {
- public static void main(String[] args) {
- new WindowsFrame();
- }
- }
-
- class WindowsFrame extends Frame{
- public WindowsFrame(){
- setBackground(Color.blue);
- setBounds(100,100,200,200);
- setVisible(true);
- //addWindowListener(new MyWindowListener());
-
- this.addWindowListener(
- //匿名内部类
- new WindowAdapter() {
- //关闭窗口
- @Override
- public void windowClosing(WindowEvent e) {
- System.out.println("windowClosing");
- System.exit(0);
- }
- //激活窗口
- @Override
- public void windowActivated(WindowEvent e) {
- WindowsFrame source = (WindowsFrame)e.getSource();
- source.setTitle("被激活了");
- System.out.println("windowActivated");
- }
- }
- );
- }
-
-
- }
- package com.qyx.www.lesson03;
-
- 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(){
- setBounds(1,2,300,400);
- setVisible(true);
-
- this.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- //获得键盘下的键是哪一个,当前的码
- int keyCode = e.getKeyCode();
- System.out.println(keyCode);
- if (keyCode == KeyEvent.VK_UP){
- System.out.println("你按下了上键");
- }
- }
- });
-
- }
- }
- package com.qyx.www.lesson04;
-
- import javax.swing.*;
- import java.awt.*;
-
- public class JFrameDemo {
- public static void main(String[] args) {
- new MyJframe2().init();
- }
- }
-
- class MyJframe2 extends JFrame{
- public void init(){
- this.setBounds(10,10,200,300);
- this.setVisible(true);
-
- JLabel label = new JLabel("欢迎来到我的世界!");
- this.add(label);
-
- //让文本标签居中 设置水平对齐
- label.setHorizontalAlignment(SwingConstants.CENTER);
-
- //获得一个容器
- Container container = this.getContentPane();
- container.setBackground(Color.YELLOW);
- }
- }
JDialog,用来被弹出,默认就有关闭事件
- package com.qyx.www.lesson04;
-
- 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.setSize(700,500);
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
-
- //JFrame 放东西,容器
- Container container = this.getContentPane();
- //绝对布局
- container.setLayout(null);
-
- //按钮
- JButton button = new JButton("点击弹出一个对话框"); //创建
- button.setBounds(30,30,200,50);
-
- //点击这个按钮的时候,弹出一个弹窗
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- //弹窗
- new MyDialogDemo();
- }
- });
-
- container.add(button);
- }
-
- public static void main(String[] args) {
- new DialogDemo();
- }
- }
-
- //弹窗的窗口
- class MyDialogDemo extends JDialog{
- public MyDialogDemo(){
- this.setVisible(true);
- this.setBounds(100,100,500,500);
-
- Container container = this.getContentPane();
- container.setLayout(null);
-
- container.add(new Label("老师带你学java"));
- }
- }
label
new JLabel("xxx");
图标ICON
- package com.qyx.www.lesson04;
-
- import javax.swing.*;
- import java.awt.*;
-
- //图标,需要实现类,Frame继承
- public class IconDemo extends JFrame implements Icon {
-
- private int width;
- private int height;
-
- public IconDemo(){} //无参构造
-
- public IconDemo(int width,int height){
- this.width=width;
- this.height=height;
- }
-
- public void init(){
- IconDemo iconDemo = new IconDemo(15, 15);
- //图标放在标签,也可以放在按钮上1
- JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
-
- Container container = getContentPane();
- container.add(label);
-
- this.setVisible(true);
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
-
- public static void main(String[] args) {
- new IconDemo().init();
- }
-
-
- @Override
- public void paintIcon(Component c, Graphics g, int x, int y) {
-
- }
-
- @Override
- public int getIconWidth() {
- return 0;
- }
-
- @Override
- public int getIconHeight() {
- return 0;
- }
- }
图片ICON
- package com.qyx.www.lesson04;
-
- import javax.swing.*;
- import java.awt.*;
- import java.net.URL;
-
- public class ImageIconDemo extends JFrame {
- public ImageIconDemo() {
- //获取图片的地址
- JLabel label = new JLabel("ImageIcon");
- URL url = ImageIconDemo.class.getResource("tx.jpg");
-
- ImageIcon imageIcon = new ImageIcon(url); //命名不要冲突了
- label.setIcon(imageIcon);
- label.setHorizontalAlignment(SwingConstants.CENTER);
-
- Container container = getContentPane();
- container.add(label);
-
- setVisible(true);
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- setBounds(100, 100, 200, 200);
- }
-
-
- public static void main(String[] args) {
- new ImageIconDemo();
- }
- }
Jpanel
- package com.qyx.www.lesson05;
-
- 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 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 JPanelDemo();
- }
- }
JScrollPanel
- package com.qyx.www.lesson05;
-
- import javax.swing.*;
- import java.awt.*;
-
- public class JScrollDemo extends JFrame {
-
- public JScrollDemo(){
- Container container = this.getContentPane();
-
- //文本域
- JTextArea textArea = new JTextArea(20, 50);
- textArea.setText("欢迎学习狂神说Java");
-
- //Scroll面板
- JScrollPane scrollpane = new JScrollPane(textArea);
- container.add(scrollpane);
-
- this.setVisible(true);
- this.setBounds(100,100,300,350);
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
-
- public static void main(String[] args) {
- new JScrollDemo();
- }
- }
(1)单选按钮
(2)复选按钮
下拉框
- package com.qyx.www.lesson05;
-
- import javax.swing.*;
- import java.awt.*;
-
- public class TestComboboxDemo01 extends JFrame {
- public TestComboboxDemo01() {
- Container container = this.getContentPane();
-
- JComboBox status = new JComboBox();
-
- status.addItem(null);
- status.addItem("正在热映");
- status.addItem("已下架");
- status.addItem("即将上映");
-
- container.add(status);
-
- this.setVisible(true);
- this.setSize(500,350);
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
-
- public static void main(String[] args) {
- new TestComboboxDemo01();
- }
- }
列表框
- package com.qyx.www.lesson05;
-
- import javax.swing.*;
- import java.awt.*;
-
- public class TestComboboxDemo02 extends JFrame {
- public TestComboboxDemo02() {
- Container container = this.getContentPane();
-
- //生成列表的内容
- String[] contents = {"1","2","3"};
- //列表中需要放入内容
- JList jList = new JList(contents);
-
- container.add(jList);
-
-
- this.setVisible(true);
- this.setSize(500,350);
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
-
- public static void main(String[] args) {
- new TestComboboxDemo02();
- }
- }
文本框
- package com.qyx.www.lesson05;
-
- import javax.swing.*;
- import java.awt.*;
-
- public class TestTextDemo01 extends JFrame {
- public TestTextDemo01() {
- Container container = this.getContentPane();
-
- JTextField textField = new JTextField("hello");
- JTextField textField2 = new JTextField("hello",20);
-
- container.add(textField,BorderLayout.NORTH);
- container.add(textField2,BorderLayout.SOUTH);
-
-
- this.setVisible(true);
- this.setSize(500,350);
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
-
- public static void main(String[] args) {
- new TestTextDemo01();
- }
- }
密码框
- package com.qyx.www.lesson05;
-
- import javax.swing.*;
- import java.awt.*;
-
- public class TestTextDemo03 extends JFrame {
- public TestTextDemo03() {
- Container container = this.getContentPane();
-
- JPasswordField passwordField = new JPasswordField();//***
- passwordField.setEchoChar('*');
-
- container.add(passwordField);
-
-
- this.setVisible(true);
- this.setSize(500,350);
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
-
- public static void main(String[] args) {
- new TestTextDemo03();
- }
- }
文本域
- package com.qyx.www.lesson05;
-
- import javax.swing.*;
- import java.awt.*;
-
- public class JScrollDemo extends JFrame {
-
- public JScrollDemo(){
- Container container = this.getContentPane();
-
- //文本域
- JTextArea textArea = new JTextArea(20, 50);
- textArea.setText("欢迎学习狂神说Java");
-
- //Scroll面板
- JScrollPane scrollpane = new JScrollPane(textArea);
- container.add(scrollpane);
-
- this.setVisible(true);
- this.setBounds(100,100,300,350);
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
-
- public static void main(String[] args) {
- new JScrollDemo();
- }
- }
内容较多,但覆盖了GUI的所有方面,后续可以利用GUI实现一个简单贪吃蛇小游戏。