I keep getting the above error, I'm sure it's something simple but I can't figure it out even after looking it up. I'm also having some problems with the menubar, but I dont care about that right now. From what I understand, in order to return all those numbers in the actionPerformed method it cant be void. But when I change it to int I get the error stated in the title. When it was void and not int, the error changed to "cannot return when void."
From others who posted this problem it seemed like they werent using actionlistener when they declared it, but I think I am.
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;
}
}