【问题标题】:Java doubts about ActionEventJava对ActionEvent的质疑
【发布时间】:2022-02-02 17:50:38
【问题描述】:

这是我在这个网站上的第一个问题。 我有这个问题,在这个类中,我有两个具有两种不同功能的按钮,一个用于退出,另一个用于将名字和姓氏放在文本字段中。 我无法让第二个 ActionEvent 工作,请帮助我,谢谢。

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

public class Prueba1 extends JFrame implements ActionListener{
    
    private JLabel nombre, apellidos,respondo;
    private JTextField textfield, textfield1;
    private JButton boton,botonoff;
    
    public Prueba1() {
        setLayout(null);
        
        nombre = new JLabel("Nombre:");
        nombre.setBounds(10, 10, 300, 30);
        add(nombre);
        
        apellidos = new JLabel("Apellidos");
        apellidos.setBounds(10, 40, 300, 30);
        add(apellidos);
        
        textfield = new JTextField();
        textfield.setBounds(100,10,150,20);
        add(textfield);
        
        textfield1 = new JTextField();
        textfield1.setBounds(100,40,150,20);
        add(textfield1);
        
        boton = new JButton("¿Que saldrá?");
        boton.setBounds(10,80,120,30);
        boton.addActionListener(this);
        add(boton);
        
        botonoff = new JButton("Salir");
        botonoff.setBounds(10,120,120,30);
        botonoff.addActionListener(this);
        add(botonoff);
        
        respondo = new JLabel("UwU");
        respondo.setBounds(160,80,300,30);
        add(respondo);
    }
    
    public void actionPerformed(ActionEvent e) {
        
        
        if(e.getSource() == boton) {
            String nombreyapellidos, nombre1, apellidos1;
            nombre1 = textfield.getText();
            apellidos1 = textfield1.getText();
            
            nombreyapellidos = nombre1 + apellidos1;
            
            respondo.setText(nombreyapellidos);
            
        }
        
    
    
    }
    
public void actionPerformed1(ActionEvent e) {
        
        
        if(e.getSource() == botonoff) {
            System.exit(0);
            
            
        }
    
    }
    
    public static void main(String args[]) {
        
        Prueba1 clase = new Prueba1();
        clase.setVisible(true);
        clase.setBounds(0, 0, 500, 500);
        clase.setResizable(true);
        clase.setLocationRelativeTo(null);
    }
    
}

【问题讨论】:

    标签: java actionevent


    【解决方案1】:

    删除public void actionPerformed1(ActionEvent e) 方法并将该方法的主体添加到public void actionPerformed(ActionEvent e) 主体的else 分支中。

    
      public void actionPerformed(ActionEvent e) {
    
        if (e.getSource() == boton) {
          String nombreyapellidos, nombre1, apellidos1;
          nombre1 = textfield.getText();
          apellidos1 = textfield1.getText();
    
          nombreyapellidos = nombre1 + apellidos1;
    
          respondo.setText(nombreyapellidos);
    
        } else if (e.getSource() == botonoff) {
          System.exit(0);
        }
    
      }
    

    【讨论】:

    • 我已经把他告​​诉我的东西放进去,它对我有用,谢谢
    【解决方案2】:

    当您向按钮提供ActionListener 对象时 button.addActionListener(listener)

    你有几种方法可以做到这一点。

     button.addActionListener(this);
    

    只有一种方式。这种方式表示该类实现了 ActionListener。 实际上它实现了

    public void actionPerformed(ActionEvent e)
    

    方法。

    你的

    public void actionPerformed1(ActionEvent e)
    

    按钮根本无法使用。

    幸运的是,还有许多其他方法可以描述在产生动作事件时应该执行的代码。

    一个内部类,不管是静态的还是非静态的。其他类/对象。 一个 lambda 表达式。

    您可以找到如何表达 lambda here

    【讨论】:

    • 我已经明白了,非常感谢,现在我会看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2020-08-01
    • 2015-01-01
    • 2014-02-03
    • 2016-11-28
    • 2023-03-29
    相关资源
    最近更新 更多