Java 第十一章(一)

一、GUI的概念

GUI(Graphical User Interface)即图形用户界面,是指采用图形方 式显示的用户界面,与早期计算机使用的命令行界面相比,图形界面对 于用户来说在视觉上更易于接受。

二、swing概述

  • swing 是一个为Java设计的GUI工具包javax.swing,该包中包括了图形用户界面 的各种组件支持。
  • 一个 Java 的图形界面,由各种不同类型的“元素”组成,这些“元素”被称为(Component)
  • swing中的组件可以分为两大类:容器(如窗口,对话框,面板)和功能组件(如按钮,输 入框,菜单等)。
  • 它们的区别主要是在概念上的。因为每个容器也都是组件。
  • 容器是一种可以包含组件的特殊组件。

 三、 常用容器

JFrame类用来创建窗体,我们的类可以直接继承该类,实现窗体制作 。

设置窗体常用的方法:

 自己创建Jpanel面板对象,把JPanel作为一个组件添加到窗口或某个面板中。

常用方法:

四、布局管理器 

Java中的图形界面在布局管理上采用容器和布局管理相分离的方案,也 就是说容器只是把组件放进来,但它不管怎样放置。至于如何放置需要 用到布局管理器(Container)。

Java中有几种常用的布局管理器,分别是:FlowLayout ,BorderLayout,GridLayout 

代码示例如下:

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

public class Demo1Frame extends JFrame {


    public Demo1Frame() throws HeadlessException {
             this.setTitle("欢迎登录");
             this.setSize(300,300);//设置宽高
             this.setLocationRelativeTo(null); //水平垂直居中
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
             this.setResizable(false);//设置禁止窗口拖拽调整大小

              /*
                 面板组件可以设置布局管理方式: 布局管理器
                 FlowLayout: 流水布局  也是面板默认的布局方式
                             把组件放在一排,从左到右排放,一行占满后,重新开启一行
                             面板默认流式布局是水平居中的

               */
              JPanel jPanel = new JPanel();
              //JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 设置内容水平对齐方式
              //JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,10,30));设置组件之间水平,垂直间距

               JButton jButton1 = new JButton("按钮1");
               JButton jButton2 = new JButton("按钮2");

               jPanel.add(jButton1);
               jPanel.add(jButton2);



              //把面板添加到窗口上
              this.add(jPanel);
             this.setVisible(true);//让窗口显示  放在设置的最后一行
    }


    public static void main(String[] args) {
           new Demo1Frame();//创建一个窗口对象
    }
}

 

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

public class Demo2Frame extends JFrame {


    public Demo2Frame() throws HeadlessException {
             this.setTitle("欢迎登录");
             this.setSize(300,300);//设置宽高
             this.setLocationRelativeTo(null); //水平垂直居中
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
             this.setResizable(false);//设置禁止窗口拖拽调整大小

              /*
                   边界布局:
                     总共有5个区域,每个区域可以放置一个组件,并且占满整个区域,
                     中间区域是必须的,其他几个区域可以根据需要使用
                     添加组件式,可以指定组件的位置,如果不指定,默认添加到中间区域
               */
              JPanel jPanel = new JPanel(new BorderLayout());
                JButton jButton1 = new JButton("按钮1");
                JButton jButton2 = new JButton("按钮2");
                JButton jButton3 = new JButton("按钮3");
                JButton jButton4 = new JButton("按钮4");
                JButton jButton5 = new JButton("按钮5");

                jPanel.add(jButton1,BorderLayout.NORTH);
                jPanel.add(jButton2,BorderLayout.SOUTH);
                jPanel.add(jButton3,BorderLayout.WEST);
                //jPanel.add(jButton4,BorderLayout.EAST);
                jPanel.add(jButton5,BorderLayout.CENTER);


              //把面板添加到窗口上
              this.add(jPanel);
             this.setVisible(true);//让窗口显示  放在设置的最后一行
    }


    public static void main(String[] args) {
           new Demo2Frame();//创建一个窗口对象
    }
}
import javax.swing.*;
import java.awt.*;

public class Demo3Frame extends JFrame {


