GUI编程

目录

一、GUI的概念

二、swing的概述

三、容器组件

四、常用容器

4.1JFrame(窗口)

4.2Jpanel(面板)

五、常用组件

5.1标签(JLabel)

5.2单行文本(JTextField)

5.3密码框(JPasswordField)

5.4多行文本框

5.5按钮

 5.6菜单栏,菜单,菜单项

六、事件处理

6.1添加事件监听器

6.2JOptionPane对话框


一、GUI的概念

GUI(Graphical User Interface)即图形用户界面,它能够使应用程序看上去更加友好;

二、swing的概述

  Swing是纯Java组件,使得应用程序在不同的平台上运行时具有相同外观和相同
的行为。
Swing中的大部分组件类位于javax.swing包中.
Swing中的组件非常丰富,支持很多功能强大的组件.

三、容器组件

Java的图形用户界面的基本组成部分是 组件
组件 是一个以图形化的方式 ,显示在屏幕上并能与用户进行交互的对象;
组件不能独立地显示出来,必须将组件放在一定的容器(container)中才可以显示出来。
容器可以容纳多个组件,通过调用容器的add(Component comp)方法
向容器中添加组件。
窗口(Frame)和面板(Panel)是最常用的两个容器

四、常用容器

4.1JFrame(窗口)

4.1.1构造方法

JFrame()     创建一个新窗口,窗口初始化是不显示的,需要调用setvisible()方法才能显示。

JFrame(String title)     创建一个窗口并命名   

4.1.2常用方法

在构造方法中设置窗口的属性和特征

void setSize(int width, int height)
设置窗口的大小
void setVisible(boolean b)
让窗口显示出来
void setTitle(String title)
设置窗口的标题
void setResizable(boolean resizable)
设置窗口能否被拉伸
void setLocation(int x,int y)
设置窗口在屏幕上的位置,坐标(x,y)
void setLocationRelativeTo(null);
窗口在屏幕上的位置为居中
void setDefaultCloseOperation(int operation)
关闭窗口时,同时关闭程序
void dispose()
销毁窗口

具体操作如下

public class Demo1 extends JFrame{
    public Demo1() throws HeadlessException {
        this.setSize(400,400);
        this.setTitle("欢迎登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        demo1.setVisible(true);

    }
}

 

会出现一个窗口,窗口标题为欢迎登录。

4.2Jpanel(面板)

窗口中不能添加组件,但可以添加面板,面板中可以添加其他组件。

构造方法

JPanel()   创建一个面板,默认布局方式为流式布局

JPanel(LayoutManaer layout)    创建一个指定布局方式的面板 

常用方法

void setBackground(Color bg)设置面板的背景色,由参数bg指定颜色
void setLayout(LayoutManager mgr)设置面板的布局,参数是布局管理器
Component add(Component comp)往面板中添加一个组件

布局方式

Java中有几种常用的布局管理器,分别是:FlowLayout(流式) , BorderLayout(边界), GridLayout(网格)。

a.流式布局:把面板分成一行一行的,在每一行可以摆放组件,可以设置摆放的位置(对齐方式)

public class DengluFlowlayout extends JFrame {
    public DengluFlowlayout() throws HeadlessException {
        this.setVisible(true);
        this.setSize(400,400);
        this.setTitle("欢迎登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        JPanel jpanel1=new JPanel(new FlowLayout(FlowLayout.LEFT));//左对齐
        jpanel1.setBackground(new Color(2, 66, 255));
        JButton jButton1=new JButton("按钮1");
        JButton jButton2=new JButton("按钮2");
        JButton jButton3=new JButton("按钮3");
        JButton jButton4=new JButton("按钮4");
        jpanel1.add(jButton1);
        jpanel1.add(jButton2);
        jpanel1.add(jButton3);
        jpanel1.add(jButton4);
        this.add(jpanel1);
    }
    public static void main(String[] args) {
        DengluFlowlayout denglu = new DengluFlowlayout();
        denglu.setVisible(true);
    }
}

将面板加到窗口上展示出来,在面板上设置背景颜色为蓝色,在面板上添加按钮组件是水平排列的

b.边界布局:把一个面板分成五个区域,东西南北中五个区域,每个区域可以添加其他组件。

public class DemoBoederlayout extends JFrame {
    public DemoBoederlayout() throws HeadlessException {
        this.setSize(400,400);
        this.setTitle("欢迎登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        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);
    }
    public static void main(String[] args) {
        DemoBoederlayout demoBoederlayout = new DemoBoederlayout();
        demoBoederlayout.setVisible(true);
    }
}

