java GUI编程

java GUI编程

awt和swing
  • awt:抽象窗口工具集
    • 所属包:java.awt
  • swing:awt的拓展,不是替换
    • swing继承了awt的许多类,因此使用swing类库的时候间接使用了awt类库
    • 所属包:javax.swing
  • component类中的常用方法:
    • String getName() 获得组件的名称

    • Container getParent() 获得直接容纳该组件的容器

    • boolean isFocusable() 判断组件是否可以获得焦点

    • boolean isFocusOwner() 判断组件是否获得了焦点

    • void setEnabled(boolean b) 设置组件是否被屏蔽(例如按钮如果是false就是变灰不可点击)

    • boolean isEnabled() 判断组件是否被屏蔽

    • void setSize(int width,int height) 设置组件的尺寸

    • void setVisible(boolean b) 设置组件是否可见

    • boolean isVisible() 判断组件是否可见

    • void setLocation(int x,int y) 指定组件显示的位置

      • 一个小例子
public class TestFrame {
    public static void main(String[] args) {
        //通过跟进看源码和构造器
        Frame frame=new Frame("第一个窗口");
        //设置可见性
        frame.setVisible(true);
        //窗口大小
        frame.setSize(400,400);
        //背景颜色
        frame.setBackground(new Color(56, 146, 255));
        //窗口位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable();
    }
}

在这里插入图片描述

编写GUI的步骤
  • 构造界面:构造组件对象
    • 构造容器对象+构造组件对象+add方法
  • 美化界面:对容器组件合理布局
  • 响应界面:给界面加入事件处理机制
  • Component类中的一些常用的方法
    • Container 类中的常用方法:
      • Component add(Component comp) 将组件加到容器中
      • void remove(Component comp) 将组件从容器中移除
      • void setLayout(LayoutManager mgr) 设置容器的布局
    • Frame 类的常用方法:
      • void setTitle(String s) 设置标题
      • String getTitle() 获取标题
    • Label 类、TextField 类的常用方法:
      void setText(String s) 设置文本信息
      String getText() 获取文本信息
    • Button 类的常用方法:
      • void setLabel(String s) 设置按钮的标签信息,不是组件名称,只是标签上面的内容而不是按钮的名字
      • String getLabel() 获取按钮的标签信息
  • FlowLayout 布局类: 组件从左到右,逐行依次摆放
  • GridLayout 布局类: 将容器分割成同等大小的网格
  • Font类:Font f1=new Font(" 宋体" , Font.BOLD , 23);
package GUI;
import java.awt.*;
import javax.swing.*;

public class GuiPra extends JFrame{
    GuiPra(){
        super("练习");
    }
    public void f(){
        setSize(400,400);
        setBackground(Color.ORANGE);
        setLocation(300,300);
        setLayout(new FlowLayout());
        add(new Label("用户名:"));
        add(new JTextField(5));
        add(new Label("密码:"));
        add(new TextField(5));
        add(new JButton("登录"));
        add(new JButton("退出"));
        setVisible(true);
    }

    public static void main(String[] args) {
        new GuiPra().f();
    }
}

在这里插入图片描述

注意Label和JLabel区别:JLabel 的setBackground 是继承自JComponent , 而JComponent 的setOpaque(boolean) 默认情况下是false ,背景透明而无法显示底层像素因此如果要用到JLabel的背景设置则需要将方法改成true

创建图形界面的5个步骤
  • 创建JFrame窗体
  • 对窗体设置大小位置背景布局等等
  • 定义一系列需要的组件
  • 将组件通过add方法添加到窗体里
  • 让窗体显示:setVisible
委托事件处理模型
  • 三要素:事件源、事件、事件处理者(监听器)
  • 三步骤:
    • 确定事件源和事件
    • 定义处理者即实现相关事件的接口
    • 关联事件源和事件处理者
