[英]getSource() - What am I doing wrong here?
我不知道我在做什么錯。 只是編寫一個簡單的程序來測試游戲的概念,而我試圖使三個按鈕在單擊時具有三個不同的輸出。 但是,對於按鈕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);
}
}
}
ActionEvent
和JButton
還支持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");
然后,您將使用ActionEvent
的actionCommand
屬性...
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);
}
}
JButton是在CodeTestingGround()
中定義的,這意味着無法在該方法之外訪問它們。 當您嘗試從actionPerformed訪問它們時,JButton不在范圍內。
您可以通過簡單地移動聲明來解決此問題:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CodeTestingGround extends JFrame implements ActionListener {
//Declarations go here instead
JButton buttonone;
JButton buttontwo;
JButton buttonthree;
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);
buttonone = new JButton("Click here to download viruses!");
buttontwo = new JButton("Click here to get scammed!");
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);
}
}
}
您的buttons
不在詞法范圍內。 使它們成為字段,例如
private JButton buttonone = new JButton("Click here to download viruses!");
private JButton buttontwo = new JButton("Click here to get scammed!");
private JButton buttonthree = new JButton("Click here to get hacked!");
並從CodeTestingGround
構造函數中刪除聲明和初始化。 類似於(或僅刪除三行)
// 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);
// ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.