java的UI基础

GUI(图形用户界面,Graphics User Interface)

  1. AWT(Abstract Window Toolkit)

较早被开发,主要考虑的是程序与操作系统的兼容性,缺点是灵活性差,占用资源多。在java包中

  1. SWING

并没有完全替代awt包,互补。在javax包中

注: java和javax包是Java两个主要的两个类库,java包汇总存放java语言的核心包。javax是一个扩展包。

组件与组件方法

  1. 抽象类JComponent,所有可视组件的父类

常用子类如下:

子类名 描述 子类名 描述
JButton 创建按钮对象 JMenuBar 创建菜单条对象
JTree 创建树对象 JMenuItem 创建菜单项对象
JComboBox 创建组合框对象 JPanel 创建面板对象
JCheckBox 创建符合框对象 JPasswordField 创建口令文本对象
JFileChooser 创建文件选择器 JPopupMenu 创建弹出式菜单
JInternalFrame 创建内部窗体 JProgressBar 创建进程条
JLabel 创建标签 JRadioButton 创建单选按钮
JMenu 创建菜单对象 JScrollBar 创建滚动条
JScrollPane 创建滚动窗格 JTextArea 创建文本区
JSlider 创建滚动条 JTextPane 创建文本窗格
JSplitPane 创建拆分窗格 JToolBar 创建工具条
JTable 创建表格 JToolTip 创建工具提示对象
  1. JFrame,java图形用户界面中最底层的容器之一,用来容纳用户界面中的所有组件。在图形用户界面中的表现形式就是窗口。

  2. JPanel,与JFrame功能差不多,不同的是JFrame可以独立应用,JPanel只能依附于JFrame才能应用,既要用JPanel就要先建一个JFrame容器,或者继承类JFrame(下面的实例程序就是继承JFrame)。

  3. JDialog 用来创建对话框窗口的主要类。继承自AWT的Dialog类,支持Swing体系结构的高级GUI属性。

  4. 布局组件

  1. FlowLayout是一个最简单的布局管理器,被称为流布局管理器(将容器中的组件从左到右从上到下排列)。
  2. BorderLayout,边界布局管理器。
  3. GridLayout,网格布局管理器。

对于每一个组件都有各自的方法,即函数。

事件与事件处理

在图形界面运行时,用户操作键盘和鼠标对组件的一个动作就是事件,系统对此作出反应的过程就是事件处理。

事件的处理不是组件本身来完成的。组件产生的事件与事件的处理过程是分开的。java将事件的处理委托给叫做 监听器 的事件处理实体(实际上是一个类的实例)来完成。这被称为委托处理模式。

监听器格式:XxxListener()
监听器方法格式:addXxxListener()

事件的处理过程:

  1. 确定产生事件的组件
  2. 确定要被处理的事件
  3. 处理事件对应的代码

实例程序,简易计算机的实现

