我的组合框有问题。 由于某种原因,我的条目未显示在下拉列表中。我尝试了两种方法,但都无法正常工作。组合框出现在框架中,但为空。我看不出问题出在哪里,编译也没有返回任何错误。
import javax.swing.*;
import java.awt.*;
public class TemplatesList extends JComboBox<String>
{
public JComboBox faceCombo;
private String[] lista = {"change", "handover"};
private JComboBox wyobr = new JComboBox();
public TemplatesList()
{
/*
faceCombo = new JComboBox();
faceCombo.setEditable(false);
faceCombo.addItem("change request");
faceCombo.addItem("emergancy change request");
faceCombo.addItem("problem request");
faceCombo.addItem("problem handover");
faceCombo.addItem("Major Incident handover");
*/
for (int i=0; i < lista.length; i++)
{
wyobr.addItem(lista[i]);
}
}
}`enter code here`
主班
import javax.swing.*;
import java.awt.*;
import javax.swing.JList;
public class ChoosingPage
{
static JFrame frame;
JLabel choose;
public ChoosingPage ()
{
frame = new JFrame ("Primark's templates");
frame.setSize(400,400);
frame.setLocation(50,50);
// frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().setBackground(Color.WHITE);
TemplatesList lista = new TemplatesList();
frame.add(lista, BorderLayout.SOUTH);
frame.setVisible(true);
}
public static void main (String [] args)
{
new ChoosingPage();
}
}
答案 0 :(得分:2)
问题是您不需要从已经拥有的JComboBox中创建另一个JComboBox。您正在扩展一个JComboBox,这意味着您可以访问其所有方法。
例如:
import javax.swing.*;
import java.awt.*;
public class TemplatesList extends JComboBox<String>
{
private String[] lista = {"change", "handover"};
public TemplatesList()
{
super(); //call JComboBox's constructor. This is standard practice
for (int i=0; i < lista.length; i++)
{
this.addItem(lista[i]);
}
}
}
我向您的构造函数添加了super()
。但是,这不是解决您的问题的方法。但是,我们也不想运行JComboBox的构造函数。