Java访问用于HashMaping的十六进制文本文件

Java访问用于HashMaping的十六进制文本文件,java,iterator,hashmap,hex,bufferedreader,Java,Iterator,Hashmap,Hex,Bufferedreader,我正在做一个GUI,我必须做按钮和背景的改变。该程序使用的是Java集合框架,我使用的是Hashmap。我真的很难理解,试图做的是访问一个txt文件,然后读取对并将它们存储在hashmap中。txt文件在单独的行中包含pairs color-十六进制值,这将按十六进制值的递增顺序对pairs进行排序,并使用迭代器将排序后的pairs显示到控制台。我尝试了BufferReader,但出现了错误,但我猜这不是用于地图的最佳方式 //输入文件.txt 红色FF0000 蓝色000084 绿色00FF0

我正在做一个GUI,我必须做按钮和背景的改变。该程序使用的是Java集合框架,我使用的是Hashmap。我真的很难理解,试图做的是访问一个txt文件,然后读取对并将它们存储在hashmap中。txt文件在单独的行中包含pairs color-十六进制值,这将按十六进制值的递增顺序对pairs进行排序,并使用迭代器将排序后的pairs显示到控制台。我尝试了BufferReader,但出现了错误,但我猜这不是用于地图的最佳方式

//输入文件.txt

红色FF0000

蓝色000084

绿色00FF00

黄色FF00

橙色FF8C00

粉红色FFC0CB

灰色D3

棕色964B00

紫色800080

黑色百万

深绿色013220

深红色8B0000

深蓝色00008B

深橙色D97700

深灰色363737

深紫色471E8A

深黄色7f7f00

淡黄色FFCC

浅蓝色C0D9D9

浅紫色D8BFD8

下面是我已经开始并最终开始为GUI工作的代码

public class FP extends JFrame implements ActionListener {

BufferedReader reader = new BufferedReader(new FileReader(new File("Input File.txt")));
private Map<String, String> buttonColors;

// Constructor
public FP() {

    super("ColorMap");
    buttonColors = new HashMap<String, String>();

    //test button
    buttonColors.put("Red", "FF0000");

    setSize(400, 400);
    setLayout(new FlowLayout());
    ButtonGroup buttonGroup = new ButtonGroup();

    for (Map.Entry<String, String> coloringButtons : buttonColors
            .entrySet()) {

        JRadioButton button = new JRadioButton(coloringButtons.getKey());

        button.setActionCommand(coloringButtons.getValue());
        button.addActionListener(this);

        // Add this new color-button to the button group
        buttonGroup.add(button);
        add(button);
    }
}

@Override
public void actionPerformed(ActionEvent e) {

    String color = e.getActionCommand();
    getContentPane().setBackground(new Color(Integer.parseInt(color, 16)));

}

public static void main(String[] args) {

    FP obj = new FP();
    obj.setVisible(true);
}
}
公共类FP扩展JFrame实现ActionListener{
BufferedReader=new BufferedReader(新文件阅读器(新文件(“Input File.txt”));
私人地图按钮颜色;
//建造师
公共FP(){
超级(“彩色地图”);
buttonColors=新HashMap();
//测试按钮
按钮颜色。放置(“红色”、“FF0000”);
设置大小(400400);
setLayout(新的FlowLayout());
ButtonGroup ButtonGroup=新建ButtonGroup();
用于(地图输入颜色按钮:按钮颜色
.entrySet()){
JRadioButton=newjradiobutton(coloringButtons.getKey());
setActionCommand(coloringButtons.getValue());
addActionListener(这个);
//将此新颜色按钮添加到按钮组
按钮组。添加(按钮);
添加(按钮);
}
}
@凌驾
已执行的公共无效操作(操作事件e){
字符串颜色=e.getActionCommand();
getContentPane().setBackground(新颜色(Integer.parseInt(Color,16));
}
公共静态void main(字符串[]args){
FP obj=新的FP();
对象设置可见(真);
}
}

这是修改后的代码,用于读取文件并填充哈希映射,在本例中,我使用树映射按顺序对十六进制字段进行排序,看看这是否适用于您

公共类FP扩展JFrame实现ActionListener{

private TreeMap<String, String> buttonColors;

// Constructor
public FP() {

    super("ColorMap");
    buttonColors = new TreeMap<String, String>();

    BufferedReader br = new BufferedReader(new FileReader("InputFile.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();

      //* to have the colors sorted by the hex value
      buttonColors.put(colorValue, colorName);
    }
    br.close();

    //test button
    //buttonColors.put("Red", "FF0000");

    setSize(400, 400);
    setLayout(new FlowLayout());
    ButtonGroup buttonGroup = new ButtonGroup();

    for (Map.Entry<String, String> coloringButtons : buttonColors
            .entrySet()) {

        JRadioButton button = new JRadioButton(coloringButtons.getValue());

        button.setActionCommand(coloringButtons.getKey());
        button.addActionListener(this);

        // Add this new color-button to the button group
        buttonGroup.add(button);
        add(button);
    }
}
私有树映射按钮颜色;
//建造师
公共FP(){
超级(“彩色地图”);
buttonColors=新树映射();
BufferedReader br=新的BufferedReader(新文件读取器(“InputFile.txt”);
while(true)
{
字符串str=br.readLine();
如果(str==null)中断;
如果(str.length()==0)继续;
字符串[]st=str.split(“”);
字符串colorName=st[0]。trim();
字符串colorValue=st[1]。trim();
//*按十六进制值对颜色进行排序
buttonColors.put(colorValue、colorName);
}
br.close();
//测试按钮
//按钮颜色。放置(“红色”、“FF0000”);
设置大小(400400);
setLayout(新的FlowLayout());
ButtonGroup ButtonGroup=新建ButtonGroup();
用于(地图输入颜色按钮:按钮颜色
.entrySet()){
JRadioButton=newjradiobutton(coloringButtons.getValue());
setActionCommand(coloringButtons.getKey());
addActionListener(这个);
//将此新颜色按钮添加到按钮组
按钮组。添加(按钮);
添加(按钮);
}
}

请向我们展示失败的代码。目前这看起来像是对代码的请求,而不是问题。很抱歉,我刚刚将
BufferedReader
部分更改为
static String fileName=“Input File.txt”因为我认为这会对Hashmap@Maarten Bodewe有所帮助,我希望这是Hashmap访问文件的一个良好开端。你需要向我们展示你用
BufferedReader尝试了什么,以及它是如何失败的。好的,在顶部,我更换了缓冲读取器,这是我第一次启动时出现的问题。
BufferedReaderreader=new BufferedReader(new FileReader(new File(“Input File.txt”));
this
BufferedReader br=new BufferedReader(new FileInputStream(“InputFile.txt”);
仍然给我一个问题,说“构造函数BufferedReader(FileInputStream)未定义”?这很奇怪。它说现在找不到文件。顺便说一下,我也在使用eclipse。根据你给我的,但我不得不修改它,以使用文件的完整路径,看看这是否解决了问题。你如何在?Folder/Input file.txt?假设你的文件名是inputfile.txt,它位于test/source/按钮中,然后在linux/unix上,它将是“/test/source/buttons/inputfile.txt”,如果它在windows上,则假定文件夹位于c:驱动器上,则它将是“c:\\test\\souce\\buttons\\inputfile.txt”