package GUI;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
//拷贝文件演示
public class CopyFileDemo extends JFrame{
private int value = 0;
private static final int PROGRESS_MIN_VALUE = 0;
private static final int PROGRESS_MAX_VALUE = 100;
private static final int PROGRESS_HEIGHT = 20;
private Timer timer;
JFileChooser fileChooser; //文件选择器
JTextField jtfTargetFile; //目标文件路径
JTextField jtfSourceFile; //目标文件路径
JButton selectFile1; //选择文件按钮
JButton selectFile2;
JButton copyButton; //拷贝按钮
JLabel label1,label2;
JButton choose;
JButton apply ;
// 创建一键换色按钮
JButton change;
Color color = Color.BLACK;
public CopyFileDemo(){
super("拷贝文件演示"); //调用父类构造函数
setLayout(new BorderLayout());
JPanel panel = new JPanel(new GridLayout(5,4,10,10));//3行2列 水平间距20 垂直间距10
//创建ButtonGroup按钮组件
ButtonGroup group = new ButtonGroup();
//创建三个JRadioButton单选框
JRadioButton green = new JRadioButton("绿");
JRadioButton yellow = new JRadioButton("黄");
JRadioButton pink = new JRadioButton("粉");
//将单选框添加到同一个ButtonGroup按钮组件中
group.add(green);
group.add(yellow);
group.add(pink);
//为单选按钮定义ActionListener监听器
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Color color = null;
if (green.isSelected()) {//red红单选框是否被选中
color = Color.GREEN;
} else if (yellow.isSelected()) {//yellow黄单选框是否被选中
color = Color.YELLOW;
} else {
color = Color.PINK;
}
panel.setBackground(color);//设置背景颜色
}
};
//为三个单选添加监听器
green.addActionListener(listener);
yellow.addActionListener(listener);
pink.addActionListener(listener);
//将复选框添加到JPanel面板
panel.add(green);
panel.add(yellow);
panel.add(pink);
//JPanel panel = new JPanel();
fileChooser=new JFileChooser(); //实例化文件选择器
//Container container=getContentPane(); //得到容器
JFrame f = new JFrame();
String lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
try {
UIManager.setLookAndFeel(lookAndFeel);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JPanel mainJP=new JPanel(new BorderLayout());
f.add(mainJP,BorderLayout.CENTER);
JScrollPane jsp=new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
mainJP.add(jsp,BorderLayout.CENTER);
JPanel showMainJP=new JPanel(new FlowLayout());
showMainJP.setPreferredSize(new Dimension(800,150));
showMainJP.setBorder(BorderFactory.createTitledBorder("Motif风格"));
//container.setLayout(null);
choose = new JButton("选择颜色");
apply = new JButton("应用颜色");
// 创建一键换色按钮
change = new JButton("一键变色");
jtfSourceFile=new JTextField(16);
jtfTargetFile=new JTextField(16);
label1 = new JLabel("源 文 件");
label2= new JLabel("目标文件");
selectFile1=new JButton("选择");
selectFile2=new JButton("选择");
panel.add(label1); //增加组件到面板(panel)上
panel.add(jtfSourceFile);
panel.add(selectFile1);
panel.add(label2); //增加组件到面板(panel)上
panel.add(jtfTargetFile);
panel.add(selectFile2);
f.add(panel); //增加组件到容器上
apply.setEnabled(false);
choose.addActionListener(e -> {
try {
color = JColorChooser.showDialog(f, "选择字体颜色", color);
} catch (Exception ex) {
ex.printStackTrace();
}
// 应用按钮可用
apply.setEnabled(true);
});
apply.addActionListener(e -> {
label1.setForeground(color);
label2.setForeground(color);
});
change.addActionListener(e -> {
JColorChooser colorChooser = new JColorChooser();
JDialog dialog = JColorChooser.createDialog(f, "请选择颜色", false, colorChooser,
e1 -> label1.setForeground(colorChooser.getColor()), null);
dialog.setVisible(true);
});
change.addActionListener(e -> {
JColorChooser colorChooser = new JColorChooser();
JDialog dialog = JColorChooser.createDialog(f, "请选择颜色", false, colorChooser,
e1 -> label2.setForeground(colorChooser.getColor()), null);
dialog.setVisible(true);
});
panel.add(choose);
panel.add(apply);
panel.add(change);
f.add(panel, BorderLayout.SOUTH);
f.setVisible(true);
copyButton=new JButton("拷贝");
panel.add(copyButton);
f.add(panel);
selectFile1.addActionListener(new SelectFileListener()); //设置选择文件的事件处理
selectFile2.addActionListener(new SelectFileListener());
copyButton.addActionListener(new ActionListener(){ //拷贝按钮事件处理
public void actionPerformed(ActionEvent event) {
String sourceFile=jtfSourceFile.getText(); //得到源文件路径
String targetFile=jtfTargetFile.getText(); //得到目标文件路径
if (copy(sourceFile,targetFile)){ //拷贝文件
JOptionPane.showMessageDialog(CopyFileDemo.this,"拷贝成功"); //显示拷贝成功信息
}
else{
JOptionPane.showMessageDialog(CopyFileDemo.this,"拷贝失败"); //发生错误
}
}
});
f.setSize(800,300); //设置窗口尺寸
f.setVisible(true); //设置窗口可视
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
class SelectFileListener implements ActionListener { //取得目录内容的事件处理
public void actionPerformed(ActionEvent event) {
if (fileChooser.showOpenDialog(CopyFileDemo.this)==JFileChooser.APPROVE_OPTION){ //弹出文件选择器,并判断是否点击了打开按钮
String fileName=fileChooser.getSelectedFile().getAbsolutePath(); //得到选择文件的绝对路径
if (event.getSource().equals(selectFile1)){ //判断事件来自于哪个按钮
jtfSourceFile.setText(fileName); //设置源文件路径
}
else{
jtfTargetFile.setText(fileName); //设置目标文件路径
}
}
}
}
public boolean copy(String file1,String file2){ //拷贝文件方法
try{
java.io.File fileIn=new java.io.File(file1); //用路径名生成源文件
java.io.File fileOut=new java.io.File(file2); //用路径名生成目标文件
FileInputStream fin=new FileInputStream(fileIn); /