// ComputeDemp.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComputeDemo extends JFrame implements ActionListener {
    private JPanel jPanel1, jPanel2;
    private JTextField resultField;
    private JButton s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
    private JButton b1,b2,b3,b4,f1,f2;
    private boolean end,add,sub,mul,div;
    private String str;
    private double num1,num2;
    
    public ComputeDemo() {
        super("Computer");
        setSize(240, 230);
        Container con = getContentPane();
        con.setLayout(new BorderLayout());
        jPanel1 = new JPanel();
        jPanel1.setLayout(new GridLayout(1, 1));
        jPanel2 = new JPanel();
        jPanel2.setLayout(new GridLayout(4, 4));
        resultField = new JTextField("0");
        jPanel1.add(resultField);
        con.add(jPanel1, BorderLayout.NORTH);
        s1 = new JButton("1");
        s1.addActionListener(this);
        s2 = new JButton("2");
        s2.addActionListener(this);
        s3 = new JButton("3");
        s3.addActionListener(this);
        s4 = new JButton("4");
        s4.addActionListener(this);
        s5 = new JButton("5");
        s5.addActionListener(this);
        s6 = new JButton("6");
        s6.addActionListener(this);
        s7 = new JButton("7");
        s7.addActionListener(this);
        s8 = new JButton("8");
        s8.addActionListener(this);
        s9 = new JButton("9");
        s9.addActionListener(this);
        s0 = new JButton("0");
        s0.addActionListener(this);
        b1 = new JButton("+");
        b1.addActionListener(this);
        b2 = new JButton("-");
        b2.addActionListener(this);
        b3 = new JButton("*");
        b3.addActionListener(this);
        b4 = new JButton("/");
        b4.addActionListener(this);
        f1 = new JButton(".");
        f1.addActionListener(this);
        f2 = new JButton("=");
        f2.addActionListener(this);
        jPanel2.add(s1);
        jPanel2.add(s2);
        jPanel2.add(s3);
        jPanel2.add(b1);
        jPanel2.add(s4);
        jPanel2.add(s5);
        jPanel2.add(s6);
        jPanel2.add(b2);
        jPanel2.add(s7);
        jPanel2.add(s8);
        jPanel2.add(s9);
        jPanel2.add(b3);
        jPanel2.add(s0);
        jPanel2.add(f1);
        jPanel2.add(f2);
        jPanel2.add(b4);
        con.add(jPanel2, BorderLayout.CENTER);
    }
    
    public void num(int i) {
        String s = null;
        s = String.valueOf(i);
        if(end) {
            //如果数字输入结束,则将文本框置零,重新输入
            resultField.setText("0");
            end = false;
        }
        if((resultField.getText()).equals("0")) {
            //如果文本框的内容为0,则覆盖文本框的内容
            resultField.setText(s);
        } else {
            //如果文本框的内容不为0,则在内容后面添加数字
            str = resultField.getText() + s;
            resultField.setText(str);
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        //数字事件
        if(e.getSource() == s1)
            num(1);
        else if(e.getSource() == s2)
            num(2);
        else if(e.getSource() == s3)
            num(3);
        else if(e.getSource() == s4)
            num(4);
        else if(e.getSource() == s5)
            num(5);
        else if(e.getSource() == s6)
            num(6);
        else if(e.getSource() == s7)
            num(7);
        else if(e.getSource() == s8)
            num(8);
        else if(e.getSource() == s9)
            num(9);
        else if(e.getSource() == s0)
            num(0);     
        //符号事件
        else if(e.getSource() == b1)
            sign(1);
        else if(e.getSource() == b2)
            sign(2);
        else if(e.getSource() == b3)
            sign(3);
        else if(e.getSource() == b4)
            sign(4);
        //等号
        else if(e.getSource() == f1) {
            str = resultField.getText();
            if(str.indexOf(".") <= 1) {
                str += ".";
                resultField.setText(str);
            }
        } else if(e.getSource() == f2) {
            num2 = Double.parseDouble(resultField.getText());
            if(add) {
                num1 = num1 + num2;
            } else if(sub) {
                num1 = num1 - num2;
            } else if(mul) {
                num1 = num1 * num2;
            } else if(div) {
                num1 = num1 / num2;
            }
            resultField.setText(String.valueOf(num1));
            end = true;
        }
    }
    
    public void sign(int s) {
        if(s == 1) {
            add = true;
            sub = false;
            mul = false;
            div = false;
        } else if(s == 2) {
            add = false;
            sub = true;
            mul = false;
            div = false;
        } else if(s == 3) {
            add = false;
            sub = false;
            mul = true;
            div = false;
        } else {
            add = false;
            sub = false;
            mul = false;
            div = true;
        }
        num1 = Double.parseDouble(resultField.getText());
        end = true;
    }
    
    public static void main(String[] args) {
        ComputeDemo th1 = new ComputeDemo();
        th1.show();
    }
 }

运行结果

图片.png

以上内容是学习《Java程序设计与工程实践》(于波,齐鑫,唐光义编注)。
这是一本很好的适合java初学者的书,推荐给大家!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 面向对象主要针对面向过程。 面向过程的基本单元是函数。 什么是对象:EVERYTHING IS OBJECT(万物...
    sinpi阅读 1,096评论 0 4
  • 1.import static是Java 5增加的功能,就是将Import类中的静态方法,可以作为本类的静态方法来...
    XLsn0w阅读 1,267评论 0 2
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 4,681评论 1 114
  • 本文介绍三种科学的思维方式,它们不仅在科学研究中起着重要的作用,也可以指导我们日常的活动。 奥卡姆剃须刀 斯坦福大...
    jran_ml阅读 2,040评论 0 8
  • 一戴上耳机,思绪便不和我姓
    尘醉了没阅读 153评论 0 0