7

Java-GUI-事件监听 - i_归零

 2 years ago
source link: https://www.cnblogs.com/lw14589/p/lw--12345.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java-GUI-事件监听

1、窗口的事件监听

主要用来关闭窗口
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
           System.exit(0);
        }

2、按钮的事件监听

当按下按钮时,会触发一些事件,可监听一个按钮或者多个按钮,可以用匿名内部类写出或者自己创建一个类来实现,这里用的是后者方法。
1、当监听一个按钮时

重新写一个类和一个接口。

具体代码如下:
class MeActionListner implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//语句体(实现按下按钮后所触发的一些事件)
}
}
2、当多个按钮同共用一个监听时

可以通过设置按钮上的信息来对按钮进行区分,从而实现不同的按钮而能触发不同的事件

具体代码如下:
  //定义两个按钮或者多个,以下以两个为例
  Button button1 = new Button("开始");
  Button button2 = new Button("结束");
  //用setActionCommand()方法来设置每个按钮中的信息
  button1.setActionCommand("start");
  button2.setActionCommand("stop");
  //主函数中只需要new一次MeActionListner()即可实现多个按钮的监听
  MeActionListner meActionListner= new MeActionListner();
    button.setActionCommand("start");
    button1.setActionCommand("stop");
  //在MeActionListner()中实现按钮的监听事件
  class MeActionListner implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
    //用e.getActionCommand()来获取按钮信息,然后用equals("字符串")来判断哪一个按钮
    if(e.getActionCommand().equals("start"))
    {
        //语句体1(实现按钮1的触发事件)
    }
    if(e.getActionCommand().equals("stop"))
    {
       //语句体2(实现按钮2的触发事件)
    }
	//若还有其他按钮,只需再添加条件语句进行判断即可
}

与按钮监听有关的代码:

   import java.awt.*;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import java.awt.event.WindowAdapter;
   import java.awt.event.WindowEvent;
 public class GUI06 {
 public static void main(String[] args) {
    Frame frame = new Frame("监听事件");
    Button button = new Button("开始");
    Button button1=new Button("结束");
    //addActionListener需要一个ActionListener,所以我们需要构造一个,若为接口就写实现类,若为父类就继承
    MeActionListner meActionListner= new MeActionListner();
    button.setActionCommand("start");
    button1.setActionCommand("stop");
    button.addActionListener(meActionListner);
    button1.addActionListener(meActionListner);
    frame.add(button,BorderLayout.EAST);
    frame.add(button1,BorderLayout.WEST);
    frame.pack();
    frame.setVisible(true);
    windowClose(frame);

}
//关闭窗口的事件
private static void windowClose(Frame frame){
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
           System.exit(0);
        }
    });
}
	//触发按钮事件的接口
class MeActionListner implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("start"))
    {
        System.out.println("开始");
    }
    if(e.getActionCommand().equals("stop"))
    {
        System.out.println("结束");
    }
}

3、输入框TextField的事件监听

1、写一个继承类,继承frame,运用frame中的属性,在这个类中,new了一个单行的文本框,并且添加到窗口中,当设置输入框的监听事件时,在另一个类中实现。

  class Myframe1 extends Frame{
//继承类需要一个构造器
public Myframe1(){
    TextField textField = new TextField();//可写单行文本
    //向窗口中添加文本框
    add(textField);

    //监听这个文本框输入的东西
    MyActionListener myActionListener = new MyActionListener();
    textField.addActionListener(myActionListener);
    //设置窗口的可见性
    setVisible(true);
    setBounds(400,400,400,400);
	//关闭窗口的监听事件
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    pack();
}

}
2、再写一个接口,用来实现对输入框的触发事件。
在这个类中,需要用e.getSouce()来获取资源,并且引用文本类型对e.getSouce()返回的对象进行强制转换成文本语言,然后输出输入框中的文本信息

  class MyActionListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {

    //用e.getSouce()来获取一些资源,这里需要用所监听的事件类型对所获得的资源进行强转
    TextField field = (TextField) e.getSource();
    System.out.println(field.getText());//获得输入框中的文本
}

}
3、拓展--设置替换编码
即将输入框中输入的文字进行替换,但是在后台中显示的仍然是替换前的文字信息
只需要在第1步的类中添加如下代码即可

  textField.setEchoChar('*');//用字符*来替换输入到文本框中的文字

若想输入之后清空文本框,则只需要在第2步的接口中加上如下代码
field.setText("");

  class MyActionListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {

    //用e.getSouce()来获取一些资源,这里需要用所监听的事件类型对所获得的资源进行强转
    TextField field = (TextField) e.getSource();
    System.out.println(field.getText());//获得输入框中的文本
    //清空文本框
    field.setText("");
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK