Java &引用;不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent)”;
我不断得到上面的错误,我相信这是一些简单的东西,但我无法找出它,即使在查找后。我在菜单栏上也有一些问题,但我现在不在乎。据我所知,为了返回actionPerformed方法中的所有数字,它不能为空。但是当我把它改为int时,我得到了标题中所述的错误。当它是void而不是int时,错误变为“void时无法返回” 从发布这个问题的其他人看来,他们声明这个问题时似乎没有使用actionlistener,但我想我是这样Java &引用;不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent)”;,java,swing,actionlistener,Java,Swing,Actionlistener,我不断得到上面的错误,我相信这是一些简单的东西,但我无法找出它,即使在查找后。我在菜单栏上也有一些问题,但我现在不在乎。据我所知,为了返回actionPerformed方法中的所有数字,它不能为空。但是当我把它改为int时,我得到了标题中所述的错误。当它是void而不是int时,错误变为“void时无法返回” 从发布这个问题的其他人看来,他们声明这个问题时似乎没有使用actionlistener,但我想我是这样 import java.io.*; import java.awt.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
public class Widget extends JFrame implements ActionListener
{
DataOutputStream output;
String[] styleStrings = { "Economy", "Standard", "Advanced", "Exceptional" };
String[] colorStrings = { "Red", "Green", "Blue", "Yellow" };
int economyCount, standardCount, advancedCount, exceptionalCount, redCount, greenCount, blueCount, yellowCount, entryValue;
JPanel economyPanel = new JPanel();
JPanel standardPanel = new JPanel();
JPanel advancedPanel = new JPanel();
JPanel exceptionalPanel = new JPanel();
JPanel redPanel = new JPanel();
JPanel greenPanel = new JPanel();
JPanel bluePanel = new JPanel();
JPanel yellowPanel = new JPanel();
JLabel selectStyle = new JLabel("Select a style: ");
JComboBox styleList = new JComboBox(styleStrings);
JLabel selectColor = new JLabel("Select a color: ");
JComboBox colorList = new JComboBox(colorStrings);
JLabel enterAmount = new JLabel("Enter amount: ");
JTextField enterField = new JTextField(5);
JButton submitButton = new JButton("Submit");
JLabel blankLabel = new JLabel("");
JLabel totalLabel = new JLabel("Total Widgets:");
JLabel economyLabel = new JLabel("Economy: " + economyCount);
JLabel standardLabel = new JLabel("Standard: " + standardCount);
JLabel advancedLabel = new JLabel("Advanced: " + advancedCount);
JLabel exceptionalLabel = new JLabel("Exceptional: " + exceptionalCount);
JLabel redLabel = new JLabel("Red: " + redCount);
JLabel greenLabel = new JLabel("Green: " + greenCount);
JLabel blueLabel = new JLabel("Blue: " + blueCount);
JLabel yellowLabel = new JLabel("Yellow: " + yellowCount);
public static void main(String[] args)
{
Widget w = new Widget();
w.setSize(500, 500);
w.setTitle("ABC Computers Inventory Management System");
w.setResizable(true);
w.setLocation(300,300);
w.setVisible(true);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Widget()
{
Container c = getContentPane();
c.setLayout((new GridLayout(9,2)));
FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT);
economyPanel.setLayout(rowSetup);
standardPanel.setLayout(rowSetup);
advancedPanel.setLayout(rowSetup);
exceptionalPanel.setLayout(rowSetup);
redPanel.setLayout(rowSetup);
greenPanel.setLayout(rowSetup);
bluePanel.setLayout(rowSetup);
yellowPanel.setLayout(rowSetup);
economyPanel.add(economyLabel);
standardPanel.add(standardLabel);
advancedPanel.add(advancedLabel);
exceptionalPanel.add(exceptionalLabel);
redPanel.add(redLabel);
greenPanel.add(greenLabel);
bluePanel.add(blueLabel);
yellowPanel.add(yellowLabel);
c.add(selectStyle);
c.add(selectColor);
c.add(styleList);
c.add(colorList);
c.add(enterAmount);
c.add(enterField);
c.add(submitButton);
c.add(blankLabel);
c.add(totalLabel);
c.add(blankLabel);
c.add(economyPanel);
c.add(redPanel);
c.add(standardPanel);
c.add(greenPanel);
c.add(advancedPanel);
c.add(bluePanel);
c.add(exceptionalPanel);
c.add(yellowPanel);
styleList.setSelectedIndex(1);
styleList.setEditable(false);
styleList.addActionListener(this);
colorList.setSelectedIndex(1);
colorList.setEditable(false);
colorList.addActionListener(this);
submitButton.addActionListener(this);
String filename = "widgettotals";
}
public JMenuBar createMenuBar()
{
JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);
JMenu mnuFile = new JMenu("File", true);
mnuFile.setMnemonic(KeyEvent.VK_F);
mnuFile.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuFile);
JMenuItem mnuFileExit = new JMenuItem("Exit");
mnuFileExit.setMnemonic(KeyEvent.VK_X);
mnuFileExit.setDisplayedMnemonicIndex(1);
mnuFile.add(mnuFileExit);
mnuFileExit.setActionCommand("Exit");
mnuFileExit.addActionListener(this);
JMenu mnuHelp = new JMenu("Help", true);
mnuHelp.setMnemonic(KeyEvent.VK_E);
mnuHelp.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuHelp);
return mnuBar;
}
public int actionPerformed(ActionEvent e)
{
economyCount = 0;
standardCount = 0;
advancedCount = 0;
exceptionalCount = 0;
redCount = 0;
greenCount = 0;
blueCount = 0;
yellowCount = 0;
String chosenStyle = (String)styleList.getSelectedItem();
String chosenColor = (String)colorList.getSelectedItem();
String entryNumbers = enterField.getText();
int entryValue = Integer.parseInt(entryNumbers);
if ("Submit".equals(e.getActionCommand()))
{
if (styleList.getSelectedItem() == "Economy")
{
economyCount = economyCount + entryValue;
}
if (styleList.getSelectedItem() == "Standard")
{
standardCount = standardCount + entryValue;
}
if (styleList.getSelectedItem() == "Advanced")
{
advancedCount = advancedCount + entryValue;
}
if (styleList.getSelectedItem() == "Exceptional")
{
exceptionalCount = exceptionalCount + entryValue;
}
if (colorList.getSelectedItem() == "Red")
{
redCount = redCount + entryValue;
}
if (colorList.getSelectedItem() == "Green")
{
greenCount = greenCount + entryValue;
}
if (colorList.getSelectedItem() == "Blue")
{
blueCount = blueCount + entryValue;
}
if (colorList.getSelectedItem() == "Yellow")
{
yellowCount = yellowCount + entryValue;
}
}
return economyCount; return standardCount; return advancedCount; return exceptionalCount;
return redCount; return greenCount; return blueCount; return yellowCount;
}
}
此方法不应返回int
,或与此相关的任何内容。它应该返回void
。重写方法时,必须保留相同的签名(只有少数例外-返回类型不是其中之一)
当更改为void
时,会出现此错误,因为void
方法不应返回任何内容,并且您正在尝试返回int
值。您需要去掉返回的值
最重要的是,你正试图归还一百万件东西。只允许返回一个项/对象/值
如果您试图返回的那些值被认为对ui的其余部分有影响,那么在actionPerformed
方法中进行这些更改
旁注:
- 看看能不能帮你解决这个问题
if (styleList.getSelectedItem() == "Economy")
- 使用
@Override
注释,以便知道是否正确地重写了方法。如果您没有,并且正在实现一个接口(即“ActionListener”),并且您的类不是抽象的,那么您将得到当前收到的错误
@Override
public void actionPerformed(ActionEvent e) {}
非常感谢您迄今为止的帮助,我已经将==改为.equals(),并删除了所有返回值。但是我有一个问题,当你说“在actionPerformed方法中进行那些更改”时,你的意思是将代码从Widget()方法移动到actionPerformed方法中吗?再次感谢您的帮助。除非我知道您通过返回这些值试图实现的目标,否则我无法回答您的问题。你期望发生什么?好的,我知道你想做什么。使用label.setText()
。增加计数后,只需在相应标签上使用setText
。比如economyCount=economyCount+entryValue;economyLabel.setText(“经济:+economyCount”)编码>并对所有其他标签执行该操作。不需要返回任何完美的东西!这正是我所需要的,我以为事情会比这复杂得多。你是救命恩人!
@Override
public void actionPerformed(ActionEvent e) {}