gpt4 book ai didi

java - GUI 中国家和城市的动态列表

转载 作者:行者123 更新时间:2023-12-02 12:58:54 25 4
gpt4 key购买 nike

我想为国家和城市创建两个下拉列表。因此,如果用户从下拉列表中选择一个国家/地区,则其他下拉列表应自动刷新以显示其相应的城市。

 public void fillCombo(){
String sql = " select * from sallytimes.table_name " ;
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery(sql);

while (rs.next()){
jComboBox_Country.addItem(rs.getString("_id"));
jComboBox_City.addItem(rs.getString("city"));
}
} catch (SQLException ex) {
Logger.getLogger(Location.class.getName()).log(Level.SEVERE, null, ex);
}
}

我该如何取得进展?

最佳答案

您需要将 ActionListener 添加到第一个组合框,以便在选择项目时执行操作。在 ActionListener 中,您想要用新数据替换第二个组合框的模型。

类似于:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxTwo extends JPanel implements ActionListener
{
private JComboBox<String> mainComboBox;
private JComboBox<String> subComboBox;
private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();

public ComboBoxTwo()
{
String[] items = { "Select Item", "Color", "Shape", "Fruit" };
mainComboBox = new JComboBox<String>( items );
mainComboBox.addActionListener( this );

// prevent action events from being fired when the up/down arrow keys are used
mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
add( mainComboBox );

// Create sub combo box with multiple models

subComboBox = new JComboBox<String>();
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
add( subComboBox );

JButton arrow = SwingUtils.getDescendantOfType(JButton.class, subComboBox, "Text", "");
Dimension d = arrow.getPreferredSize();
System.out.println(arrow.getClass());
System.out.println(d);
d.width = 35;
arrow.setPreferredSize(d);

String[] subItems1 = { "Select Color", "Red", "Blue", "Green" };
subItems.put(items[1], subItems1);

String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" };
subItems.put(items[2], subItems2);

String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" };
subItems.put(items[3], subItems3);
}

public void actionPerformed(ActionEvent e)
{
String item = (String)mainComboBox.getSelectedItem();
Object o = subItems.get( item );

if (o == null)
{
subComboBox.setModel( new DefaultComboBoxModel() );
}
else
{
subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );
}
}

private static void createAndShowUI()
{
try
{
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ComboBoxTwo() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

因此,您需要修改 ActionListener 中的代码,以根据所选国家/地区从数据库中获取城市。

关于java - GUI 中国家和城市的动态列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44348332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com