我希望在第一个comboBox中选择的城市不出现在第二个comboBox中 [英] I want the city selected in the first comboBox not to appear in the second comboBox
问题描述
在此处截屏如果我选择的城市来自:comboBox,则不应出现在TO:comboBox中.如何为该actionListener的actionListener编写代码?
Screenshot hereIf I choose a city FROM: comboBox then it shouldn't appear in TO: comboBox. How can I code the actionListener of this actionListener?
我的代码在这里:
公共类CityFromTo {
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
.
I reworked your code to get away from static. You only need one list of cities for your from and to JComboBoxes
.
您使用 ActionListener
侦听 JComboBox
中的更改,并将城市从 JComboBox
中删除.我们通过删除所有城市并将其一次又一次添加回去来删除该城市.这使用户可以改变对城市的想法.
You use an ActionListener
to listen for changes in the from JComboBox
, and remove the city from the to JComboBox
. We remove the city by removing all the cities and adding them back, one at a time. This allows the user to change his mind about the from city.
这是完整的可运行代码.
Here's the complete runnable code.
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);
}
}
}
}
}
这篇关于我希望在第一个comboBox中选择的城市不出现在第二个comboBox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!