我希望第一个组合框中选择的城市不出现在第二个中
I want the city selected in the first combo box not to appear in the second
如果我选择一个城市 FROM: JComboBox
那么它不应该出现在 TO: 组合框中。如何编写此动作侦听器的 ActionListener
?
Screenshot
代码:
public class CityFromTo {
public static void main(String[] args) {
String[] cFrom = {"Choose", "Istanbul", "New York", "London", "Milano", "Tokyo", "Paris", "Moscow", "Elâzığ"};
String[] cTo = {"Choose", "Istanbul", "New York", "London", "Milano", "Tokyo", "Paris", "Moscow", "Elâzığ"};
JLabel from = new JLabel("FROM : ");
JLabel to = new JLabel(" TO : ");
JComboBox comboFrom = new JComboBox(cFrom);
JComboBox comboTo = new JComboBox(cTo);
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.add(from);
frame.add(comboFrom);
frame.add(to);
frame.add(comboTo);
frame.setSize(400, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
我重新编写了您的代码以摆脱静态。您只需要一个城市列表作为您的往返城市 JComboBoxes
。
您使用 ActionListener
来监听来自 JComboBox
的变化,并从 JComboBox
中移除城市。我们通过删除所有城市并将它们添加回来来删除城市,一次一个。这允许用户改变他对来自城市的想法。
这是完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CityFromTo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new CityFromTo());
}
private JComboBox<String> comboFrom;
private JComboBox<String> comboTo;
private String[] cities = {"Choose", "Istanbul", "New York", "London",
"Milano", "Tokyo", "Paris", "Moscow", "Elâzığ"};
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panel.setPreferredSize(new Dimension(400, 350));
JLabel from = new JLabel("FROM : ");
panel.add(from);
comboFrom = new JComboBox<>(cities);
comboFrom.addActionListener(new FromListener());
panel.add(comboFrom);
JLabel to = new JLabel(" TO : ");
panel.add(to);
comboTo = new JComboBox<>(cities);
panel.add(comboTo);
return panel;
}
public class FromListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
String city = (String) comboFrom.getSelectedItem();
comboTo.removeAllItems();
for (String s : cities) {
if (!s.equals(city)) {
comboTo.addItem(s);
}
}
}
}
}
如果我选择一个城市 FROM: JComboBox
那么它不应该出现在 TO: 组合框中。如何编写此动作侦听器的 ActionListener
?
Screenshot
代码:
public class CityFromTo {
public static void main(String[] args) {
String[] cFrom = {"Choose", "Istanbul", "New York", "London", "Milano", "Tokyo", "Paris", "Moscow", "Elâzığ"};
String[] cTo = {"Choose", "Istanbul", "New York", "London", "Milano", "Tokyo", "Paris", "Moscow", "Elâzığ"};
JLabel from = new JLabel("FROM : ");
JLabel to = new JLabel(" TO : ");
JComboBox comboFrom = new JComboBox(cFrom);
JComboBox comboTo = new JComboBox(cTo);
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.add(from);
frame.add(comboFrom);
frame.add(to);
frame.add(comboTo);
frame.setSize(400, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
我重新编写了您的代码以摆脱静态。您只需要一个城市列表作为您的往返城市 JComboBoxes
。
您使用 ActionListener
来监听来自 JComboBox
的变化,并从 JComboBox
中移除城市。我们通过删除所有城市并将它们添加回来来删除城市,一次一个。这允许用户改变他对来自城市的想法。
这是完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CityFromTo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new CityFromTo());
}
private JComboBox<String> comboFrom;
private JComboBox<String> comboTo;
private String[] cities = {"Choose", "Istanbul", "New York", "London",
"Milano", "Tokyo", "Paris", "Moscow", "Elâzığ"};
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panel.setPreferredSize(new Dimension(400, 350));
JLabel from = new JLabel("FROM : ");
panel.add(from);
comboFrom = new JComboBox<>(cities);
comboFrom.addActionListener(new FromListener());
panel.add(comboFrom);
JLabel to = new JLabel(" TO : ");
panel.add(to);
comboTo = new JComboBox<>(cities);
panel.add(comboTo);
return panel;
}
public class FromListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
String city = (String) comboFrom.getSelectedItem();
comboTo.removeAllItems();
for (String s : cities) {
if (!s.equals(city)) {
comboTo.addItem(s);
}
}
}
}
}