gpt4 book ai didi

java - 我有一个 HashMap 如何将它与 GUI 集成;我想输入整数并获取字符串值。

转载 作者:行者123 更新时间:2023-12-02 04:03:44 24 4
gpt4 key购买 nike

我有五十个州的 HashMap;我想使用一个 GUI,您可以从 HashMap 输入整数,结果是字符串。我知道“JOptionPane”是什么,但我不知道如何将它与 GUI 集成。

import java.util.HashMap;

public class FIftyStates {

public static void main(String[] args) {

HashMap<Integer, String> FiftyStates = new HashMap <Integer, String>();




FiftyStates.put(1,"Delaware");
FiftyStates.put(2,"Pennsylvania");
FiftyStates.put(3,"New Jersey");
FiftyStates.put(4,"Georgia");
FiftyStates.put(5,"Connecticut");
FiftyStates.put(6,"Massachusetts" );
FiftyStates.put(7,"Maryland");
FiftyStates.put(8,"South Carolina");
FiftyStates.put(9,"New Hampshire");
FiftyStates.put(10,"Virginia");
FiftyStates.put(11,"New York");
FiftyStates.put(12,"North Carolina");
FiftyStates.put(13,"Rhode Island");
FiftyStates.put(14,"Vermont");
FiftyStates.put(15,"Kentucky");
FiftyStates.put(16,"Tennessee");
FiftyStates.put(17,"Ohio");
FiftyStates.put(18,"Louisiana");
FiftyStates.put(19,"Indiana");
FiftyStates.put(20,"Mississippi");
FiftyStates.put(21,"Illinois");
FiftyStates.put(22,"Alabama");
FiftyStates.put(23,"Maine");
FiftyStates.put(24,"Missouri");
FiftyStates.put(25,"Arkansas");
FiftyStates.put(26,"Michagan");
FiftyStates.put(27,"Florida");
FiftyStates.put(28,"Texas");
FiftyStates.put(29,"Iowa");
FiftyStates.put(30,"Wisconsin");
FiftyStates.put(31,"California");
FiftyStates.put(32,"Minnesota");
FiftyStates.put(33,"Oregon");
FiftyStates.put(34,"Kansas");
FiftyStates.put(35,"West Virginia");
FiftyStates.put(36,"Nevada");
FiftyStates.put(37,"Nebraska");
FiftyStates.put(38,"Colorado");
FiftyStates.put(39,"North Dakota");
FiftyStates.put(40,"South Dakota");
FiftyStates.put(41,"Montana");
FiftyStates.put(42,"Washington");
FiftyStates.put(43,"Idaho");
FiftyStates.put(44,"Wyoming");
FiftyStates.put(45,"Utah");
FiftyStates.put(46,"Oklahoma");
FiftyStates.put(47,"New Mexico");
FiftyStates.put(48,"Arizona");
FiftyStates.put(49,"Alaska");
FiftyStates.put(50,"Hawaii");
}

}

最佳答案

你可以...

做一些类似的事情

int selected = -1;
do {
String value = JOptionPane.showInputDialog(null, "Pick a state: ", "Pick a state", JOptionPane.QUESTION_MESSAGE);
try {
selected = Integer.parseInt(value);
} catch (NumberFormatException exp) {
selected = -1;
}
} while (selected == -1);

JOptionPane.showMessageDialog(null, "You selected " + FiftyStates.get(selected));

但坦率地说,这是一种非常糟糕的用户体验,相反......

你可以...

做一些更像......的事情

Object value = JOptionPane.showInputDialog(null, "Help", "Help", JOptionPane.QUESTION_MESSAGE, null, FiftyStates.values().toArray(new String[FiftyStates.size()]), null);
JOptionPane.showMessageDialog(null, "You selected " + value);

这将提供更好的用户体验

How can I have it print the "integer" as well?

Set<Map.Entry<Integer, String>> entrySet = FiftyStates.entrySet();
DefaultComboBoxModel model = new DefaultComboBoxModel();
for (Map.Entry<Integer, String> entry : entrySet) {
model.addElement(entry);
}
JComboBox cb = new JComboBox(model);
cb.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof Map.Entry) {
value = ((Map.Entry)value).getValue();
}
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
});

JOptionPane.showMessageDialog(null, cb, "Select State", JOptionPane.QUESTION_MESSAGE);
Object selectedItem = cb.getSelectedItem();
if (selectedItem != null && selectedItem instanceof Map.Entry) {
Map.Entry<Integer, String> entry = (Map.Entry<Integer, String>) selectedItem;
JOptionPane.showMessageDialog(null, "You selected " + entry.getKey() + "/" + entry.getValue());
}

关于java - 我有一个 HashMap 如何将它与 GUI 集成;我想输入整数并获取字符串值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34601003/

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