java - 我如何在事件中获取变量? [英] java - how do i acess varibles in a event?
问题描述
这是我的代码
this is my code
final URL url = new URL("http://minecraft.aggenkeech.com/face.php?u=z3nox");
image1 = new ImageIcon(ImageIO.read(url));
FaceImage = new JLabel(image1);
FaceImage.setHorizontalAlignment(SwingConstants.LEFT);
FaceImage.setVerticalAlignment(SwingConstants.TOP);
FaceImage.setBounds(550, 11, 65, 60);
contentPane.add(FaceImage);
UserNameTextField = new JTextField();
UserNameTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
// search
final URL url = new URL("http://minecraft.aggenkeech.com/face.php?u=hghg32");
image1 = new ImageIcon(ImageIO.read(url));
}
}
});
UserNameTextField.setHorizontalAlignment(SwingConstants.CENTER);
UserNameTextField.setBorder(BorderFactory.createLineBorder(Color.black));
UserNameTextField.addInputMethodListener(new InputMethodListener() {
public void caretPositionChanged(InputMethodEvent arg0) {
}
public void inputMethodTextChanged(InputMethodEvent arg0) {
UserNameTextField.setBackground(Color.red);
}
});
UserNameTextField.setBounds(557, 326, 124, 20);
contentPane.add(UserNameTextField);
UserNameTextField.setColumns(10);
问题在
the problem is at
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
// search
final URL url = new URL("http://minecraft.aggenkeech.com/face.php?u=hghg32");
image1 = new ImageIcon(ImageIO.read(url));
}
}
});
当我按下输入图像时我想要改变,但我在该行上得到2个错误的网址
它要我tey / catch但是我尝试了但它不起作用?有人可以帮帮我吗?如何访问url varible?
I want when I press enter for the image to change but I get 2 errors on that line with the url
it wants me to "tey/catch" but I try that but it dosen't work? can some one please help me? how do I access the url varible?
推荐答案
你需要用try / catch包围相关的行:
You need to surround the lines in question with try/catch:
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode() == KeyEvent.VK_ENTER) { // search
try {
final URL url = new URL("http://minecraft.aggenkeech.com/face.php?u=hghg32");
image1 = new ImageIcon(ImageIO.read(url));
}
catch(IOException e) {
//TODO do something here...
e.printStackTrace();
}
}
}
如上所述,URL不一定是最终的,你没有在任何地方定义image1,但我认为你知道如何处理它。你的catch块应该有更多有用的代码,并做一些事情来优雅地退出逻辑流。
As written, URL does not need to be final, and you haven't defined image1 anywhere but I assume that you know what to do with it. Your catch block should have more useful code in it and do something to gracefully quit the logic stream.
这篇关于java - 我如何在事件中获取变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!