Java--GUI编程

目录

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);//将面板加入JFrame
        this.setVisible(true);//显示窗口
菜单组件
   JMenuBar();//创建菜单
  JMenu();//菜单名称
JMenuItem();//菜单项名称
setJMenuBar(menuBar);//将菜单栏添加到窗口

布局管理器

每个Jpanel 都有一个布局管理器

常用的布局管理器:流式布局,边界布局BorderLayout和网格布局GridLayout

 //面板,可以看成是一个空间,但不能单独存在
        //流式布局
        JPanel jp = new JPanel();//默认是流式布局
        jp.setBackground(PINK);//设置背景颜色
        this.add(jp);//将面板加入JFrame
        this.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() {
            @Override
            public 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() {
            @Override
            public 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{

               @Override
               public void actionPerformed(ActionEvent e) {
                   System.out.println("被点击了");
                   System.out.println(accountText.getText());//使用外部类的成员
               }
           }

匿名内部类

 accountText.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("被点击了");
                System.out.println(accountText.getText());
            }
        });

内部类特点:

是独立的类

不能用普通的方式访问,内部可以自由访问外部类的成员变量

声明成静态的,就不能随便访问外部类的成员变量,只能访问外部类的静态成员变量

意义:

封装性

实现多继承

javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值