有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java getSource()我做错了什么?

我不知道我做错了什么。我只是制作了一个简单的程序来测试游戏的概念,我试着在点击三个按钮时有三个不同的输出。但是,对于按钮1、按钮2和按钮3,我得到一个错误,它们不能解析为变量。我不知道该怎么办。我做错了什么

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

public class CodeTestingGround extends JFrame implements ActionListener {

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
{
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    new CodeTestingGround();

}

public CodeTestingGround() {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    JFrame frameone = new JFrame();
    frameone.setLayout(null);

    frameone.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frameone.setLocation(screenSize.width / 3, screenSize.height / 3);

    JButton buttonone = new JButton("Click here to download viruses!");
    JButton buttontwo = new JButton("Click here to get scammed!");
    JButton buttonthree = new JButton("Click here to get hacked!");
    buttonone.setBounds(10, 10, 260, 30);
    buttontwo.setBounds(10, 50, 260, 30);
    buttonthree.setBounds(10, 90, 260, 30);
    buttonone.addActionListener(this);
    buttontwo.addActionListener(this);
    buttonthree.addActionListener(this);
    frameone.add(buttonone);
    frameone.add(buttontwo);
    frameone.add(buttonthree);
    frameone.pack();
    frameone.setVisible(true);
    frameone.setSize(300, 400);

}

public void actionPerformed(ActionEvent event) {
    Object control = event.getSource();
    if (control == buttonone) { // error right here
        JOptionPane.showMessageDialog(null, "Viruses sucessfully downloaded!", "Important Alert", JOptionPane.WARNING_MESSAGE);
    }
    else if (control == buttontwo) { // error right here
        JOptionPane.showMessageDialog(null, "YOU HAVE WON A MILLION DOLLARS!!! Enter you credit card information to claim your prize.", "YOU ARE WIN", JOptionPane.INFORMATION_MESSAGE);
    }
    else if (control == buttonthree) { // error right here
        JOptionPane.showMessageDialog(null, "You have been haxored", "get hacked", JOptionPane.ERROR_MESSAGE);
    }



}

}

共 (1) 个答案

  1. # 1 楼答案

    ActionEventJButton也支持actionCommand属性的概念。除非另有规定,ActionEvent#getActionCommand将返回按钮的文本,您可以使用JButton#setActionCommand方法更改此内容

    JButton buttonone = new JButton("Click here to download viruses!");
    buttonone.setActionCommand("bad");
    JButton buttontwo = new JButton("Click here to get scammed!");
    buttonone.setActionCommand("ugly");
    JButton buttonthree = new JButton("Click here to get hacked!");
    buttonone.setActionCommand("hacked");
    

    然后使用ActionEventactionCommand属性

    public void actionPerformed(ActionEvent event) {
        String cmd = event.getActionCommand();
        if ("bad".equals(cmd)) { // error right here
            JOptionPane.showMessageDialog(null, "Viruses sucessfully downloaded!", "Important Alert", JOptionPane.WARNING_MESSAGE);
        }
        else if ("ugly".equals(cmd)) { // error right here
            JOptionPane.showMessageDialog(null, "YOU HAVE WON A MILLION DOLLARS!!! Enter you credit card information to claim your prize.", "YOU ARE WIN", JOptionPane.INFORMATION_MESSAGE);
        }
        else if ("hacked".equals(cmd)) { // error right here
            JOptionPane.showMessageDialog(null, "You have been haxored", "get hacked", JOptionPane.ERROR_MESSAGE);
        }
    }