Java 找不到符号,Java 无法在数组中找到我的符号?

发布于 2024-10-17 01:41:45 字数 4202 浏览 5 评论 0原文

这是我编写这行代码时遇到的错误。

symbol  : variable isSeletecd
location: class java.lang.String
if (dorm[0].isSeletecd && meal[0].isSeletecd())
           ^

if (dorm[0].isSeletecd && meal[0].isSeletecd())
totalCharges.setText("2150.00");

程序:

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

/** 

The Main class creates the GUI for the Dorm and 
Meal charges. 
*/ 

public class Main extends JFrame 
{ 
private JPanel dormPanel; 
private JComboBox dormBox; 
private JPanel mealPanel; 
private JComboBox mealBox; 
private JPanel totalChargesPanel; 
private JPanel selectedMealPanel; 
private JPanel buttonPanel; 
private JButton calcButton; 
private JLabel label1; 
private JTextField totalCharges; 

private String[] dorm = { "Allen Hall: $1,500 per semester", 
"Pike Hall: $1,600 per semester", 
"Farthing Hall: $1,200 per semester", 
"University Suites: $1,800 pe r semester"}; 

private String[] meal = { "7 meals per week: $650 per semester", 
"14 meals per week: $1,095 per semester", 
"Unlimited meals: $1,500 per semester"}; 

/** 
Constructor 
*/ 
public Main() 
{ 
super("Dormitory and Meal Plan"); 

// Specify an action for the close button. 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

// Create a BorderLayout manager. 
setLayout(new BorderLayout()); 

// Create the dorm and meal panel. 
buildDormPanel(); 
buildMealPanel(); 
buildSelectedTotalChargesPanel(); 
buildButtonPanel(); 

// Add the components to the content pane. 
add(dormPanel, BorderLayout.WEST); 
add(mealPanel, BorderLayout.EAST); 
add(totalChargesPanel, BorderLayout.SOUTH); 
add(buttonPanel, BorderLayout.NORTH); 

// Pack the contents of the window and display it. 
pack(); 
setVisible(true); 
} 

// The buildDormPanel method builds the dorm panel. 
private void buildDormPanel() 
{ 
// Create the dorm panel. 
dormPanel = new JPanel(); 
dormBox = new JComboBox(dorm); 

// Register the action listener. 
dormBox.addActionListener(new ComboBoxListener()); 

// Add the dorm panel to the panel. 
dormPanel.add(dormBox); 
} 

// The buildMealPanel method builds the meal panel. 
private void buildMealPanel() 
{ 
// Create the meal panel. 
mealPanel = new JPanel(); 
mealBox = new JComboBox(meal); 

// Register the action listener. 
mealBox.addActionListener(new ComboBoxListener()); 

// Add the meal panel to the panel. 
mealPanel.add(mealBox); 
} 

// The buttonPanel method builds the bottun panel. 
private void buildButtonPanel() 
{ 
// Create a panel. 
buttonPanel = new JPanel(); 

// Create a button. 
calcButton = new JButton("Calculate"); 

// Register an action listener with the button. 
calcButton.addActionListener(new ButtonListener()); 

// Add the button to the panel. 
buttonPanel.add(calcButton); 
} 

// The buildSelectedDormPanel builds the selected totalCharges panel. 
private void buildSelectedTotalChargesPanel() 
{ 
// Create the totalChargesPanel for the label. 
totalChargesPanel = new JPanel(); 
label1 = new JLabel("Total charges per semester: "); 

// Create the totalCharges textfield. 
totalCharges = new JTextField (25); 
totalCharges.setEditable(false); 

// Add the totalChargesPanel to the panel. 
totalChargesPanel.add(label1); 
totalChargesPanel.add(totalCharges); 
} 

/** Private inner class that handles the event when the user 
selects the dorm and meal boxes. 
*/ 
private class ComboBoxListener implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
// Variables to hold the dorm, meal, and total charges. 
String dorm = (String) dormBox.getSelectedItem(); 
String meal = (String) mealBox.getSelectedItem(); 

// Calculates the total. 
totalCharges.setText(meal + dorm); 
} 
} 

private class ButtonListener implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
if (dorm[0].isSeletecd && meal[0].isSeletecd())
totalCharges.setText("2150.00");
} 
} 

public static void main(String[] args) 
{ 
new Main(); 
} 
} 

This is the error i get when i write this line of code.

symbol  : variable isSeletecd
location: class java.lang.String
if (dorm[0].isSeletecd && meal[0].isSeletecd())
           ^

if (dorm[0].isSeletecd && meal[0].isSeletecd())
totalCharges.setText("2150.00");

Program:

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

/** 

The Main class creates the GUI for the Dorm and 
Meal charges. 
*/ 