    public Demo3Frame() throws HeadlessException {
             this.setTitle("欢迎登录");
             this.setSize(300,300);//设置宽高
             this.setLocationRelativeTo(null); //水平垂直居中
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
             this.setResizable(false);//设置禁止窗口拖拽调整大小

              /*
                     网格布局:
                        网格就类似一个表格,可以设置行数和列数
                        每个网格中只能放一个组件,占满整个区域
                        从第一行开始摆放,第一行占满后,再开启第二行
               */
              JPanel jPanel = new JPanel(new GridLayout(2,2));

                    JButton jButton1 = new JButton("按钮1");
                    JButton jButton2 = new JButton("按钮2");
                    JButton jButton3 = new JButton("按钮3");
                    JButton jButton4 = new JButton("按钮4");


                    jPanel.add(jButton2);
                    jPanel.add(jButton3);
                    jPanel.add(jButton4);
                    jPanel.add(jButton1);

              //把面板添加到窗口上
              this.add(jPanel);
             this.setVisible(true);//让窗口显示  放在设置的最后一行
    }


    public static void main(String[] args) {
           new Demo3Frame();//创建一个窗口对象
    }
}

 

五、常用组件

标签(JLabel)     标签是容纳文本和图标的控件,通常用来在界面中标识别的控件。

构造函数: JLabel() 创建一个空的标签 JLabel(String text) 创建一个带文本的标签。

方法:

 

单行文本(JTextField)

JTextField的构造函数: JTextField(int columns) 

方法:

多行文本框(JTextArea)

构造函数: JTextArea(int rows, int columns) 创建一个指定行数和列数的空文本域 

方法:

 注:
如果需要文本区自动出现滚动条,可将文本区对象放入滚动窗格(JScrollPane)中:

JScrollPane scrollPane = new JScrollPane(txtArea);

add(scrollPane );

密码框(PasswordField)

构造函数: JPasswordField(String text) JPasswordField(String text, int columns)

 方法:

按钮(JButton)

构造方法: JButton(String text) 创建一个带文本的标签 

方法:

菜单栏组件:
构造方法:JMenuBar();
方法:add(menu); 向菜单栏添加菜单

