如何创建我需要附加到个人图像的个人细节?当点击按钮时,它可以选择图片,但我仍然无法弄清楚如何设置这种编码,任何人都可以帮助解决这个问题?netbeans:使用Jbutton在Jlabel中附加图像
JFileChooser chooser=new JFileChooser();
chooser.showOpenDialog(null);
File f=chooser.getSelectedFile();
personal_image.
如何创建我需要附加到个人图像的个人细节?当点击按钮时,它可以选择图片,但我仍然无法弄清楚如何设置这种编码,任何人都可以帮助解决这个问题?netbeans:使用Jbutton在Jlabel中附加图像
JFileChooser chooser=new JFileChooser();
chooser.showOpenDialog(null);
File f=chooser.getSelectedFile();
personal_image.
正如我注意到,你在使用NetBeans IDE,NetBeans的已经提供了便利写的actionPerformed为一个JButton在ActionEvent的情况。
我建议你写在你的JButton的actionPerformed下面的代码与动作事件EVT如下所述: -
private void DesiredButtonActionPerformed(java.awt.event.ActionEvent evt){
// Add your mentioned code here before coding this.
try{
Image image = ImageIO.read(f);
ImageIcon icon = new ImageIcon(image);
// JLabel toBeSet = new JLabel();
// considering that you have a JLabel having name as what I've used here
toBeSet.setIcon(icon);
}
catch(IOException ioe){
System.out.println("Exception occured while setting Image on the Label!");
}
}
1)由于新创建的标签永远不会添加到任何内容,因此写入的代码不会有明显的影响。此外,在GUI显示后向GUI添加组件需要特别注意。 2)总的来说,代码会建议'personal_image.setIcon(new ImageIcon(f));'3)使用合乎逻辑的一致形式缩进代码行和块。缩进旨在使代码的流程更易于遵循! –
@ AndrewThompson - 是SIR,我从那里删除那个JLabel。这只是为了获得参考,我会让它推测它已经存在。 SIR,也已经提到他使用NetBeans IDE。所以,他可能使用了NetBeans自动Swing处理的定义良好的功能。而且,SIR,另外OP的问题是如何使用JButton添加图像到标签----我提到他将如何处理这个逻辑。 –
不,我在哪里做的?请指出,你的'f'我已经改名为'file'。 @JiunJye –
为了更好地帮助越早,张贴[MCVE](http://stackoverflow.com/help/ (最小完整可验证示例)或[SSCCE](http://www.sscce.org/)(简短的,独立的,正确的示例)。 –