我有五十个国家的HashMap;我想使用一个GUI,你从HashMap输入整数,结果是String.I知道" 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");
}
}
答案 0 :(得分:2)
做类似的事情......
const fs = require('fs');
var contents = fs.readFileSync('inject.txt').toString();
但坦率地说,这是一个非常可怕的用户体验,而不是......
做更像......的事情。
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));
可以提供更好的用户体验
我怎样才能打印&#34;整数&#34;还有吗?
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);