Je travaille sur une TreeMap avec un itérateur qui lit un fichier texte avec 20 valeurs nom et hexadécimales, par exemple RED; FF0000. D'abord dans le principal j'ai fait un Iterator mais je ne suis pas sûr pourquoi il ne lit pas tous les 20 et l'affichage. Essayez également de les trier en valeur croissante lorsque je les affiche sur la console.Tri et affichage d'une TreeMap en augmentant la valeur Problèmes
Console Affichage:
`00008B=Dark Blue
013220=Dark Green
37FDFC=Blue
7f7f00=Dark Yellow
8B0000=Dark Red
B26200=Dark Orange
D3D3D3=Grey
FF0000=Red
FFC0CB=Pink
FFFFCC=Light Yellow`
code:
public class coloring extends JFrame implements ActionListener {
public static TreeMap<String, String> buttonColors;
// Constructor
public FP(TreeMap<String, String> buttonColors) throws IOException {
super("Hexidecimal Button Map");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.buttonColors = buttonColors;
setSize(500, 400);
setLayout(new FlowLayout());
ButtonGroup buttonGroup = new ButtonGroup();
for (Map.Entry<String, String> coloringButtons : buttonColors
.entrySet()) {
JRadioButton button = new JRadioButton(coloringButtons.getValue()
+ " " + coloringButtons.getKey());
button.setActionCommand(coloringButtons.getKey());
button.addActionListener(this);
buttonGroup.add(button);
add(button);
}
}
public void actionPerformed(ActionEvent e) {
String color = e.getActionCommand();
getContentPane().setBackground(new Color(Integer.parseInt(color, 16)));
}
public static void main(String[] args) throws IOException {
TreeMap<String, String> buttonColors = new TreeMap<String, String>();
BufferedReader br = new BufferedReader(new FileReader("Input File.txt"));
while (true) {
String str = br.readLine();
if (str == null)
break;
if (str.length() == 0)
continue;
String[] st = str.split("; ");
String colorName = st[0].trim();
String colorValue = st[1].trim();
buttonColors.put(colorValue, colorName);
}
br.close();
Set<Map.Entry<String, String>> buttons = buttonColors.entrySet();
Iterator sortingItr = buttons.iterator();
while (sortingItr.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry) sortingItr.next();
System.out.println(sortingItr.next());
}
FP obj = new FP(buttonColors);
obj.setVisible(true);
obj.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
}
}
ouais son pas de tri en augmentant la valeur hexadécimale lors de l'affichage, l'interface graphique est très bien – FreshCoder
je veux dire affiché dans la console – FreshCoder