无法从 java 中的列表中获取项目
Can not get item from List in java
所以我正在制作一个小程序,它必须获取该项目并说明该人的年龄,例如警报 window。尝试通过列表中的索引获取所选项目时出现错误。我正在审查 Whosebug 中的一些主题,但我发现只从 ArrayList 获取项目,我正在使用 List(我认为存在差异)并从 applets 站点找到 getSelectedIndex(index) 。但我不知道为什么它会给我那一行的错误:
类型 List 中的方法 getSelectedIndex() 不适用于参数 (int)。
我只需要获取选定的索引,这样我就可以获取该项目,当我知道它已被选中时,我将 运行 我的逻辑。抱歉,我在 java 中表现不佳。
这是代码:
package course;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.applet.Applet;
import java.awt.event.*;//vnimavai kak go pishesh
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.List;
import javax.swing.JOptionPane;
public class applet extends Applet implements ActionListener
{
int fontStyle = Font.PLAIN;
TextField name_enter;
TextField pass_enter;
TextField pass_re;
List age;
CheckboxGroup radioGroup;
Checkbox radio1;
Checkbox radio2;
Button send;
Button clear;
public void init()
{
radioGroup = new CheckboxGroup();
radio1 = new Checkbox("male",radioGroup,true);
radio2 = new Checkbox("female",radioGroup,false);
send = new Button("send");
clear = new Button("clear");
name_enter = new TextField("",20);
pass_enter = new TextField("",14);
pass_re = new TextField("",14);
age = new List();
name_enter.setBounds(100, 100, 130, 70);
pass_enter.setBounds(100, 110, 130, 80);
pass_re.setBounds(100, 120, 130, 70);
age.setBounds(100, 130, 130, 70);
radio1.setBounds(100, 140, 130, 70);
radio2.setBounds(100, 150, 130, 70);
send.setBounds(100, 160, 130, 70);
clear.setBounds(105, 160, 130, 70);
add(new Label("Enter name:"));
add(name_enter);
add(new Label("Enter password >=6 char:"));
add(pass_enter);
add(new Label("Reenter password:"));
add(pass_re);
add(new Label("Enter age:"));
add(age);
age.add("-30");
age.add("31-50");
age.add("51+");
add(new Label("You are:"));
add(radio1);
add(radio2);
add(send);
add(clear);
send.addActionListener(this);
clear.addActionListener(this);
}
public void actionPerformed(ActionEvent evnt)
{
if(evnt.getSource()==send)
{
if(age.getSelectedIndex(0))
{
JOptionPane.showMessageDialog(null,"Send button pushed.");
}
else if()
{
}
else if()
{
}
repaint();
}
else if(evnt.getSource()==clear)
{
name_enter.setText(null);
pass_enter.setText(null);
pass_re.setText(null);
repaint();
}
}
}
1) java.awt.List在getSelectedIndex()中没有参数;
请参阅文档以获取完整信息:Reference Documentation
2) 您正在尝试将 return 类型 int
的函数传递给 if( )
块,该块仅适用于布尔值。
所以,这部分应该是这样的:
if (age.getSelectedIndex() == 0) {
JOptionPane.showMessageDialog(null, "Send button pushed.");
}
或者:
if (age.isIndexSelected(0)) {
JOptionPane.showMessageDialog(null, "Send button pushed.");
}
isIndexSelected(int index)
- Determines if the specified item in this scrolling list is selected
.
所以我正在制作一个小程序,它必须获取该项目并说明该人的年龄,例如警报 window。尝试通过列表中的索引获取所选项目时出现错误。我正在审查 Whosebug 中的一些主题,但我发现只从 ArrayList 获取项目,我正在使用 List(我认为存在差异)并从 applets 站点找到 getSelectedIndex(index) 。但我不知道为什么它会给我那一行的错误: 类型 List 中的方法 getSelectedIndex() 不适用于参数 (int)。 我只需要获取选定的索引,这样我就可以获取该项目,当我知道它已被选中时,我将 运行 我的逻辑。抱歉,我在 java 中表现不佳。 这是代码:
package course;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.applet.Applet;
import java.awt.event.*;//vnimavai kak go pishesh
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.List;
import javax.swing.JOptionPane;
public class applet extends Applet implements ActionListener
{
int fontStyle = Font.PLAIN;
TextField name_enter;
TextField pass_enter;
TextField pass_re;
List age;
CheckboxGroup radioGroup;
Checkbox radio1;
Checkbox radio2;
Button send;
Button clear;
public void init()
{
radioGroup = new CheckboxGroup();
radio1 = new Checkbox("male",radioGroup,true);
radio2 = new Checkbox("female",radioGroup,false);
send = new Button("send");
clear = new Button("clear");
name_enter = new TextField("",20);
pass_enter = new TextField("",14);
pass_re = new TextField("",14);
age = new List();
name_enter.setBounds(100, 100, 130, 70);
pass_enter.setBounds(100, 110, 130, 80);
pass_re.setBounds(100, 120, 130, 70);
age.setBounds(100, 130, 130, 70);
radio1.setBounds(100, 140, 130, 70);
radio2.setBounds(100, 150, 130, 70);
send.setBounds(100, 160, 130, 70);
clear.setBounds(105, 160, 130, 70);
add(new Label("Enter name:"));
add(name_enter);
add(new Label("Enter password >=6 char:"));
add(pass_enter);
add(new Label("Reenter password:"));
add(pass_re);
add(new Label("Enter age:"));
add(age);
age.add("-30");
age.add("31-50");
age.add("51+");
add(new Label("You are:"));
add(radio1);
add(radio2);
add(send);
add(clear);
send.addActionListener(this);
clear.addActionListener(this);
}
public void actionPerformed(ActionEvent evnt)
{
if(evnt.getSource()==send)
{
if(age.getSelectedIndex(0))
{
JOptionPane.showMessageDialog(null,"Send button pushed.");
}
else if()
{
}
else if()
{
}
repaint();
}
else if(evnt.getSource()==clear)
{
name_enter.setText(null);
pass_enter.setText(null);
pass_re.setText(null);
repaint();
}
}
}
1) java.awt.List在getSelectedIndex()中没有参数;
请参阅文档以获取完整信息:Reference Documentation
2) 您正在尝试将 return 类型 int
的函数传递给 if( )
块,该块仅适用于布尔值。
所以,这部分应该是这样的:
if (age.getSelectedIndex() == 0) {
JOptionPane.showMessageDialog(null, "Send button pushed.");
}
或者:
if (age.isIndexSelected(0)) {
JOptionPane.showMessageDialog(null, "Send button pushed.");
}
isIndexSelected(int index) - Determines if the specified item in this scrolling list is selected
.