目录
GUI的概念
Swing概述
编辑
容器组件
布局管理器
常用组件
对话框
内部类
GUI的概念
GUI(Graphics User Interface),图形用户界面,是指采用图形方式显示的计算机操作用户界面,是计算机与其使用者之间的对话接口,是计算机系统的重要组成部分。它能够使应用程序看上去更加友好
Swing概述
 * JFrame           窗口
 * JPanel           面板(流式布局,边界布局,网格布局)
 * JTextFiled       单行文本输入框
 * JLable           标签
 * JPasswordFiled   密码框组件
 * JTextArea        多行文本输入框
 * JScrollPane      滚动面板
容器组件
组件是一个以图形化的方式显示在屏幕上能够与用户进行交互的对象,不能独立显示,必须放在一定的容器中。
容器可容纳多个组件,用add方法添加组件进容器
窗口Frame和面板Panel是最常用的容器
JFrame(框架)
用于在Swing中创建窗体
通过get()方法就可以获得此框架的内容面板
或者自己创建一个JPanel面板对象,把它作为一个组件加到容器中
public class Demo1JFrame extends JFrame {public Demo1JFrame() throws HeadlessException {this.setSize(500, 300);//设置窗口的大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口选项// this.setLocation(450,500);//位置坐标this.setResizable(false);//禁止设置窗口大小this.setLocationRelativeTo(null);//相对位置 水平垂直居中this.setTitle("欢迎登录");//窗口标题      this.setVisible(true);//显示窗口this.dispose();//关闭窗口}public static void main(String[] args) {Demo1JFrame d = new Demo1JFrame();}
}
JPanel(面板)
提供面板,是轻量级别的容器,一般使用面板来实现布局嵌套
 //面板,可以看成是一个空间,但不能单独存在//流式布局JPanel jp = new JPanel();//默认是流式布局jp.setBackground(PINK);//设置背景颜色jp.setLayout();//设置页面布局this.add(jp);//将面板加入JFramethis.setVisible(true);//显示窗口菜单组件JMenuBar();//创建菜单JMenu();//菜单名称
JMenuItem();//菜单项名称
setJMenuBar(menuBar);//将菜单栏添加到窗口布局管理器
每个Jpanel 都有一个布局管理器
常用的布局管理器:流式布局,边界布局BorderLayout和网格布局GridLayout
 //面板,可以看成是一个空间,但不能单独存在//流式布局JPanel jp = new JPanel();//默认是流式布局jp.setBackground(PINK);//设置背景颜色this.add(jp);//将面板加入JFramethis.setVisible(true);//显示窗口JButton jb = new JButton("登录");jp.add(jb);JPanel jp = new JPanel(new BorderLayout());//边界布局JButton jb1 = new JButton("登录1");JButton jb2 = new JButton("登录2");JButton jb3 = new JButton("登录3");jp.add(jb1, BorderLayout.EAST);jp.add(jb2, BorderLayout.SOUTH);jp.add(jb3);this.add(jp);this.setVisible(true);//显示窗口JPanel jp = new JPanel(new GridLayout(2, 2));//网格布局JButton jb1 = new JButton("登录1");JButton jb2 = new JButton("登录2");JButton jb3 = new JButton("登录3");JButton jb4 = new JButton("登录4");jp.add(jb1);jp.add(jb2);jp.add(jb3);jp.add(jb4);this.add(jp);
常用组件
JLabel(标签)
 JLabel label = new JLabel("账号");label.setForeground(Color.PINK);//设置背景颜色label.setFont(new Font("宋体", Font.BOLD, 20));//设置字体JTextField(单行文本)
 JTextField accountText = new JTextField(15);accountText.setText("你好");
JTextArea(多行文本框)
 JTextArea ja=new JTextArea(5,15);ja.setLineWrap(true);//自动换行JScrollPane(滚动条)
JScrollPane js=new JScrollPane(ja);//滚动条
jp.add(js);JPasswordField(密码框)
 JPasswordField passwordField=new JPasswordField(15);passwordPanel.add(passwordLabel);passwordLabel.add(passwordField);JButton(按钮)
 JButton loginBtn=new JButton("登录");btnPanel.add(loginBtn);菜单栏组件
  JMenuBar jMenuBar=new JMenuBar();d.setJMenuBar(jMenuBar);JMenu jm = new JMenu("菜单");jMenuBar.add(jm);JMenuItem ji = new JMenuItem("文件");JMenuItem ji1 = new JMenuItem("保存");jm.add(ji);jm.add(ji1);事件处理
当我们点击一个按钮时,或者用键盘输入字符,或者是点击鼠标时,我们应该如何处理这个事件。
事件监听:
 jb.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String account = null;               }}
对话框
JOptionPane:
showMessageDialog(消息对话框)
消息类型:
     ERROR_MESSAGE                     错误消息提示
      INFORMATION_MESSAGE         信息提示
      WARNING_MESSAGE                 警告提示
      QUESTION_MESSAGE                问题提示
      PLAIN_MESSAGE                        简洁提示
showConfirmDialog(确认对话框)
     DEFAULT_OPTION                   默认选项   
      YES_NO_OPTION                    是/否选项
      YES_NO_CANCEL_OPTION   是/否/取消选项
      OK_CANCEL_OPTION             确定/取消
jb.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String account = null;if (account.length() > 10) {JOptionPane.showMessageDialog(null, "你好", "就是你", JOptionPane.QUESTION_MESSAGE);int res = JOptionPane.showConfirmDialog(null, "小朋友", "很能吃", JOptionPane.YES_NO_OPTION);System.out.println(res);if (res == 0) {return;}}内部类
内部类是java对类的一种定义方式,是嵌套的一个分类。
 非静态嵌套类(内部类)可访问其外围类的其他成员,即使这些成员被声明为私有的。
 静态内部类,只能访问外部类中静态成员
内部类分为:
成员内部类
public class Outer {int num = 10;public void eat() {new Inner();//一般在本类中使用内部类}//在一个类的内部定义的类称为内部类class Inner {public void test() {System.out.println(num);//在内部类中可以直接使用外部类中的成员}}
}静态内部类
 //静态内部类,只能访问外部类中静态成员
private static int num1=20;class Inner {public void test() {System.out.println(num1);}}public static void main(String[] args) {}
}局部内部类
jb.addActionListener(new ActionListener() {//将只在本类中使用的功能,定义在内部类中,实现封装,在哪儿使用,new内部类即可class MyAction implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("被点击了");System.out.println(accountText.getText());//使用外部类的成员}}匿名内部类
 accountText.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("被点击了");System.out.println(accountText.getText());}});
内部类特点:
是独立的类
不能用普通的方式访问,内部可以自由访问外部类的成员变量
声明成静态的,就不能随便访问外部类的成员变量,只能访问外部类的静态成员变量
意义:
封装性
实现多继承



















