嘿伙计们,所以我刚开始练习Java的图形部分,我遇到了一个我从未见过的错误。
我要发布我的代码,然后错误消息的图像会提示我,因为我对实际发生的事情一无所知,我有一个想法,但不要让自己感到困惑,所以我想也许这里有人有类似的问题?多谢你们! (以netbeans开发)
程序在尝试单击任一按钮时运行并抛出此错误,错误消息行指的是代码中包含的覆盖方法的行号
在我运行程序后自动生成覆盖方法 尝试点击两个按钮中的任何一个时,它会抛出错误(上图)和错误
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
*
* @author Jonathan
*/
class WindowDestroyer extends WindowAdapter
{
public void window(WindowEvent e)
{
System.exit(0);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package game;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Set;
/**
*
* @author Jonathan
*/
public class ButtonDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
public ButtonDemo()
{
setSize(WIDTH, HEIGHT);
WindowDestroyer listener = new WindowDestroyer();
addWindowListener(listener);
Container contentPane = getContentPane();
contentPane.setBackground(Color.white);
contentPane.setLayout(new FlowLayout());
JButton sunnyButton = new JButton("Sunny");
sunnyButton.addActionListener(this);
contentPane.add(sunnyButton);
JButton cloudyButton = new JButton("Cloudy");
cloudyButton.addActionListener(this);
contentPane.add(cloudyButton);
}
public void actionPreformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
Container contentPane = getContentPane();
if(actionCommand.equals("Sunny"))
contentPane.setBackground(Color.BLUE);
else if(actionCommand.equals("Cloudy"))
contentPane.setBackground(Color.GRAY);
else
System.out.println("Error in button interface");
}
@Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
答案 0 :(得分:1)
您希望它调用的方法名称不正确。它自动生成
>>> sa([89, 23, 33, 45, 10, 12, 45, 45, 45])
[89, 23, 33, 45, 10, 12, 45, 45, 45]
[10, 45, 45, 45, 45, 45, 45, 45, 45]
因为您宣布:
public void actionPerformed(ActionEvent ae)
不确定" Preformed"手段。 : - )
自动生成的actionPerformed会抛出你提到的异常。