It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                6年前关闭。
            
        

我正在尝试了解有关actionListeners的更多信息。

如果单击“保存”按钮,则尝试打印消息“测试操作”。
无论如何,我一点都不明白。

这是我的代码,希望任何人都可以帮助我。提前致谢。

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class applet extends JApplet implements ActionListener {

    private static final long serialVersionUID = -5561312464056465383L;
    private JTextField txtNameEingeben;
    private JTextField txtPwEingeben;

    public applet() {
        getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
        JPanel panel = new JPanel();
        panel.setBackground(Color.DARK_GRAY);
        getContentPane().add(panel);
        panel.setLayout(null);
        JLabel lblANewLabel = new JLabel("Name");
        lblANewLabel.setHorizontalAlignment(SwingConstants.LEFT);
        lblANewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
        lblANewLabel.setBounds(33, 57, 117, 37);
        lblANewLabel.setForeground(Color.WHITE);
        panel.add(lblANewLabel);
        //TEXTFELD NAME
        txtNameEingeben = new JTextField();
        txtNameEingeben.setText("");
        txtNameEingeben.setBounds(162, 64, 134, 28);
        panel.add(txtNameEingeben);
        txtNameEingeben.setColumns(10);
        //TEXTFELD PASSWORT
        txtPwEingeben = new JTextField();
        txtPwEingeben.setText("");
        txtPwEingeben.setBounds(162, 113, 134, 28);
        panel.add(txtPwEingeben);
        txtPwEingeben.setColumns(10);
        //LABEL ÜBERSCHRIFT
        JLabel lblNamePasswort = new JLabel("Name & Passwort in einem Array     speichern");
        lblNamePasswort.setForeground(Color.WHITE);
        lblNamePasswort.setHorizontalAlignment(SwingConstants.CENTER);
        lblNamePasswort.setBounds(0, 23, 450, 16);
        panel.add(lblNamePasswort);
        JButton btnSave = new JButton("save");
        btnSave.setBounds(308, 251, 117, 29);
        panel.add(btnSave);
        btnSave.addActionListener(new events());
    }

    public void save(ActionEvent event) {
        System.out.println("Button gedrückt.");
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource(btnSave)) {
            System.out.println("Test Action");
        }
    }

    public static void main(String[] args) {
        applet applet1 = new applet();
        applet1.setVisible(true);
    }
}

最佳答案

不要将保留的Java类和方法名称用作项目名称。 public class applet extends...应该是public class MyApplet extends....
使用正确的Java Naming Convention
使用JFrame代替JApplet,创建JFrame作为局部变量,而不是扩展JFrame,类似于private JTextField txtNameEingeben;
使用LayoutManager代替AbsoluteLayout (setBounds(...))
来自btnSave.addActionListener(new events());events()未声明
您应该使用event.getSource() == btnSave而不是event.getSource(btnSave)
阅读有关How to Write an Action Listener的Oracle教程

10-05 17:56