【Java学习笔记】48:盒内组件的支撑和ActionEvent事件处理

还是跟着课本敲代码。

盒内组件的支撑

用添加水平支撑和添加垂直支撑的方式,控制行式盒容器列式盒容器中组件之间的距离。

Main.java
public class Main {
    public static void main(String[] args) {
        WindowBoxLayout wbl=new WindowBoxLayout();
        wbl.setBounds(100,100,400,140);
        wbl.setTitle("嵌套盒式布局容器,又嵌套给了底层流体布局");
    }
}
WindowBoxLayout.java
import java.awt.FlowLayout;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class WindowBoxLayout extends JFrame{
    Box boxH;//行式盒
    Box boxV1,boxV2;//列式盒
    WindowBoxLayout(){
        this.setLayout(new FlowLayout());//设置为流体布局
        this.init();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void init() {
        boxH=Box.createHorizontalBox();//建立行式盒
        boxV1=Box.createVerticalBox();//建立列式盒
        boxV2=Box.createVerticalBox();
        //分别向两个列式盒中添加组件
        boxV1.add(new JLabel("列式盒1中的标签1"));
        boxV1.add(new JLabel("列式盒1中的标签2"));
        boxV2.add(new JTextField(10));
        boxV2.add(new JTextField(10));
        //把两个列式盒放进行式盒里,并在中间添加水平支撑
        boxH.add(boxV1);
        boxH.add(Box.createHorizontalStrut(10));
        boxH.add(boxV2);
        this.add(boxH);//把这个行式盒给底层容器JFrame
    }

}
运行

这里写图片描述

处理事件的基本方式

在这个例子中,文本框对象jtf使用了addActionListener方法,将实现ActionListener接口的实例注册为自己这个事件源的监视器。

实现了ActionListener接口即需要实现其中的actionPerformed方法,覆写这个方法,以指明当监听的事件发生时,也就是为方法传入了ActionEvent对象时,所做的事情。

Main.java
public class Main {

    public static void main(String[] args) {
        WindowActionEvent wnd=new WindowActionEvent();
        wnd.setTitle("处理ActionEvent事件");
        wnd.setBounds(100,100,300,90);
    }

}
WindowActionEvent.java
import java.awt.FlowLayout;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class WindowActionEvent extends JFrame{
    JTextField jtf;
    ActionListener al;//监视器

    WindowActionEvent(){
        this.setLayout(new FlowLayout());
        jtf=new JTextField(10);
        this.add(jtf);

        //父类用子类的对象实例化
        //而子类中覆写了对事件发生时的处理方法
        al=new ReaderListen();

        //JTextField创建的对象是能触发ActionEvent事件的
        //用这个对象,使用addActionListener方法,传入监视器
        //从而将实现ActionListener接口的实例注册为事件源的监视器
        jtf.addActionListener(al);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
ReaderListen.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//用于创建监视器的类
public class ReaderListen implements ActionListener{
    //当事件发生时,监视器调用接口中的方法actionPerformed对事件处理
    //ActionEvent类事先创建的对象就会传递给方法的参数e
    @Override
    public void actionPerformed(ActionEvent e) {
        String str=e.getActionCommand();//获取封装在事件中的命令字符串
        System.out.println("这个命令字符串是:"+str);
    }

}

这里写图片描述

(对文本框而言,焦点在框内,且按下回车时事件发生)
这里写图片描述

在处理事件时如何影响到组件

这个例子展示了在输入完按下回车,或者按按钮时,在下面的文本区显示消息,并且清空文本框本身。

要影响到组件,就需要获得组件的引用,所以可以让(直接或间接)实现了ActionListener接口的监听器类,用某种方式获取组件的应用。

这个例子用,将获取组件的引用这件事,和ActionListener中的响应事件用的actionPerformed方法放在一起,即让监听器类去实现继承了ActionListener接口的新的接口,新的接口中添加了这两个方法,能获取到要改变的相关组件的引用。

MyCommandListener.java
import java.awt.event.ActionListener;

import javax.swing.JTextArea;
import javax.swing.JTextField;

//实现了ActionListener接口的子接口,多给了两个方法
public interface MyCommandListener extends ActionListener{
    //用来设置文本框内容的方法
    public void setJTextField(JTextField jtf);
    //用来设置文本区内容的方法
    public void setJTextArea(JTextArea jta);
}
PoliceListen.java
import java.awt.event.ActionEvent;

import javax.swing.JTextArea;
import javax.swing.JTextField;

//监听器,实现继承了ActionListener的自己建的接口
public class PoliceListen implements MyCommandListener{
    JTextField jtf;
    JTextArea jta;//文本区域(多行文本)
    @Override
    public void actionPerformed(ActionEvent e) {
        String str=jtf.getText();//获取文本框内容
        jta.append("输入了:"+str+"\n");//附加到文本区域去
        jtf.setText("");//清空输入框
    }

    //用这种方式获得文本框和文本区域的引用
    //从而在actionPerformed方法中使用它们
    @Override
    public void setJTextField(JTextField jtf) {
        this.jtf=jtf;

    }
    @Override
    public void setJTextArea(JTextArea jta) {
        this.jta=jta;

    }

}
WindowActionEvent.java
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class WindowActionEvent extends JFrame{
    JTextField jtf;
    JTextArea jta;
    JButton jb;
    //自己建的接口,其实还是要用监视器类实例化(泛型)
    MyCommandListener mcl;

    WindowActionEvent(){
        init();
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    void init() {
        this.setLayout(new FlowLayout());
        jtf=new JTextField(10);
        jb=new JButton("确定");
        jta=new JTextArea(9,30);
        this.add(jtf);
        this.add(jb);
        //把文本区封装到JScrollPane(滚动条)然后再添加给底层容器
        this.add(new JScrollPane(jta));
    }

    //设置自己写的监听器(也是用泛型的接口)
    void setMyCommandListener(MyCommandListener mcl) {
        this.mcl=mcl;//传入进来
        //把当前的文本框和文本区传进去,监听器收到消息才能用
        mcl.setJTextField(jtf);
        mcl.setJTextArea(jta);
        //用当前文本框为事件源,注册监听器
        jtf.addActionListener(mcl);
        //用按钮为事件源,注册相同的监听器
        jb.addActionListener(mcl);
        //那么当两种事件发生时,会执行一样的监听器里的actionPerformed方法
    }


}
Main.java
public class Main {

    public static void main(String[] args) {
        WindowActionEvent wae=new WindowActionEvent();
        PoliceListen pl=new PoliceListen();//自定的监视器
        wae.setMyCommandListener(pl);//泛型传入,监视器
        wae.setBounds(100,100,460,360);
        wae.setTitle("处理ActionEvent事件时对组件改动");
    }

}
运行

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值