【问题标题】:Why is my program is not finding the getSelectedIndex() method that is trying to get a selection from a JComboBox?为什么我的程序没有找到试图从 JComboBox 中获取选择的 getSelectedIndex() 方法?
【发布时间】:2014-04-01 18:42:22
【问题描述】:

我的程序没有找到符号 getSelectedIndex() 方法。我想知道为什么它没有找到它以及如何解决这个问题。 这是错误:
错误:找不到符号
符号:方法 getSelectedIndex()
位置:变量 roomChoice 类型为 java.lang.String[]

这是我的代码:

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  

public class CottageRental2 extends JFrame implements ItemListener{
  // Declare all instance data  (primitives and objects used) 
  private int WIDTH = 676;
  private int HEIGHT = 321; 
  Container con; 
  private JLabel label1;
  private JLabel label2;
  private JLabel label3;
  JComboBox  cbox1;
  JComboBox cbox2;
  String [ ] roomChoice = {"1 Bedroom: $600","2 Bedroom: $800","3 Bedroom: $1000"};
 String [] activityChoice = {"Horse Back Riding: $60","Rafting: $40","Row Boat Rental:       $50"};
  ImageIcon icon1 = new ImageIcon("C:\\Users\\Coding\\Desktop\\cottage.jpeg");
  Font f1 = new Font("Ariel", Font.BOLD, 30);

//constructor 
  public CottageRental2(){
    super("Cottage Rental"); 
    con = getContentPane(); 
    con.setLayout(new BorderLayout()); 
    con.setBackground(Color.GRAY);
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
}

  public void createGUI(){
    label1 = new JLabel("Woodberry Cottage Rental", JLabel.CENTER);
    label1.setFont(f1);
    label1.setForeground(Color.WHITE);
    label2 = new JLabel("Rental Amount Due: ", JLabel.CENTER);
    label2.setFont(f1);
    label2.setForeground(Color.WHITE);
    label3 = new JLabel(icon1);
    cbox1 = new JComboBox();
    cbox1.addItem(roomChoice);
    cbox2 = new JComboBox();
    cbox2.addItem(activityChoice);
    con.add(label1, BorderLayout.NORTH);
    con.add(label2, BorderLayout.SOUTH);
    con.add(label3, BorderLayout.CENTER);
    con.add(cbox1, BorderLayout.WEST);
    con.add(cbox2, BorderLayout.EAST);
  }

  public void itemStateChanged(ItemEvent event){
    Object source = event.getSource();
    if(source == roomChoice){
      int roomIndex = roomChoice.getSelectedIndex();
      if(roomIndex == 1){
        int price1 = 600;
      }
      if(roomIndex == 2){
        int price1 = 800;
      }
      if(roomIndex == 3){
        int price1 = 1000;
      }
    }
    if(source == activityChoice){
      int activityIndex = activityChoice.getSelectedIndex();
      if(activityIndex == 1){
        int price2 = 60;
      }
      if(activityIndex == 2){
        int price2 = 40;
      }
      if(activityIndex == 3){
        int price2 = 50;
      }
    }
  }

  public static void main(String[] args){
    CottageRental2 object = new CottageRental2(); 
    object.createGUI();
    object.setSize(675, 320);
  }
}

【问题讨论】:

  • 你不应该使用 == 来测试对象相等性,使用 equals。虽然源永远不会等于 roomChoice,因为它不是 JComboBox,而是替代品,一个字符串数组。您应该使用 cbox1 或 cbox2 对象。
  • 感谢您提供的信息,您也帮助了我!

标签: java jcombobox itemlistener


【解决方案1】:

您在 String[] (roomChoice) 上调用 getSelectedIndex(),但数组没有该名称的方法。所以,它告诉你它找不到那个方法。

我相信你的意思是 cbox1.getSelectedIndex()。这同样适用于activityChoicecbox2

【讨论】:

  • 感谢您的帮助,我希望这可能对其他人有所帮助
猜你喜欢
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 2023-04-06
  • 1970-01-01
相关资源
最近更新 更多