【问题标题】:getSource() - What am I doing wrong here?getSource() - 我在这里做错了什么?
【发布时间】:2016-03-03 11:27:32
【问题描述】:

我不知道我在这里做错了什么。只是制作一个简单的程序来测试游戏的概念,我试图让三个按钮在单击时具有三个不同的输出。但是,对于按钮一、二和三,我收到一条错误消息,指出它们无法解析为变量。我不知道该怎么办。我做错了什么?

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);
    }



}

}

【问题讨论】:

  • 那些按钮非常悲观。不能加个new JButton("Click here to have all your programs work perfectly the first time and have unicorns show up at your doorstep");???
  • 避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正
  • ActionEventJButton 也支持actionCommand 属性的概念。除非另有说明,ActionEvent#getActionCommand 将返回按钮的文本,您可以使用 JButton#setActionCommand 方法更改此内容

标签: java swing jframe awt jbutton


【解决方案1】:

JButtons 在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);
    }
}
}

【讨论】:

    【解决方案2】:

    您的 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);
    // ...
    

    【讨论】:

      【解决方案3】:

      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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-28
        • 2023-01-19
        • 1970-01-01
        • 2020-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多