Java 无法在表单中显示自己的元素
我想以我的形式创建我自己的盒子。我做这个代码。但是组合框no出现((( 这是我自己的组合框的代码((Java 无法在表单中显示自己的元素,java,user-interface,combobox,Java,User Interface,Combobox,我想以我的形式创建我自己的盒子。我做这个代码。但是组合框no出现((( 这是我自己的组合框的代码(( 公共类ComboBoxDemo{ 私人名单国家; 私人JComboxCBox; 公共ComboBoxDemo(JFrame){ countries=createCountryList(); cBox=createComboBox(国家/地区); 帧。添加(cBox); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack
公共类ComboBoxDemo{
私人名单国家;
私人JComboxCBox;
公共ComboBoxDemo(JFrame){
countries=createCountryList();
cBox=createComboBox(国家/地区);
帧。添加(cBox);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
私有JComboBox createComboBox(列出国家){
最终JComboBox组合框=新的JComboBox(countries.toArray());
setRenderer(新的ComboBoxRenderer());
comboBox.addItemListener(新的ItemListener(){
@凌驾
公共无效itemStateChanged(ItemEvent e){
如果(如getStateChange()==ItemEvent.SELECTED){
国家=(国家)组合框。getSelectedItem();
System.out.println(country.getIso());
}
}
});
返回组合框;
}
私有类ComboBoxRenderer扩展了DefaultListCellRenderer{
@凌驾
公共组件GetListCellRenderComponent(JList列表、对象值、int索引、布尔isSelected、,
布尔单元(聚焦){
JLabel=(JLabel)super.getListCellRenderComponent(列表、值、索引、isSelected、cellHasFocus);
国家=(国家)值;
label.setText(country.getName());
退货标签;
}
}
私有列表createCountryList(){
列表=新的ArrayList();
增加(新国家(“阿富汗”、“阿富汗”);
增加(新国家(“奥兰群岛”、“AX”);
增加(新国家(“阿尔巴尼亚”、“AL”);
退货清单;
}
公营国家{
私有字符串名称;
私有字符串iso;
公共国家(字符串名称、字符串iso){
this.name=名称;
这是iso=iso;
}
公共字符串getName(){
返回名称;
}
公共字符串getIso(){
返回iso;
}
}
}
但是我无法在GUI表单中显示我的ComboBox。我需要从表单中调用它,但是,当我运行程序时-没有错误,但表单中没有ComboBox…首先使用null布局不是一个好主意。我想你应该重新设计你的应用程序。检查以下讨论: 您的组合框未显示,因为在null布局中,所有组件都需要设置大小。您只是忘记了使用
cBox的setBounds()
public class GUI {
String RetVal1;
String[][] RetValArr;
private JFrame frame;
private JLabel lblNewLabel;
private JTextField Name_textField;
private JFormattedTextField Phone_formattedTextField;
static Connection conn3 = null;
static int EC1;
static int CurrentEntry;
static String CurrentEntrySTR;
private JTextField CurrentEnty_textField;
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GUI() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 504, 513);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JComboBox From_comboBox = new JComboBox();
From_comboBox.setModel(new DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));
From_comboBox.setEditable(true);
From_comboBox.setMaximumRowCount(2);
From_comboBox.setBounds(274, 194, 186, 23);
frame.getContentPane().add(From_comboBox);
new ComboBoxDemo(frame);
JComboBox Status_comboBox = new JComboBox();
Status_comboBox.setBounds(274, 281, 186, 23);
frame.getContentPane().add(Status_comboBox);
CurrentEnty_textField = new JTextField();
}
}
public class ComboBoxDemo {
private List<Country> countries;
private JComboBox cBox;
public ComboBoxDemo(JFrame frame) {
countries = createCountryList();
cBox = createComboBox(countries);
frame.add(cBox);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JComboBox createComboBox(List<Country> countries) {
final JComboBox comboBox = new JComboBox(countries.toArray());
comboBox.setRenderer(new ComboBoxRenderer());
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
Country country = (Country) comboBox.getSelectedItem();
System.out.println(country.getIso());
}
}
});
return comboBox;
}
private class ComboBoxRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
Country country = (Country) value;
label.setText(country.getName());
return label;
}
}
private List<Country> createCountryList() {
List<Country> list = new ArrayList<>();
list.add(new Country("Afghanistan", "AF"));
list.add(new Country("Åland Islands", "AX"));
list.add(new Country("Albania", "AL"));
return list;
}
public class Country {
private String name;
private String iso;
public Country(String name, String iso) {
this.name = name;
this.iso = iso;
}
public String getName() {
return name;
}
public String getIso() {
return iso;
}
}
}