[英]How do I declare a JComboBox?
I was creating a program to work with a drop-down list, but I got stuck at the line of declaration of JComboBox box, I get these error messages: Multiple markers at this line - The constructor JComboBox(String[]) is undefined - Line breakpoint:JComboBox [line: 25] - JComboBox() No matter how I try to define JComboBox, I get some sort of error. 我正在创建一个用于处理下拉列表的程序,但遇到了JComboBox框的声明行,得到了以下错误消息:这行有多个标记-构造函数JComboBox(String [])未定义-行断点:JComboBox [行:25]-JComboBox()无论我如何尝试定义JComboBox,我都会遇到某种错误。 Please help me with it. 请帮我。
Here's the code of the public class: 这是公共类的代码:
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
public class JComboBox extends JFrame {
private JComboBox box;
private JLabel picture;
private static String[] filename = { "p.png", "i.png" };
private Icon pics[] = { new ImageIcon(getClass().getResource(filename[0])),
new ImageIcon(getClass().getResource(filename[1])) };
public JComboBox() {
super("This is the title");
setLayout(new FlowLayout());
JComboBox box = new JComboBox(filename);
box.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
picture.setIcon(pics[box.getSelectedIndex()]);
}
}
});
add(box);
picture = new JLabel(pics[0]);
add(picture);
}
}
And here's the code of the main class: 这是主类的代码:
import javax.swing.*;
public class JComboBox1 extends JFrame {
public static void main(String[] args) {
JComboBox Box = new JComboBox();
Box.setVisible(true);
Box.setSize(400,400);
Box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Thank you. 谢谢。
Your problem, as @Andy mentioned is that you have a collision in the name of your class and the swing component. 正如@Andy提到的那样,您的问题是您的类和swing组件的名称发生冲突。 If you absolutely must name your class JComboBox you'll have to reference the swing component by the fully-qualified name, as such 如果绝对必须为类JComboBox命名,则必须使用完全限定的名称来引用swing组件,例如
public class JComboBox extends JFrame {
private javax.swing.JComboBox box;
If you hover over your private instance with an IDE you should see the fully-qualified name matching the package in which you've created your JComboBox class. 如果使用IDE将鼠标悬停在私有实例上,则应该看到与创建JComboBox类的包相匹配的标准名称。 Save yourself some pain and rename your class. 减轻您的痛苦,并重新命名您的班级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.