[英]ImageIO exception cannot read my input file. For the buffered Image when reading file
public FinalProject() throws IOException
{
String fontType = JOptionPane.showInputDialog("What kind of font would you like? ");
String backgroundColor = JOptionPane.showInputDialog("Would you like your background color to be blue, green or pink?");
if(backgroundColor.equals("blue"))
{
setContentPane(new JLabel(new ImageIcon("calculator_back_and_keypad.png")));
}
else if(backgroundColor.equals("pink"))
{
setContentPane(new JLabel(new ImageIcon("calculator_back_and_keypad2.png")));
}
else if(backgroundColor.equals("green"))
{
setContentPane(new JLabel(new ImageIcon("calculator_back_and_keypad3.png")));
}
else
{
JOptionPane.showMessageDialog(c, "Sorry color not recognized so color was set to the defalut (blue)");
setContentPane(new JLabel(new ImageIcon("calculator_back_and_keypad.png")));
}
Font font = new Font(fontType, Font.BOLD, 30);
txt = new JTextField(13); //create a text field
Color color = new Color(255,0,0);
txt.setForeground(color);
txt.setFont(font);
c = getContentPane();
setLayout(new FlowLayout());
c.add(txt);
buttons = new HashMap <String, JButton>();
String[] names = {"1","2","3","4","5","6","7","8","9","0","+","-","x","/","sin","cos","tan","arcsin","arccos","arctan","log","abs","sqrt","exp","M","M Recall","C","="};
for (String d: names)
{
BufferedImage pic1 = ImageIO.read(new File("button1.jpg"));
JButton i = new JButton(new ImageIcon(pic1));
i.setPreferredSize(new Dimension(75,75));
c.add(i);
i.addActionListener(this);
buttons.put(d, i);
}
?the ImageIO is saying that it is unable to read the file and the file is inside the folder already. ?ImageIO表示无法读取文件,并且该文件已经在文件夹中。
setSize(400,650); setSize(400,650); setVisible(true); setVisible(true); setResizable(false); setResizable(false); txt.setEditable(false); txt.setEditable(false);
From the comment I think you have to use ImageIO.#read(URL)
method. 根据评论,我认为您必须使用ImageIO.#read(URL)
方法。 Also Why are you doing same thing in the for loop? 另外,为什么在for循环中做同样的事情?
URL url = this.getClass().getResource("/path/to/resource/button1.jpg");
BufferedImage pic1 = ImageIO.read(url);
JButton i = new JButton(new ImageIcon(pic1));
i.setPreferredSize(new Dimension(75,75));
c.add(i);
i.addActionListener(this);
this should be done outside the for loop and in the for loop 这应该在for循环之外和for循环中完成
String[] names = {"1","2","3","4","5","6","7","8","9","0","+","-","x","/","sin","cos","tan","arcsin","arccos","arctan","log","abs","sqrt","exp","M","M Recall","C","="};
for (String d : names)
{
buttons.put(d, i);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.