public class Main extends JFrame 
{ 
private JPanel dormPanel; 
private JComboBox dormBox; 
private JPanel mealPanel; 
private JComboBox mealBox; 
private JPanel totalChargesPanel; 
private JPanel selectedMealPanel; 
private JPanel buttonPanel; 
private JButton calcButton; 
private JLabel label1; 
private JTextField totalCharges; 

private String[] dorm = { "Allen Hall: $1,500 per semester", 
"Pike Hall: $1,600 per semester", 
"Farthing Hall: $1,200 per semester", 
"University Suites: $1,800 pe r semester"}; 

private String[] meal = { "7 meals per week: $650 per semester", 
"14 meals per week: $1,095 per semester", 
"Unlimited meals: $1,500 per semester"}; 

/** 
Constructor 
*/ 
public Main() 
{ 
super("Dormitory and Meal Plan"); 

// Specify an action for the close button. 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

// Create a BorderLayout manager. 
setLayout(new BorderLayout()); 

// Create the dorm and meal panel. 
buildDormPanel(); 
buildMealPanel(); 
buildSelectedTotalChargesPanel(); 
buildButtonPanel(); 

// Add the components to the content pane. 
add(dormPanel, BorderLayout.WEST); 
add(mealPanel, BorderLayout.EAST); 
add(totalChargesPanel, BorderLayout.SOUTH); 
add(buttonPanel, BorderLayout.NORTH); 

// Pack the contents of the window and display it. 
pack(); 
setVisible(true); 
} 

// The buildDormPanel method builds the dorm panel. 
private void buildDormPanel() 
{ 
// Create the dorm panel. 
dormPanel = new JPanel(); 
dormBox = new JComboBox(dorm); 

// Register the action listener. 
dormBox.addActionListener(new ComboBoxListener()); 

// Add the dorm panel to the panel. 
dormPanel.add(dormBox); 
} 

// The buildMealPanel method builds the meal panel. 
private void buildMealPanel() 
{ 
// Create the meal panel. 
mealPanel = new JPanel(); 
mealBox = new JComboBox(meal); 

// Register the action listener. 
mealBox.addActionListener(new ComboBoxListener()); 

// Add the meal panel to the panel. 
mealPanel.add(mealBox); 
} 

// The buttonPanel method builds the bottun panel. 
private void buildButtonPanel() 
{ 
// Create a panel. 
buttonPanel = new JPanel(); 

// Create a button. 
calcButton = new JButton("Calculate"); 

// Register an action listener with the button. 
calcButton.addActionListener(new ButtonListener()); 

// Add the button to the panel. 
buttonPanel.add(calcButton); 
} 

// The buildSelectedDormPanel builds the selected totalCharges panel. 
private void buildSelectedTotalChargesPanel() 
{ 
// Create the totalChargesPanel for the label. 
totalChargesPanel = new JPanel(); 
label1 = new JLabel("Total charges per semester: "); 

// Create the totalCharges textfield. 
totalCharges = new JTextField (25); 
totalCharges.setEditable(false); 

// Add the totalChargesPanel to the panel. 
totalChargesPanel.add(label1); 
totalChargesPanel.add(totalCharges); 
} 

/** Private inner class that handles the event when the user 
selects the dorm and meal boxes. 
*/ 
private class ComboBoxListener implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
// Variables to hold the dorm, meal, and total charges. 
String dorm = (String) dormBox.getSelectedItem(); 
String meal = (String) mealBox.getSelectedItem(); 

// Calculates the total. 
totalCharges.setText(meal + dorm); 
} 
} 

private class ButtonListener implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
if (dorm[0].isSeletecd && meal[0].isSeletecd())
totalCharges.setText("2150.00");
} 
} 

public static void main(String[] args) 
{ 
new Main(); 
} 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恍梦境° 2024-10-24 01:41:45

dormmeal 是字符串数组,上没有 isSeletecd 字段或 isSelected() 方法java.lang.String

dorm and meal are String arrays, there is no isSeletecd field or isSelected() method on a java.lang.String.

淡淡離愁欲言轉身 2024-10-24 01:41:45

dormmeal 是字符串数组

String 类没有 isSelected() 方法,但有一个JComboBoxgetSelectedIndex() 方法,可用于获取 JComboBox 组件中当前所选项目的索引号。

通过将 ButtonListener 类中的 if 语句中的条件替换

if (dorm[0].isSeletecd && meal[0].isSeletecd())

to

if(dormBox.getSelectedIndex()==0 && mealBox.getSelectedIndex()==0)

将解决您的问题。

因此,您的 ButtonListener 类现在将是

private class ButtonListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
        if(dormBox.getSelectedIndex()==0 && mealBox.getSelectedIndex()==0)
            totalCharges.setText("2150.00");
    } 
} 

Reffer getSelectedIndex() 了解更多信息。

dorm and meal are String arrays

There is no isSelected() method for the String class, but there is a getSelectedIndex() method for JComboBox which can be used to get the index no of the currently selected item in the JComboBox component.

By replacing the condition in the if statement in your ButtonListener class

from

if (dorm[0].isSeletecd && meal[0].isSeletecd())

to

if(dormBox.getSelectedIndex()==0 && mealBox.getSelectedIndex()==0)

will solve your problem.

So your ButtonListener class will now be

private class ButtonListener implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
        if(dormBox.getSelectedIndex()==0 && mealBox.getSelectedIndex()==0)
            totalCharges.setText("2150.00");
    } 
} 

Reffer getSelectedIndex() for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文