java怎么给图片按钮添加监听

java怎么给图片按钮添加监听,第1张

JButton jbt = new JButton("选择文件/文件夹")

panel.add(jtf,BorderLayout.CENTER)

panel.add(jbt,BorderLayout.SOUTH)

jbt.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

JFileChooser jfc = new JFileChooser()

jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)

int returnVal = jfc.showOpenDialog(jfc)

if(returnVal == JFileChooser.APPROVE_OPTION){

filePath = jfc.getSelectedFile().getAbsolutePath()

jtf.setText(filePath)

File file = new File(filePath)

try {

FindAndReplaceService.replace(file)

//d出替换成功对话框

JOptionPane.showConfirmDialog(FindAndReplaceFrame.this, "替换成功","消息",JOptionPane.CLOSED_OPTION)

//点击确定按钮推出

} catch (Exception ee) {

JOptionPane.showConfirmDialog(FindAndReplaceFrame.this, "替换失败")

ee.printStackTrace()

}

}

}

})望采纳,谢谢。

楼主这样写试试

p1_lab2=new JLabel("按钮",new ImageIcon("…….gif"),0)

p1_lab2.addMouseListener(this)//监听

public void mouseClicked(MouseEvent e) {

//判断

if(e.getSource()==this.p1_lab2){

。。。

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11592809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存