package Practice2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Guipra2 extends JFrame implements ActionListener{
    private JButton bok,bexit;
    private JTextField username;
    private Label tla;
    private JPasswordField password;
    public Guipra2(){
        super("一个界面");
    }
    public void f(){
        setSize(400,400);
        setLocation(300,300);
        setBackground(Color.GRAY);
        setLayout(new FlowLayout());
        username=new JTextField(5);
        password=new JPasswordField(5);
        add(new Label("用户名:"));
        add(username);
        add(new Label("密码:"));
        add(password);
        bok=new JButton("确定");
        bexit=new JButton("退出");
        add(bok);
        add(bexit);
        add(new Label("  "));
        tla=new Label("  ");//提示信息的标签
        add(tla);
        setVisible(true);
        //GUI界面设计部分
        bexit.addActionListener(this);//建立关联
        bok.addActionListener(this);
    }
    public  void actionPerformed(ActionEvent e){
    	//发生按钮点击事件监听器必须实现与事件对应的接口AcctionListener,并且自动调用事件处理方法actionPerformed
        if (e.getSource()==bexit)
            System.exit(0);
        if (e.getActionCommand().equals("确定")){
            String KeyText=String.valueOf(password.getPassword());
            //将字符数组转换成字符串
            if(username.getText().equals("why")&&password.getPassword().equals("1234"))
                tla.setText("欢迎你");
            else
                tla.setText("用户名或者密码输入错误");
            setVisible(true);
        }
    }

    public static void main(String[] args) {
        Guipra2 g=new Guipra2();
        g.f();
    }
}


在这里插入图片描述

  • 注意点:在java类库中每个事件源能产生的事件是确定的,与之匹配的事件监听器接口也是确定的,与监听器建立关联的方法也是确定的。比如上面例子中的JButton的对象可以产生ActionEvent事件,那么相应建立关联的方法就是addActionListener(ActionListener e)
    • 并且监听器接口中有特定的方法,该方法其实就是完成时间处理的方法,例如上面例子中的 public void actionPerformed(ActionEvent e)
    • 实现窗体的关闭动作:
      • 实现WindowListener接口
      • 重写接口中的7个方法,其中只需要利用public void WindowClosing(WindowEvent e){System.exit(0);}
由窗口关闭引出的适配器类
  • 由于监听器必须要实现监听器接口中的所有抽象方法比较麻烦且像窗口关闭只需要用到一个方法却要重写7个方法
  • 第二种窗口关闭的方法
//定义一个内部类
class A extents WindowAdapter{
	public void WindowClosing(WindowEvent e){System.exit(0);}
	//在构造函数中加入下面语句
	this.addWindowListener(new A());
}
  • 第三种关闭窗体的方法
    • 由于关闭窗口是Frame常用特性,因此JFrame提供了静态int型常量EXIT_ON_CLOSE
JFrame f=new JFrame("why的界面");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
布局管理
  • 设定布局的方法:void setLayout(LayoutManager m)
  • FlowLayout:流式布局
    • 默认时从左至右、从上到下逐行摆放
    • LEFT(左对齐)
    • RIGHT(右对齐)
    • CENTER(居中对齐)
    • LEADING(首对齐):主要是针对英文字母
    • TRAILING(尾对齐)
setLayout ( new FlowLayout( FlowLayout.RIGHT ) );
  • GridLayout:网格布局
setLayout(new GridLayout(3,2,5,10));//将容器等分成3 行*2 列个网格 ,每个网格只能放一个组件,且自动填满;网格水平间隙为5 ,纵向间隙为10,只能以行为序从左至右填写网格
  • BorderLayout:边界布局(默认的布局管理器)
    • 将容器分成东西南北中5 个区域,每个区域只能放一个组件,且自动填满可将组件放到指定区域,也可缺省某些区域
add(new JButton("EAST"),BorderLayout.EAST);

猜你喜欢

转载自blog.csdn.net/Phoebe4/article/details/111461400