📄 cofecutgui.java
字号:
/////////////////////////////////////////////////
//////////////CofeCutGUI1.0//////////////////////
/////////////此程序用于文件GUI //////////////////
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.border.*;
public class CofeCutGUI implements ActionListener
{
public CofeCutGUI()
{
MainWindow=new JFrame("CofeCut 1.0");
theKit = MainWindow.getToolkit();
Dimension wndSize = theKit.getScreenSize();
MainWindow.setBounds(wndSize.width/4,wndSize.height/3,
wndSize.width/2,wndSize.height/4);
MainWindow.setLocation(wndSize.width/3,wndSize.height/4);
MainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel MainPanel = new JPanel();
MainPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.blue,2),
"CofeCut 1.0",TitledBorder.CENTER,TitledBorder.TOP));
MainPanel.setLayout(new GridLayout(1,2));
MainPanel.setBackground(new Color(180,207,245));
content = MainWindow.getContentPane();
Cut = new CofeButton("切割文件",new ImageIcon(".\\resources\\cut.jpg"),false,false);
Cut.setBackground(new Color(180,207,245));
Link = new CofeButton("合并文件",new ImageIcon(".\\resources\\link.jpg"),false,false);
Link.setBackground(new Color(180,207,245));
content.add(MainPanel);
MainPanel.add(Cut);
MainPanel.add(Link);
Cut.addActionListener(this);
Link.addActionListener(this);
this.Width=wndSize.width;
this.Height=wndSize.height;
MainWindow.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==Cut){
if(CutDialogflag==0){
CutDialogflag=1;
cutDialog = new CutDialog(MainWindow,"CutFile");
cutDialog.setLocation(this.Width/3,this.Height/3);
cutDialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
CutDialogflag=0;
cutDialog.dispose();}});
cutDialog.pack();
cutDialog.show();
}
else
Toolkit.getDefaultToolkit().beep();
}
else if(e.getSource()==Link){
if(LinkDialogflag==0){
LinkDialogflag=1;
linkDialog = new LinkDialog(MainWindow,"LinkFile");
linkDialog.setLocation(this.Width/3,this.Height/3);
linkDialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
LinkDialogflag=0;
linkDialog.dispose();}});
linkDialog.pack();
linkDialog.show();
}
else
Toolkit.getDefaultToolkit().beep();
}
}
private JFrame MainWindow = null;//主窗口
private Toolkit theKit = null;
private Dimension wndSize = null;
private Container content = null;//主容器
private CofeButton Cut = null;
private CofeButton Link = null;
private CutDialog cutDialog = null;
private LinkDialog linkDialog = null;
int CutDialogflag=0;
int LinkDialogflag=0;
int Width;
int Height;
class CutDialog extends JDialog implements ActionListener
{
private JLabel label1,label2,label3;
private JTextField selectText,Bytes;
private CofeButton select,Cut,Close;
private Container Dialogcontent;
private JFrame Wnd=null;
public File file;
CutDialog(JFrame Wnd,String title){
this.Wnd=Wnd;
setTitle(title);
Dialogcontent=getContentPane();
label1 = new JLabel("选择文件:");
label1.setBackground(new Color(180,207,245));
selectText= new JTextField();
selectText.addActionListener(this);
select = new CofeButton("选择",new ImageIcon(".\\resources\\select.jpg"),false,false);
select.setBackground(new Color(180,207,245));
select.addActionListener(this);
label2 = new JLabel("切割后大小:");
label2.setBackground(new Color(180,207,245));
Bytes = new JTextField();
label3 = new JLabel("Bytes");
Cut = new CofeButton("开始切割",new ImageIcon(".\\resources\\cut.jpg"),false,false);
Cut.setBackground(new Color(180,207,245));
Cut.addActionListener(this);
Close = new CofeButton("关闭",new ImageIcon(".\\resources\\close.jpg"),false,false);
Close.setBackground(new Color(180,207,245));
Close.addActionListener(this);
JPanel Head = new JPanel();
Head.setBackground(new Color(180,207,245));
Head.setLayout(new GridLayout(2,3));
JPanel Bottom = new JPanel();
Bottom.setBackground(new Color(180,207,245));
Bottom.setLayout(new GridLayout(1,2));
Head.add(label1);
Head.add(selectText);
Head.add(select);
Head.add(label2);
Head.add(Bytes);
Head.add(label3);
Bottom.add(Cut);
Bottom.add(Close);
Dialogcontent.setLayout(new GridLayout(2,1));
Dialogcontent.add(Head);
Dialogcontent.add(Bottom);
setBackground(new Color(180,207,245));
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==select){
JFileChooser chooser = new JFileChooser();
int result;
chooser.setApproveButtonText("确定");
chooser.setDialogTitle("选择文件");
result = chooser.showOpenDialog(Wnd);
if(result==JFileChooser.APPROVE_OPTION)
{
file = chooser.getSelectedFile();
selectText.setText(file.getAbsolutePath());
/*System.out.println(file.getParent());
System.out.println(file.getAbsolutePath());
System.out.println(file.getName());*/
}
if(result==JFileChooser.CANCEL_OPTION)
{
selectText.setText("");
}
}
else if(e.getSource()==Close){
CutDialogflag=0;
dispose();
}
else if(e.getSource()==Cut){
//System.out.println(selectText.getText());
file = new File(selectText.getText());
/*if(file.isFile()){System.out.println("test ok"+selectText.getText());
System.out.println(*/
String SubString=file.getAbsolutePath().substring(0,file.getAbsolutePath().length()-file.getName().length());
if(file!=null&&!(Bytes.getText().equals(""))&&file.isFile()){
CutFile cutFile = new CutFile(SubString,file.getName());
String size= Bytes.getText();
char[] sizeArray = size.toCharArray();
boolean test = true;
for(int i=0;i<sizeArray.length;i++){
if(!Character.isDigit(sizeArray[i]))
{JOptionPane.showMessageDialog(Wnd,"数据不合法","CofeCut",
JOptionPane.ERROR_MESSAGE);
test=false;
break;
}
}
if(test){
if(cutFile.ReadFile()){
cutFile.FileNumber(new Float(size).floatValue());
cutFile.DoCut();
cutFile.CreateKey();
JOptionPane.showMessageDialog(Wnd,"完成","CofeCut",
JOptionPane.INFORMATION_MESSAGE);}
else{JOptionPane.showMessageDialog(Wnd,"失败","CofeCut",
JOptionPane.INFORMATION_MESSAGE);}
}
}
else{JOptionPane.showMessageDialog(Wnd,"文件或目录不存在","CofeCut",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
class LinkDialog extends JDialog implements ActionListener
{
private JFrame Wnd = null;
private JLabel label1;
private JTextField selectText;
private CofeButton select,Link,Close;
private Container Dialogcontent;
public File file;
LinkFile linkFile;
LinkDialog(JFrame Wnd,String title){
this.Wnd = Wnd;
setTitle(title);
Dialogcontent=getContentPane();
label1 = new JLabel("选择Key文件:");
label1.setBackground(new Color(180,207,245));
selectText= new JTextField();
select = new CofeButton("选择",new ImageIcon(".\\resources\\select.jpg"),false,false);
select.setBackground(new Color(180,207,245));
select.addActionListener(this);
Link = new CofeButton("开始合并",new ImageIcon(".\\resources\\link.jpg"),false,false);
Link.setBackground(new Color(180,207,245));
Link.addActionListener(this);
Close = new CofeButton("关闭",new ImageIcon(".\\resources\\close.jpg"),false,false);
Close.setBackground(new Color(180,207,245));
Close.addActionListener(this);
JPanel Head = new JPanel();
Head.setBackground(new Color(180,207,245));
Head.setLayout(new GridLayout(1,3));
JPanel Bottom = new JPanel();
Bottom.setBackground(new Color(180,207,245));
Bottom.setLayout(new GridLayout(1,2));
Head.add(label1);
Head.add(selectText);
Head.add(select);
Bottom.add(Link);
Bottom.add(Close);
Dialogcontent.setLayout(new GridLayout(2,1));
Dialogcontent.add(Head);
Dialogcontent.add(Bottom);
setBackground(new Color(180,207,245));
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==select){
JFileChooser chooser = new JFileChooser();
int result;
chooser.setApproveButtonText("确定");
chooser.setDialogTitle("选择文件");
result = chooser.showOpenDialog(Wnd);
if(result==JFileChooser.APPROVE_OPTION)
{
file = chooser.getSelectedFile();
selectText.setText(file.getAbsolutePath());
}
if(result==JFileChooser.CANCEL_OPTION)
{
selectText.setText("");
}
}
else if(e.getSource()==Close){
LinkDialogflag=0;
dispose();
}
else if(e.getSource()==Link){
file = new File(selectText.getText());
String SubString=file.getAbsolutePath().substring(0,file.getAbsolutePath().length()-file.getName().length());
if(file!=null&&file.isFile()){
linkFile = new LinkFile(SubString,file.getName());
if(linkFile.ReadKey()){
if(linkFile.DoLink()){
JOptionPane.showMessageDialog(Wnd,"完成","CofeCut",
JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(Wnd,"失败--请检查文件是否在同一目录中","CofeCut",
JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(Wnd,"失败--Key文件被破坏","CofeCut",
JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(Wnd,"文件或目录不存在","CofeCut",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
class CofeButton extends JButton
{
CofeButton(String text,ImageIcon icon,boolean BorderPainted,
boolean FocusPainted){
setText(text);
setIcon(icon);
setBorderPainted(BorderPainted);
setFocusPainted(FocusPainted);
}
CofeButton(ImageIcon icon,boolean BorderPainted,
boolean FocusPainted){
setIcon(icon);
setBorderPainted(BorderPainted);
setFocusPainted(FocusPainted);
}
CofeButton(String text,boolean BorderPainted,
boolean FocusPainted){
setText(text);
setBorderPainted(BorderPainted);
setFocusPainted(FocusPainted);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -