1)这种说法是空的,可能你想Integer/Object/String
值从当前选择Item
Integer/Object/String myWhatever = ageEntries.getSelectedItem();
2)更好的方式是使用ItemListener为JComboBox,而比ActionListener,通知ItemListener
发射事件SELECTED/DESELECTED
,总是两次
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxListeners {
private JFrame f;
private JComboBox flyFromCombo;
private JComboBox flyToCombo;
private JLabel tripLabel = new JLabel();
private Object[] itemsFrom;
private Object[] itemsTo;
public ComboBoxListeners() {
itemsFrom = new Object[]{"-", "First - From", "Second - From", "Third - From"};
itemsTo = new Object[]{"-", "First - To", "Second - To", "Third - To"};
//flyFromCombo.setPrototypeDisplayValue("################################################");
flyFromCombo = new JComboBox(itemsFrom);
flyFromCombo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() == ItemEvent.SELECTED)) {
String str = flyFromCombo.getSelectedItem().toString();
String str1 = flyToCombo.getSelectedItem().toString();
setLabelText(str, str1);
}
}
});
flyToCombo = new JComboBox(itemsTo);
flyToCombo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() == ItemEvent.SELECTED)) {
String str = flyFromCombo.getSelectedItem().toString();
String str1 = flyToCombo.getSelectedItem().toString();
setLabelText(str, str1);
}
}
});
tripLabel.setPreferredSize(new Dimension(400, 30));
f = new JFrame("ComboBox ItemListeners");
f.setLayout(new GridLayout(0, 1, 15, 15));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(flyFromCombo);
f.add(flyToCombo);
f.add(tripLabel);
f.setLocation(150, 150);
f.pack();
f.setVisible(true);
}
private void setLabelText(String str1, String str2) {
String textForLabel = "";
String helpStringFirst = str1.trim();
if (helpStringFirst != null && helpStringFirst.length() > 0) {
if (!helpStringFirst.equals("-")) {
textForLabel = "Flight No57. from : " + helpStringFirst;
} else {
textForLabel = "Flight from Un-Know : ";
}
}
String helpStringSecond = str2.trim();
if (helpStringSecond != null && helpStringSecond.length() > 0) {
if (!helpStringSecond.equals("-")) {
textForLabel = textForLabel + " --> to : " + helpStringSecond;
} else {
textForLabel += " to : Un-Know ";
}
}
final String pushTextForLabel = textForLabel;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
tripLabel.setText(pushTextForLabel);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ComboBoxListeners comboBoxListeners = new ComboBoxListeners();
}
});
}
}
编辑
我还没有(和不想过多)JDK7,
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class ComboBoxListeners {
private JFrame f;
private JComboBox flyFromCombo;
private JLabel tripLabel = new JLabel();
public ComboBoxListeners() {
ArrayList<Integer> ageList = new ArrayList<Integer>();
for (int i = 1; i <= 100; ++i) {
ageList.add(i);
}
DefaultComboBoxModel modelAge = new DefaultComboBoxModel();
for (Integer i : ageList) {
modelAge.addElement(i);
}
flyFromCombo = new JComboBox(modelAge);
flyFromCombo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() == ItemEvent.SELECTED)) {
String str = flyFromCombo.getSelectedItem().toString();
tripLabel.setText("Selected Age From JComboBox is : " + str);
}
}
});
tripLabel.setPreferredSize(new Dimension(400, 30));
f = new JFrame("ComboBox ItemListeners");
f.setLayout(new GridLayout(0, 1, 15, 15));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(flyFromCombo);
f.add(tripLabel);
f.setLocation(150, 150);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ComboBoxListeners comboBoxListeners = new ComboBoxListeners();
}
});
}
}
我仍然收到一个错误...我编辑了它,但仍然无法工作。问题可能在于放入JComboBox的内容? – Maydayfluffy 2012-02-25 02:30:45
从哪一个你得到一个例外,有天空上的星星选项,看我的编辑 – mKorbel 2012-02-25 02:36:21
错误是从行ageEntries.getSelectedItem();该行旨在采取用户选择的内容并将其放置在其他地方(如文本文件)。我编辑的代码是另一个尝试这样做,但也失败了。 – Maydayfluffy 2012-02-25 02:42:28