Java了解getSource()&动作听众

时间:2013-06-20 15:02:42

标签: java swing actionlistener actionevent

我正在尝试了解有关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);
    }
}

3 个答案:

答案 0 :(得分:4)

  1. 不要将保留的Java类和方法名称用作项目的名称。 public class applet extends...应为public class MyApplet extends....

  2. 使用正确的Java Naming Convention

  3. 使用JFrame代替JApplet,创建JFrame作为本地变量,而不是扩展JFrame,类似于private JTextField txtNameEingeben;

  4. 使用LayoutManager代替AbsoluteLayout (setBounds(...))

  5. 来自btnSave.addActionListener(new events());
  6. events()未声明

  7. 您应该使用event.getSource() == btnSave代替event.getSource(btnSave)

  8. 阅读有关How to Write an Action Listener

  9. 的Oracle教程

答案 1 :(得分:1)

ActionEvent#getSource()提供负责触发的组件。在按钮的情况下,Swing将按钮本身作为源。这意味着,您需要检查按钮是否为源。在您的代码中有两种可能性:

  1. 按钮指向字段:

    public class applet extends JApplet implements ActionListener {
    
      /**
     * 
     */
      private static final long serialVersionUID = -5561312464056465383L;
      private JTextField txtNameEingeben;
      private JTextField txtPwEingeben;
    
      private JButton btnSave;
    
      /**
       * Create the applet.
       */
      public applet() {
    
        ...
    
        lblNamePasswort.setHorizontalAlignment(SwingConstants.CENTER);
        lblNamePasswort.setBounds(0, 23, 450, 16);
        panel.add(lblNamePasswort);
    
        btnSave = new JButton("save");
        btnSave.setBounds(308, 251, 117, 29);
        panel.add(btnSave);
        btnSave.addActionListener(this);
    
      }
    
      public void actionPerformed(ActionEvent event) {
        if (btnSave.equals(event.getSource())) {
          save();
        }
      }
    
      public void save() {
        System.out.println("Button gedrückt.");
      }
      ...
    

    }

  2. 实现它的一种更好的方法,不是让整个applet实现Action侦听器,而只是使用内联实现。

          public class applet extends JApplet {
    
            private static final long serialVersionUID = -5561312464056465383L;
            private JTextField txtNameEingeben;
            private JTextField txtPwEingeben;
    
            /**
             * Create the applet.
             */
            public applet() {
              getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
    
              // left out
              ...              
    
              JButton btnSave = new JButton("save");
              btnSave.setBounds(308, 251, 117, 29);
              panel.add(btnSave);
              btnSave.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                  save();
                }
              });
    
            }
    
            public void save() {
              System.out.println("Button gedrückt.");
            }
    
            public static void main(String[] args) {
              applet applet1 = new applet();
              applet1.setVisible(true);
            }
          }
    

答案 2 :(得分:0)

您可以替换:

// (Where 'events' seems to come from nowhere)
btnSave.addActionListener(new events()); 

通过:

btnSave.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent event)
        {
            //if (event.getSource() == btnSave) { // Usefull only in case 2.
                System.out.println("Test Action");
            //}
        }
});     

你的听众可以是:

  1. 如上所述(匿名),或,
  2. 通过实例化一个实现ActionListener的你的类来创建(看here)。
  3. 对于选项2.当嵌入类实现ActionListener时,您可以将其作为参数传递:

    btnSave.addActionListener(this);
    


    注意: 此外,您应该遵循mKorbel的每一条建议。