我一直收到上述错误,我确定它很简单,但即使在查找之后我也无法弄明白。我对菜单栏也有一些问题,但我现在不关心它。据我所知,为了在actionPerformed方法中返回所有这些数字,它不能无效。但是当我将其更改为int时,我会收到标题中声明的错误。如果它是void而不是int,则错误更改为"无效时无法返回。"
从发布此问题的其他人看来,当他们宣布它时,他们似乎没有使用actionlistener,但我认为我是。
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;
}
}
答案 0 :(得分:2)
public int actionPerformed(ActionEvent e)
此方法不应返回int
或其他任何内容。它应该返回void
。覆盖方法时,必须保留相同的签名(除少数例外 - 返回类型不是其中之一)
更改为"cannot return when void."
时收到此void
错误,因为void
方法不应返回任何内容,并且您尝试返回int
值。你需要摆脱回归的价值。
最重要的是,你正试图回报一百万件事。只允许返回一个项/对象/值。
如果您尝试返回的值应该对ui的其余部分产生影响,那么在actionPerformed
方法中进行这些更改
附注:
请参阅How do I compare Strings in Java以帮助您解决此问题
if (styleList.getSelectedItem() == "Economy")
使用@Override
注释,因此您知道是否正确覆盖了某个方法。如果你不是并且你正在实现一个接口(即`ActionListener),并且你的类不是抽象的,你将得到你当前正在接收的错误
@Override
public void actionPerformed(ActionEvent e) {}