菜单组件:
构造方法:JMenu(“菜单名称");
方法:add(menuItem); 向菜单添加菜单选项

菜单项组件:
构造方法:JMenuItem(“菜单项名称");
将菜单栏添加到窗口 setJMenuBar(menuBar); 

代码示例如下:

public class ComponentFrame extends JFrame {

    public ComponentFrame() throws HeadlessException {
        this.setTitle("欢迎登录");
        this.setSize(300,300);//设置宽高
        this.setLocationRelativeTo(null); //水平垂直居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
        this.setResizable(false);//设置禁止窗口拖拽调整大小

        //创建一个面板
        JPanel jPanel = new JPanel();

          //标签组件,用来放置文字
          JLabel jLabel = new JLabel("账号");
          jLabel.setFont(new Font("楷体", Font.BOLD,20));//设置字体
          jLabel.setForeground(new Color(27, 147, 27));//设置字体的颜色

          //单行文本框组件  设置列数  列宽
          JTextField jTextField = new JTextField(15);
          //jTextField.getText();//获得文本框中输入的内容

          jPanel.add(jLabel);
          jPanel.add(jTextField);

        //把面板添加到窗口上
        this.add(jPanel);
        this.setVisible(true);//让窗口显示  放在设置的最后一行

    }

    public static void main(String[] args) {
         new ComponentFrame();
    }
}
import javax.swing.*;
import java.awt.*;

public class Component1Frame extends JFrame {

    public Component1Frame() throws HeadlessException {
        this.setTitle("欢迎登录");
        this.setSize(300,300);//设置宽高
        this.setLocationRelativeTo(null); //水平垂直居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
        this.setResizable(false);//设置禁止窗口拖拽调整大小

        //创建一个面板
        JPanel jPanel = new JPanel();

          //多行文本框组件(文本域)
          JTextArea jTextArea = new JTextArea(5,20);
                     jTextArea.setLineWrap(true);//设置强制换行
          //带滚动条的面板   把多行文本框组件加进来
          JScrollPane jsp = new JScrollPane(jTextArea);

          jPanel.add(jsp);

        //把面板添加到窗口上
        this.add(jPanel);
        this.setVisible(true);//让窗口显示  放在设置的最后一行

    }

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

 

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

public class Component2Frame extends JFrame {

    public Component2Frame() throws HeadlessException {
        this.setTitle("欢迎登录");
        this.setSize(300,300);//设置宽高
        this.setLocationRelativeTo(null); //水平垂直居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
        this.setResizable(false);//设置禁止窗口拖拽调整大小

        //创建一个面板
        JPanel jPanel = new JPanel();

           JLabel passwordjLabel = new JLabel("密码");

           JPasswordField jPasswordField = new JPasswordField(15);
           char[] password =  jPasswordField.getPassword();//获得输入的密码

           JButton jButton  = new JButton("登录");
                   jButton.setEnabled(false);//禁用按钮
                    jButton.setToolTipText("点击登录");

           jPanel.add(passwordjLabel);
           jPanel.add(jPasswordField);
           jPanel.add(jButton);

        //把面板添加到窗口上
        this.add(jPanel);
        this.setVisible(true);//让窗口显示  放在设置的最后一行

    }

    public static void main(String[] args) {
         new Component2Frame();
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Component3Frame extends JFrame {

    public Component3Frame() throws HeadlessException {
        this.setTitle("欢迎登录");
        this.setSize(300,300);//设置宽高
        this.setLocationRelativeTo(null); //水平垂直居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
        this.setResizable(false);//设置禁止窗口拖拽调整大小

        //菜单栏  菜单  菜单项
         JMenuBar jMenuBar  = new JMenuBar();//创建菜单栏
        //创建菜单
        JMenu jMenu1 = new JMenu("文件");
        JMenu jMenu2 = new JMenu("编辑");
        JMenu jMenu3 = new JMenu("帮助");

        //创建菜单项
        JMenuItem jMenuItem1 = new JMenuItem("新建");
        JMenuItem jMenuItem2 = new JMenuItem("保存");
        JMenuItem jMenuItem3 = new JMenuItem("剪切");
        JMenuItem jMenuItem4 = new JMenuItem("复制");
        JMenuItem jMenuItem5 = new JMenuItem("关于我们");

        //把菜单项添加到菜单中
        jMenu1.add(jMenuItem1);
        jMenu1.add(jMenuItem2);
        jMenu2.add(jMenuItem3);
        jMenu2.add(jMenuItem4);
        jMenu3.add(jMenuItem5);

        //把菜单添加到菜单栏
        jMenuBar.add(jMenu1);
        jMenuBar.add(jMenu2);
        jMenuBar.add(jMenu3);

         this.setJMenuBar(jMenuBar);//把菜单栏添加到窗口


        //创建一个面板
        JPanel jPanel = new JPanel();
        //把面板添加到窗口上
        this.add(jPanel);

        this.setVisible(true);//让窗口显示  放在设置的最后一行

        //为菜单项添加事件监听以及事件的处理程序
        jMenuItem5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("聊天室2.0");
            }
        });

    }

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

 六、事件处理

 Java中,事件处理的基本思路如下:
一个事件源产生一个事件并把它送到监听器那里,监听器一直等待,直 到它收到一个事件,一旦事件被接受,监听器将处理这些事件。

代码示例如下:

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

public class Demo1Frame extends JFrame {


    public Demo1Frame() throws HeadlessException {
             this.setTitle("欢迎登录");
             this.setSize(300,300);//设置宽高
             this.setLocationRelativeTo(null); //水平垂直居中
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
             this.setResizable(false);//设置禁止窗口拖拽调整大小

              //创建一个面板
              JPanel jPanel = new JPanel();

              JTextField jTextField = new JTextField(15);

              JButton jButton = new JButton("登录");

               jPanel.add(jTextField);
               jPanel.add(jButton);

              //把面板添加到窗口上
              this.add(jPanel);
              this.setVisible(true);//让窗口显示  放在设置的最后一行

        //为组件添加事件处理程序
        //jButton.addActionListener(new B());
        //new 接口  创建一个匿名内部类的对象,  是为了简化语法
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(jTextField.getText());
            }
        });


    }

    //内部类
   /* class B  implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    }*/


    public static void main(String[] args) {
           new Demo1Frame();//创建一个窗口对象
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Demo2Frame extends JFrame {


    public Demo2Frame() throws HeadlessException {
             this.setTitle("欢迎登录");
             this.setSize(300,300);//设置宽高
             this.setLocationRelativeTo(null); //水平垂直居中
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
             this.setResizable(false);//设置禁止窗口拖拽调整大小

              //创建一个面板
              JPanel jPanel = new JPanel();

              JTextField jTextField = new JTextField(15);

              JButton jButton = new JButton("登录");
              jButton.setEnabled(false);

               jPanel.add(jTextField);
               jPanel.add(jButton);

              //把面板添加到窗口上
              this.add(jPanel);
              this.setVisible(true);//让窗口显示  放在设置的最后一行

        //为文本框添加事件监听
        jTextField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //System.out.println("按键按下了"+e.getKeyChar()+":"+e.getKeyCode());
                jButton.setEnabled(true);
            }
        });

        //为组件添加事件处理程序
        //鼠标处理事件 共有5种
          jButton.addMouseListener(new MouseAdapter() {
              @Override
              public void mouseClicked(MouseEvent e) {
                  System.out.println("鼠标点击触发 -- 一次按下抬起");
              }

              @Override
              public void mousePressed(MouseEvent e) {
                  System.out.println("鼠标按下");
              }

              @Override
              public void mouseReleased(MouseEvent e) {
                  System.out.println("鼠标释放 按键抬起");
              }

              @Override
              public void mouseEntered(MouseEvent e) {
                  System.out.println("鼠标移入到标签上");
              }

              @Override
              public void mouseExited(MouseEvent e) {
                  System.out.println("鼠标移除标签");
              }
          });


    }



    public static void main(String[] args) {
           new Demo2Frame();//创建一个窗口对象
    }
}

