【问题标题】:Using Multiple Buttons with ActionEvent在 ActionEvent 中使用多个按钮
【发布时间】:2014-05-14 19:00:18
【问题描述】:

我已经得到了一些帮助,但是现在当我按下按钮时,什么也没有发生。我希望在您单击按钮时显示文本。如果是布局问题,我应该使用哪一个? FlowLayout 不适用于此程序,因为它会扭曲按钮。

import java.awt.*;
import java.awt.event.*;

public class Option3 extends Frame implements WindowListener,ActionListener
{
    Label l1;
    Label l2;
    Label l3;
    Button b1;
    Button b2;
    Button b3;
    public static void main(String[] args)
    {
        Option3 o3 = new Option3("Press a Button");
        o3.setSize(700,350);
        o3.setVisible(true);
    }
    public Option3(String option3)
    {
        super(option3);
        setLayout(null);
        addWindowListener(this);

        Label l1 = new Label();
        l1 = new Label();
        l1.setBounds(50,150,125,50);
        l1.setVisible(true);
        add(l1);

        Label l2 = new Label();
        l2 = new Label();
        l2.setBounds(275,150,125,50);
        l2.setVisible(true);
        add(l2);

        Label l3 = new Label();
        l3 = new Label();
        l3.setBounds(500,150,125,50);
        l3.setVisible(true);
        add(l3);

        Button b1 = new Button();
        b1 = new Button();
        b1.addActionListener(this);
        b1.setBounds(25,100,175,175);
        add(b1);

        Button b2 = new Button();
        b2 = new Button();
        addWindowListener(this);
        b2.addActionListener(this);
        b2.setBounds(250,100,175,175);
        add(b2);

        Button b3 = new Button();
        b3 = new Button();
        b3.addActionListener(this);
        b3.setBounds(475,100,175,175);
        add(b3);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == b1)
        {
            l1.setText("You pressed button 1.");
        }
        else if (e.getSource() == b2)
        {
            l2.setText("You pressed button 2.");
        }
        else if (e.getSource() == b3)
        {
            l3.setText("You pressed button 3.");
        }
    }
    public void windowClosing(WindowEvent e)
    {
        dispose();
        System.exit(0);
    }
    public void windowActivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
}

【问题讨论】:

    标签: java actionevent


    【解决方案1】:
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            // do stuff
        } else if (e.getSource() == b2) {
            // do other stuff
        }
    }
    

    e.getSource() 返回触发事件的对象引用。

    【讨论】:

    • 首先如果你没有改变它,你需要给你的按钮添加一个动作监听器,例如b1.addActionListener(this);。其次,您所有的类变量仍然是null,因为您正在创建本地对象Button b1 = new Button();,而不是初始化类变量b1 = new Button();。第三,您将标签的宽度和高度设置为 0,0。用l1.setBounds(10,10,50,50); x,y = 10,10 和 width,height = 50,50 调整它的大小。您可以使用这些值。
    • 我发布了我的更新代码——也许你可以在看到整个内容后提供帮助。
    • 您需要完全删除您的局部变量。 Label l1 = new Label(); 等不应该在那里。由于变量范围在 java 中的工作方式,您仍在将局部变量添加到框架中而不是初始化类变量。
    【解决方案2】:

    为此,您应该使用 actionCommand:

    //after object creation:
    firstButton.setActionCommand("upper");
    firstButton.addActionListener(this);// if this object is the listener
    secondButton.addActionListener(this);// if this object is the listener
    secondButton.setActionCommand("lower");
    

    然后在你的 actionPerformed() 中:

    public void actionPerformed(ActionEvent e) {
      String command = event.getActionCommand();
      if("upper".equals(command)){
        //Do something
      } else if("lower".equals(command)){
        //Do something
      }
    }
    

    【讨论】:

    • 这解决了它,但你下面有一个更简单的答案。还是谢谢。
    • @YoungDeezie 不同之处在于,如果您使用我的解决方案,您必须使用 getSource() 解决方案使您的按钮保持全局,而您可以在您想要的地方定义您的按钮。但它也可以!
    • 你永远不会跳入“actionPerformed-方法。你必须在每个按钮上注册监听器。像这样:b3.addActionListener(this);
    【解决方案3】:

    首先,我更喜欢 Swing 而不是 AWT。下面的代码显示了一个带有 3 个按钮的 JFrame。如果您单击一个按钮,它将显示一个对话框,告诉您单击了哪个按钮。

    我使用过 Lambda 表达式。所以,你必须有 jdk8 才能运行 以下代码。如果此代码不适合您,请不要打分。 去拿 jdk8。

    如果您对 Lambda 表达式不满意。然后按照以下说明进行操作:-

    创建一个内部类 ListenForButton implements ActionListener
    在重写的方法定义中编写逻辑。然后将此侦听器添加到您的按钮
    b1.addActionListener(new ListenForButton())


    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class Option3 extends JFrame {
    
        JButton b1, b2, b3;
        JPanel jp1;
    
        private void initComponents() {
            jp1 = new JPanel();
            add(jp1);
    
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            b1 = new JButton("Button1");
            b1.addActionListener(e -> {
                JOptionPane.showMessageDialog(this, "Button1 clicked");
            });
            jp1.add(b1);
    
            b2 = new JButton("Button2");
            b2.addActionListener(e -> {
                JOptionPane.showMessageDialog(this, "Button2 clicked");
            });
            jp1.add(b2);
    
            b3 = new JButton("Button3");
            b3.addActionListener(e -> {
                JOptionPane.showMessageDialog(this, "Button3 clicked");
            });
            jp1.add(b3);
            pack();
    
        }
    
        public static void main(String[] args) {
            new Option3("Press a Button").setVisible(true);
        }
    
        public Option3(String option3) {
            super(option3);
            initComponents();
            this.setLocationRelativeTo(null);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 2016-06-11
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      相关资源
      最近更新 更多