pessoal boa tarde.
eu tenho uma classe com construtor com throw.
o meu problema é pq estou usando a ide netbeans, e como arrastei este componente pra um jframe, ele n me deixa criar este objeto.
da erro.
ja tentei adicionar inclusive clausula try catch no código personalizado.
alguma dica?
publicclassJImagePanelextendsJPanel{BufferedImagebackground=null;/** Creates a new panel with the given background image. * @param img The background image. */publicJImagePanel(BufferedImageimg){if(img==null){thrownewNullPointerException("Buffered image cannot be null!");}this.background=img;}/** Creates a new panel with the given background image. * @param img The background image. * @throws IOException, if the image file is not found. */publicJImagePanel(FileimgSrc)throwsIOException{this(ImageIO.read(imgSrc));}/** Creates a new panel with the given background image. * @param img The background image. * @throws IOException, if the image file is not found. */publicJImagePanel(StringfileName)throwsIOException{this(newFile(fileName));}publicJImagePanel(){}@OverrideprotectedvoidpaintComponent(Graphicsg){super.paintComponent(g);Graphics2Dg2d=(Graphics2D)g.create();g2d.drawImage(background,0,0,this.getWidth(),this.getHeight(),null);g2d.dispose();}
A
alissonvla
cara,
seu construtor da sua classe JImagePanel recebe como parametro um objeto do tipoBufferedImage e vc ta passando uma String, ta ai o motivo do erro.
t+
renanreismartins
amigo veja se vc está tratanto a exception dentro de FrmLogin
alissonvla ele tem sobrecarga de construtores…
abrassss
T
TJapaFine
pessoal existe 3 construtores…um string, um file e um bufferedImage.
caso ele passe a string, ele chama o construturo file e o bufferImage
se ele passar um file tem-se a chamada do construtor BufferedImage.
ai onde esta o problema. ao criar um objeto do tipo JImagePanel (independente do construtor) ele precisa de uma clausula try catch.
eu quero excluir esta necessidade.
Entenderam?
abrss
ViniGodoy
O problema está no “throws IOException”. Como é uma exceção verificada, vc deve captura-la.
Há duas opções:
a) Capture dentro do método e lance-a como uma RuntimeException.
b) Coloque a carga do JImagePanel num try…catch.
T
TJapaFine
Obrigado pessoal pela ajuda.
Consegui resolver usando a dica de vinniGodoy.
abrss