我想在我的表格中创建自己的盒子。我做这个代码。但ComboBox没有出现((
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();
}
}
这是我自己的ComboBox的代码((
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;
}
}
}
但我无法在GUI表单中显示我的ComboBox。我需要从我的表单中调用它,但是,当我运行程序时 - 没有错误,但没有表单中的组合框...
答案 0 :(得分:0)
首先使用null布局不是一个好主意。我认为你应该重新设计你的应用程序。请查看此讨论:why-is-it-frowned-upon-to-use-a-null-layout-in-swing
您的组合框未显示,因为在null布局中,所有组件都需要设置大小。您忘了setBounds()
使用cBox
。