在上一个问题中,建议我使用以下格式将特定方法分配给特定的 Jbutton。每个按钮我都得到相同的错误。
public class MealPlannerGUI
{
JPanel foodOptions;
JButton pButton;
JButton cButton;
JButton fButton;
static JComboBox[] box= new JComboBox[3];
JPanel search;
JLabel searchL ;
JTextField foodSearch;
JButton startSearch;
JPanel foodProfile;
JLabel foodLabel;
JTextArea foodInfo;
JButton addFood;
JPanel currentStatus;
JLabel foodsEaten;
JComboBox foodsToday;
JLabel calories;
JTextArea totalKCal;
JButton clearInfo;
//ow Carbs
FoodListMaker tester = new FoodListMaker();
public void go()
{
JFrame frame = new JFrame();
foodOptions = new JPanel();
foodOptions.setBackground(Color.darkGray);
pButton = new JButton ("Add");
cButton = new JButton ("Add");
fButton = new JButton ("Add");
for (int i = 0; i<3; i++)
{
box[0] = new JComboBox (tester.groupProtein());
box[1] = new JComboBox (tester.groupKCal());
box[2] = new JComboBox (tester.groupFat());
//box[3]= new JComboBox (fillOptionPanel(carbFoods.groupCarb()));
foodOptions.add(box[i]);
}
search = new JPanel();
search.setBackground(Color.BLUE);
search.setLayout(new BoxLayout( search, BoxLayout.Y_AXIS));
searchL = new JLabel("Search for a food:");
//searchInfo= new JTextArea(10,20);
startSearch = new JButton ("Search");
//JScrollPane scrollPane = new JScrollPane(searchInfo);
foodSearch = new JTextField(10);
search.add(searchL);
search.add(foodSearch);
//search.add(searchInfo);
search.add(startSearch);
foodProfile = new JPanel();
foodLabel = new JLabel("Food Contents Profile:");
foodInfo = new JTextArea(10,20);;
addFood= new JButton ("Add");
foodsEaten= new JLabel ("Foods consumed today:");
foodsToday= new JComboBox();
calories= new JLabel ("Calorie Counter");
totalKCal = new JTextArea( 1 , 2);
foodProfile.add( foodLabel);
foodProfile.add( foodInfo);
foodProfile.add( addFood);
currentStatus = new JPanel();
currentStatus.setLayout(new BoxLayout( currentStatus, BoxLayout.Y_AXIS));
currentStatus.add( foodsEaten);
currentStatus.add( foodsToday);
currentStatus.add( calories);
currentStatus.add( totalKCal);
frame.getContentPane().add(BorderLayout.NORTH,foodOptions);
frame.getContentPane().add(BorderLayout.WEST,search);
frame.getContentPane().add(BorderLayout.CENTER,foodProfile);
frame.getContentPane().add(BorderLayout.EAST,currentStatus);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,800);
frame.setVisible(true);
}
startSearch.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent e)
{
String str = foodSearch.getText();
searchFoods(str, tester.getFoodList());
}
});
addFood.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent e)
{
foodsEaten = add(currentFood);
foodsToday = new JComboBox (foodsConsumed());
calorieTracker(currentFood.getKCal());
}
});
pButton.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent e)
{
String foodChoice = (String)box[0].getSelectedItem();
searchFoods(foodChoice, tester.getFoodList());
}
});
cButton.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent e)
{
String foodChoice = (String)box[1].getSelectedItem();
searchFoods(foodChoice, tester.getFoodList());
}
});
fButton.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent e)
{
String foodChoice = (String)box[2].getSelectedItem();
searchFoods(foodChoice, tester.getFoodList());
}
});
第一个错误是关于@Override 如何需要“;” 这不应该发生。剩下的错误是指上面代码的结构:
mealplans/MealPlannerGUI.java:104: <identifier> expected
startSearch.addActionListener(new ActionListener()
^
mealplans/MealPlannerGUI.java:104: illegal start of type
startSearch.addActionListener(new ActionListener()
^
mealplans/MealPlannerGUI.java:104: ')' expected
startSearch.addActionListener(new ActionListener()
^
mealplans/MealPlannerGUI.java:104: ';' expected
startSearch.addActionListener(new ActionListener()
^
mealplans/MealPlannerGUI.java:104: illegal start of type
startSearch.addActionListener(new ActionListener()
^
mealplans/MealPlannerGUI.java:104: <identifier> expected
startSearch.addActionListener(new ActionListener()