七、对话框 

JOptionPane对话框

showMessageDialog():消息对话框 
主要有五种消息类型,类型不同,图标不同

 showConfirmDialog():确认对话框
主要有四种消息类型,类型不同,图标不同:

 

 代码示例如下:

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

public class Demo3Frame extends JFrame {


    public Demo3Frame() throws HeadlessException {
             this.setTitle("欢迎登录");
             this.setSize(300,300);//设置宽高
             this.setLocationRelativeTo(null); //水平垂直居中
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
             this.setResizable(false);//设置禁止窗口拖拽调整大小

              //创建一个面板
              JPanel jPanel = new JPanel();

              JTextField jTextField = new JTextField(15);

              JButton jButton = new JButton("登录");

               jPanel.add(jTextField);
               jPanel.add(jButton);

              //把面板添加到窗口上
              this.add(jPanel);
              this.setVisible(true);//让窗口显示  放在设置的最后一行

        //为组件添加事件处理程序
        //jButton.addActionListener(new B());
        //new 接口  创建一个匿名内部类的对象,  是为了简化语法
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获得文本框输入的内容
                 String account = jTextField.getText();
                 if(account.length()==0){
                     //消息提示框
                     //JOptionPane.showMessageDialog(null,"请输入账号!");
                     //JOptionPane.showMessageDialog(null,"请输入账号!","操作提示",JOptionPane.WARNING_MESSAGE);

                    int res =   JOptionPane.showConfirmDialog(null, "您确定要退出吗?","操作提示",JOptionPane.OK_CANCEL_OPTION);
                     System.out.println(res);// 点击确定返回0 , 点击取消返回2
                     if(res==0){
                         //执行退出操作
                     }
                     return;
                 }
                if(account.length()<6 ||account.length()>10){
                    JOptionPane.showMessageDialog(null,"请输入一个6-10位之间的账号!");
                    return;
                }

            }
        });


    }




    public static void main(String[] args) {
           new Demo3Frame();//创建一个窗口对象
    }
}

  • 19
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值