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);
//弹出替换成功对话框
JOptionPane.showConfirmDialog(FindAndReplaceFrame.this, "替换成功","消息",JOptionPane.CLOSED_OPTION);
//点击确定按钮推出
} catch (Exception ee) {
JOptionPane.showConfirmDialog(FindAndReplaceFrame.this, "替换失败");
ee.printStackTrace();
}
}
}
});望采纳,谢谢。
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);
//弹出替换成功对话框
JOptionPane.showConfirmDialog(FindAndReplaceFrame.this, "替换成功","消息",JOptionPane.CLOSED_OPTION);
//点击确定按钮推出
} catch (Exception ee) {
JOptionPane.showConfirmDialog(FindAndReplaceFrame.this, "替换失败");
ee.printStackTrace();
}
}
}
});望采纳,谢谢。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询