 面板上的5个按钮分别布满了5个指定区域。

c.网格布局:把一个面板分为n行n列,可以从第一行第一列向表格中添加组件

public class DemoGridLayout extends JFrame {
    public DemoGridLayout() throws HeadlessException {
        this.setSize(400,400);
        this.setTitle("欢迎登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        JPanel jpanel = new JPanel(new GridLayout(2,2));//布局方式为网格型,2行2列
        JButton jButton1=new JButton("按钮1");
        JButton jButton2=new JButton("按钮2");
        JButton jButton3=new JButton("按钮3");
        JButton jButton4=new JButton("按钮4");
        jpanel.add(jButton1);
        jpanel.add(jButton2);
        jpanel.add(jButton3);
        jpanel.add(jButton4);
        this.add(jpanel);
    }
    public static void main(String[] args) {
        DemoGridLayout demoGridLayout = new DemoGridLayout();
        demoGridLayout.setVisible(true);
    }
}

 面板上出现4个按钮,按2行2列布局。

五、常用组件

5.1标签(JLabel)

标签是容纳文本和图标的控件,通常用来在界面中标识别的控件。
构造方法:
JLabel()       创建一个空的标签
JLabel(String text)        创建一个带文本的标签
JLabel(Icon image)          创建一个带图像的标签
方法:
void setText(String text)        设置标签上的文本
String getText()                获得标签上的文本
setFont(new Font(“ 宋体 ”,Font.BOLD, 18));      设置字体

5.2单行文本(JTextField)

构造方法:
JTextField()           创建一个空的单行文本
JTextField(String text )     创建一个有默认值的单行文本
JTextField(int columns)     创建一个列为columns的单行文本
JTextField(String text, int columns)    创建一个既有默认值又有列长度的单行文本
方法:
void setText(String text)      设置文本框中的文本
String getText()              获得文本框中的文本
void setEditable(boolean b)      设置文本框是否可以编辑
setColumns(20);     设置列数

5.3密码框(JPasswordField)

构造方法:
JPasswordField()      创建一个空的密码框
JPasswordField(String text)    创建一个有默认值的密码框
JPasswordField(String text, int columns)   创建一个既有默认值又有列长度的密码框
方法:
char[] getPassword()    用字符数组的形式返回实际输入的密码
具体实现如下:
public class Demo3 extends JFrame {
    public Demo3() throws HeadlessException {
        this.setSize(500,500);
        this.setTitle("欢迎登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        JPanel jPanel = new JPanel();
        JLabel jLabel = new JLabel("账号");
        JLabel jLabel1 = new JLabel("密码");
        jLabel.setFont(new Font("楷体",Font.BOLD, 18));
        jLabel1.setFont(new Font("楷体",Font.BOLD, 18));
        JButton jButton=new JButton("登录");
        JTextField jTextField = new JTextField(15);
        JPasswordField jPasswordField = new JPasswordField(15);
        jPanel.add(jLabel);
        jPanel.add(jTextField);
        jPanel.add(jLabel1);
        jPanel.add(jPasswordField);
        jPanel.add(jButton);
        this.add(jPanel);
    }
    public static void main(String[] args) {
        Demo3 demoJLable = new Demo3();
        demoJLable.setVisible(true);
    }
}

