package gui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; public class MyWindow { public static void main(String[] args) { //创建窗口 JFrame frame=new JFrame("我的第一个窗口"); //设置大小 frame.setSize(800,600); //位置,左上角原点坐标 frame.setLocation(100,100); //设置jframe的布局为null frame.setLayout(null); //创建按钮 JButton button=new JButton("确定" ); //设置边界==大小+位置(相对于所在容器的) button.setBounds(0, 0, 100, 50); //给按钮添加监听器 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); } }); //将按钮添加给容器 frame.add(button); frame.show(); } }
记事本:
package gui; import java.awt.Color; import java.awt.FileDialog; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** * 主窗口 * @author ASUS * */ public class MainFrame extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = -8026416994513756565L; //按钮 private JButton btnOK; private JButton btnCancel; private JTextArea txtArea; //菜单项 private JMenuItem miOpen; private JMenuItem miExit; public MainFrame() { initFrame(); this.setVisible(true); } /** * 初始化 */ private void initFrame() { //标题 this.setTitle("主窗口"); //边界 this.setBounds(100, 100, 820, 600); //绝对布局 this.setLayout(null); Font font=new Font("微软雅黑",Font.BOLD,27); //将文本域添加到滚动面板中 txtArea = new JTextArea(); txtArea.setFont(font); //滚动面板 JScrollPane scrollPane=new JScrollPane(txtArea); scrollPane.setBounds(0,0,800,470); this.add(scrollPane); btnOK = new JButton("保存"); btnOK.setBounds(500, 475, 100, 50); btnOK.setFont(font); btnOK.addActionListener(this); this.add(btnOK); btnCancel = new JButton("取消"); btnCancel.setBounds(610, 475, 100, 50); btnCancel.setFont(font); btnCancel.addActionListener(this); this.add(btnCancel); //添加窗口事件处理程序,使用适配器 this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(-1); } }); //添加菜单栏 JMenuBar menuBar=new JMenuBar(); //添加菜单 JMenu menu=new JMenu("文件"); miOpen = new JMenuItem("打开"); miOpen.addActionListener(this); //添加菜单项 menu.add(miOpen); //分隔符 menu.addSeparator(); miExit = new JMenuItem("退出"); menu.add(miExit); miExit.addActionListener(this); menuBar.add(menu); this.setJMenuBar(menuBar); } /** * 动作处理程序 */ @Override public void actionPerformed(ActionEvent e) { //得到事件的源 Object es=e.getSource(); //保存文件 if(es==btnOK) { try { //打开保存对话框,定位保存文件的位置 FileDialog d=new FileDialog(this, "保存", FileDialog.SAVE); d.setVisible(true); File f= new File(d.getDirectory(),d.getFile()); String str=txtArea.getText(); FileWriter writer=new FileWriter(f); writer.write(str); writer.close(); txtArea.setText(""); } catch (IOException e1) { e1.printStackTrace(); } }else if(es==btnCancel) { this.dispose(); } //是否是菜单项 else if(es==miOpen) { FileDialog d=new FileDialog(this,"打开",FileDialog.LOAD); d.setVisible(true); String dir=d.getDirectory(); String f=d.getFile(); if(dir!=null && f!=null) { try { txtArea.setText(null); FileReader reader = new FileReader(new File(dir,f)); char[] buffer=new char[1024]; int len=-1; while((len=reader.read(buffer))!=-1) { txtArea.setText(txtArea.getText()+new String(buffer,0,len)); } reader.close(); } catch (Exception e1) { e1.printStackTrace(); } } } else if(es==miExit) { System.exit(-1); } } }
package gui; public class MyEdito { public static void main(String[] args) { MainFrame frame=new MainFrame(); } }
深度复制:
package mulcopier; import java.io.FileNotFoundException; import java.io.RandomAccessFile; /** *@author :王团结 *@version: 2019年6月18日下午10:51:22 *类说明: *复制器 */ public class Copier { private CopyUI copyui; //源文件 private String srcFile; //目标目录 private String destDir; //线程数 private int count; public Copier(CopyUI copyui,String srcFile, String destDir, int count) { super(); this.copyui=copyui; this.srcFile = srcFile; this.destDir = destDir; this.count = count; } /** * 开始复制文件 */ public void startCopy() { int start=0; int end=0; RandomAccessFile raf=null; try { //计算源文件大小 raf = new RandomAccessFile(srcFile, "r"); int fileLength=(int)raf.length(); //设置进度条的最大值 copyui.bar.setMaximum(fileLength); //每个线程复制的块大小 int block=fileLength/count; for(int i=0;i<count;i++) { start=i*block; if(i!=(count-1)) { end=(i+1)*block-1; }else { end=fileLength-1; } new CopyThread(copyui,srcFile,destDir,start,end).start(); } } catch (Exception e) { e.printStackTrace(); } } }
package mulcopier; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JProgressBar; import javax.swing.JTextField; /** * 主窗口 * @author ASUS * */ public class CopyUI extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 1L; //srcFile private JLabel lblSrcFile; private JTextField tfSrcFile; //DestDir private JTextField tfDestDir; private JLabel lblDestDir; //线程数 private JLabel lblCount; private JTextField tfCount; //开始按钮 private JButton btnStart; public JProgressBar bar; public CopyUI() { init(); } private void init() { this.setTitle("主窗口"); this.setBounds(100,100,800,600); this.setLayout(null); //SrcFile标签 lblSrcFile = new JLabel("源文件"); lblSrcFile.setBounds(10, 10, 80, 30); this.add(lblSrcFile); tfSrcFile = new JTextField(); tfSrcFile.setText("D:\\学习\\02大数据基础Hadoop 2.X\\大数据软件工具\\hadoop-2.5.0-cdh5.3.6-src.tar.gz"); tfSrcFile.setBounds(80, 10, 600, 30); this.add(tfSrcFile); //DestDir标签 lblDestDir = new JLabel("目标目录"); lblDestDir.setBounds(10, 80, 80, 30); this.add(lblDestDir); tfDestDir = new JTextField(); tfDestDir.setBounds(80,80, 600, 30); tfDestDir.setText("d:/arch/hadoop-2.5.0-cdh5.3.6-src.tar.gz"); this.add(tfDestDir); //线程数 lblCount = new JLabel("线程数"); lblCount.setBounds(10, 110, 80, 30); this.add(lblCount); tfCount = new JTextField(); tfCount.setBounds(80, 110, 600, 30); tfCount.setText(""+5); this.add(tfCount); //开始按钮 btnStart = new JButton("开始"); btnStart.setBounds(10, 160, 100, 30); btnStart.addActionListener(this); this.add(btnStart); //进度条 bar = new JProgressBar(); bar.setBounds(10, 200,650, 50); // bar.setMaximum(100); // bar.setValue(50); this.add(bar); this.setVisible(true); //设置窗口适配器 this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(-1); } }); } //添加按钮监听 public void actionPerformed(ActionEvent e) { Object source=e.getSource(); //开始按钮 if(source==btnStart) { String srcFile=tfSrcFile.getText(); String destDir=tfDestDir.getText(); int count=Integer.parseInt(tfCount.getText()); //创建复制器对象 Copier copier=new Copier(this,srcFile, destDir, count); copier.startCopy(); } } }
package mulcopier; public class App { public static void main(String[] args) { CopyUI w=new CopyUI(); } }