 创建两个标签为账号和密码,创建一个单行文本和密码框,依次加入到面板上即可。

5.4多行文本框

构造方法:
JTextArea()     创建一个空的文本域
JTextArea(String text)     用指定文本初始化文本域
JTextArea(int rows, int columns)    创建一个指定行数和列数的空文本域
JTextArea(String text,int rows, int columns)
创建一个带文本,并指行数和列数的
方法:
void setText(String text)     设置文本域中的文本
String getText()    获得文本域中的文本
void setFont(Font font) 设置文本域中文本的字体
void setLineWrap(boolean wrap) // 是否自动换行 , 默认 false
如果需要文本区自动出现滚动条,可将文本区对象放入滚动窗格 (JScrollPane) :
JScrollPane scrollPane = new JScrollPane(txtArea);
add(scrollPane )
public class Demo4 extends JFrame {
    public Demo4() throws HeadlessException {
        this.setSize(400,400);
        this.setTitle("欢迎登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        Panel panel = new Panel();
        JTextArea jTextArea=new JTextArea(5,10);
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        panel.add(jScrollPane);
        this.add(panel);
    }
    public static void main(String[] args) {
        Demo4 demo4 = new Demo4();
        demo4.setVisible(true);
    }

5.5按钮

构造方法 :
JButton (String text)     创建一个带文本的标签
JButton (Icon image)      创建一个带图像的标签
方法 :
void setBackground(Color bg)    设置按钮的背景色
void setEnabled(boolean b)     设置启用(或禁用)按钮,由参数b 决定
void setToolTipText(String text)     设置按钮的悬停提示信息
public class Demo4 extends JFrame {
    public Demo4() throws HeadlessException {
        this.setSize(400,400);
        this.setTitle("欢迎登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        Panel panel = new Panel();
        JTextArea jTextArea=new JTextArea(5,10);
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
       JButton jbutton =new JButton("登录");
        jbutton.setToolTipText("点击登录");
        jbutton.setBackground(new Color(2, 255, 247, 81));
        panel.add(jScrollPane);
        panel.add(jbutton);
        this.add(panel);
    }
    public static void main(String[] args) {
        Demo4 demo4 = new Demo4();
        demo4.setVisible(true);
    }
}

 5.6菜单栏,菜单,菜单项

菜单栏组件 :
构造方法 :    JMenuBar();
方法  :   add(menu); 向菜单栏添加菜单
菜单组件 :
构造方法 :     JMenu(“ 菜单名称 ");
方法 :       add(menuItem); 向菜单添加菜单选项
菜单项组件 :
构造方法 :    JMenuItem(“ 菜单项名称 ");
将菜单栏添加到窗口:    setJMenuBar(menuBar);
具体实现如下:
public class Demo5 extends JFrame {
    public Demo5() throws HeadlessException {
        this.setSize(400,400);
        this.setTitle("欢迎登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        Panel panel = new Panel();
        JMenuBar jMenuBar = new JMenuBar();
        JMenu jMenu = new JMenu("文件");
        JMenu jMenu1 = new JMenu("帮助");
        JMenuItem j = new JMenuItem("新建");
        JMenuItem j1 = new JMenuItem("保存");
        jMenuBar.add(jMenu1);
        jMenuBar.add(jMenu);
        panel.add(jMenuBar);
        this.add(panel);
    }
    public static void main(String[] args) {
        Demo5 demo5 = new Demo5();
        demo5.setVisible(true);

    }
}

六、事件处理

对于采用了图形用户界面的程序来说,事件控制是非常重要的;
到目前 为止,我们编写的图形用户界面程序都仅仅只是完成了静态界面,而没有任何实际的功能,要实现相应的功能,必须进行事件处理;
用户与GUI组件进行交互就会发生事件,如:按下一个按钮、用键盘输
入一个字符、点击鼠标等等;
当前我们要关注的并不是“事件是如何产生的” ,而是讨论当发生事件
后,我们应当“如何处理之” 。
Java中,事件处理的基本思路如下:
一个源(事件源)产生一个事件(事件对象)并把它送到监听器那里,监听器只是简单地等待,直到它收到一个事件,一旦事件被接受,监听器将处理这些事件;
一个事件源必须注册监听器以便监听器可以接受关于一个特定事件的通知。

6.1添加事件监听器

按钮对象 .addActionListener(new ActionListener() {
                               // 事件处理
@Override
public void actionPerformed(ActionEvent e) {
               执行操作
}
});

6.2JOptionPane对话框

showMessageDialog() :消息对话框
主要有五种消息类型,类型不同,图标不同:
ERROR_MESSAGE                                     错误消息提示
INFORMATION_MESSAGE                         信息提示
WARNING_MESSAGE                                 警告提示
QUESTION_MESSAGE                                问题提示
PLAIN_MESSAGE                                         简洁提示
showConfirmDialog() : 确认对话框
主要有四种消息类型,类型不同,图标不同:
DEFAULT_OPTION                                            默认选项
YES_NO_OPTION                                            / 否选项
YES_NO_CANCEL_OPTION                            / / 取消选项
OK_CANCEL_OPTION                                      确定 / 取消
具体实现如下:
public class Demo7 extends JFrame {
    public Demo7() throws HeadlessException {
        this.setSize(400,400);
        this.setTitle("欢迎登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        Panel panel = new Panel();
        JTextField jTextField = new JTextField(15);
        JScrollPane jScrollPane = new JScrollPane(jTextField);
        JButton jbutton =new JButton("登录");
        jbutton.setToolTipText("点击登录");
        jbutton.setBackground(new Color(2, 255, 247, 81));
        panel.add(jScrollPane);
        panel.add(jbutton);
        this.add(panel);
        jTextField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(jTextField.getText().length()==0)
                    JOptionPane.showMessageDialog(null,"请输入账号");
                JOptionPane.showConfirmDialog(null,"确定要退出吗","警告",JOptionPane.YES_NO_CANCEL_OPTION);
            }
        });
    }
    public static void main(String[] args) {
        Demo7 demo7 = new Demo7();
        demo7.setVisible(true);
    }
}

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值