📄 compressjpanel.java
字号:
package GUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import logical.Compress;
public class CompressJPanel {
/**
* The Panel for the compress function
*/
private static final long serialVersionUID = 1L;
// The name of the compressed file
private JTextField textFieldOfCompress;
// The name of the target file
private JTextField textFieldOfCompressTo;
// Show the status of the process
private JTextArea compressStatusArea;
private JPanel panel1, panel2, panel3;
private JButton button1, button2;
CompressJPanel() {
init();
}
@SuppressWarnings("deprecation")
private void init() {
// The panel1 contains the components which determine the file
// compressed
JLabel label1 = new JLabel("Choose source:");
button1 = new JButton("Select...");
button1.setEnabled(false);
textFieldOfCompress = new JTextField();
textFieldOfCompress.setBackground(Color.white);
textFieldOfCompress.setEditable(false);
panel1 = new JPanel();
panel1.setLayout(new BorderLayout(10, 10));
panel1.add(label1, BorderLayout.WEST);
panel1.add(button1, BorderLayout.EAST);
panel1.add(textFieldOfCompress, BorderLayout.SOUTH);
// The panel2 contains the components which determine the folder which
// the file is compressed to
JLabel label2 = new JLabel("Compress to:");
button2 = new JButton("Compress to...");
button2.setEnabled(false);
textFieldOfCompressTo = new JTextField();
textFieldOfCompressTo.setBackground(Color.white);
textFieldOfCompressTo.setEditable(false);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout(10, 10));
panel2.add(label2, BorderLayout.WEST);
panel2.add(button2, BorderLayout.EAST);
panel2.add(textFieldOfCompressTo, BorderLayout.SOUTH);
// panel3 contains the components which are used to control the process
// and show the status
JLabel label3 = new JLabel("Status:");
compressStatusArea = new JTextArea();
compressStatusArea.setBackground(Color.LIGHT_GRAY);
compressStatusArea.setEditable(false);
compressStatusArea.setText("PREPARE TO COMPRESS");
JPanel panel31 = new JPanel();
panel31.setLayout(new GridLayout(2, 1, 10, 10));
panel31.add(label3);
panel31.add(compressStatusArea);
// Control the pattern of the compress
JLabel label4 = new JLabel("mode");
JButton button3 = new JButton("OK");
JRadioButton folderOrFile = new JRadioButton("Single File");
JRadioButton folder = new JRadioButton("Single Folder (Unfinished)");
ButtonGroup group = new ButtonGroup();
group.add(folderOrFile);
group.add(folder);
folderOrFile.setSelected(false);
folder.setSelected(false);
ButtonListener listener = new ButtonListener();
folderOrFile.addActionListener(listener);
folder.addActionListener(listener);
JPanel panel32 = new JPanel();
panel32.setLayout(new GridLayout(4, 1, 10, 10));
panel32.add(label4);
panel32.add(folderOrFile);
panel32.add(folder);
panel32.add(button3);
panel3 = new JPanel();
panel3.setLayout(new GridLayout(1, 2, 10, 10));
panel3.add(panel31);
panel3.add(panel32);
// the actionListener for compress Panel
ButtonListener btListener1 = new ButtonListener();
button1.addActionListener(btListener1);
button2.addActionListener(btListener1);
button3.addActionListener(btListener1);
}
public JPanel getDefaultJPanel() {
// construct the panel of the compression
JPanel compressPanel = new JPanel();
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 1, 20, 20));
p1.add(panel1);
p1.add(panel2);
compressPanel.setLayout(new GridLayout(2, 1, 20, 20));
compressPanel.add(p1);
compressPanel.add(panel3);
return compressPanel;
}
String compressFile, compressToFile;// The name of the 2 files
String f1;// The path
String fileName = "";
int mode;
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Select...")) {
// Get the name of the compressed file and show the default path
mode = JFileChooser.FILES_ONLY;
if ((compressFile = showLoad()) != null) {
textFieldOfCompress.setText(compressFile);
f1 = compressFile + "~";
textFieldOfCompressTo.setText(f1);
}
button2.setEnabled(true);
} else if (e.getActionCommand().equals("Compress to...")) {
// Get the path of the compression and the name of the file
// which is the target file
mode = JFileChooser.DIRECTORIES_ONLY;
if ((compressToFile = showLoad()) != null)
textFieldOfCompressTo.setText(compressToFile + "\\"
+ fileName + "~");
} else if (e.getActionCommand().equals("OK")) {
try {
long start = System.currentTimeMillis();
new Compress(textFieldOfCompress.getText(),
textFieldOfCompressTo.getText());
long end = System.currentTimeMillis();
compressStatusArea.setText("COMPRESS DONE!\n"
+ "COMPRESS TIME IS " + (end - start) + "Millis");
} catch (Exception ex) {
}
} else if (e.getActionCommand().equals("Single File")) {
button1.setEnabled(true);
} else if (e.getActionCommand().equals("Single Folder (Unfinished)")) {
button1.setEnabled(true);
}
}
}
// Show the choices
private String showLoad() {
JFileChooser file = new JFileChooser();
file.setFileSelectionMode(mode);
file.showSaveDialog(new JPanel());
File result = file.getSelectedFile();
if (mode == JFileChooser.FILES_ONLY)
fileName = result.getName();
return result.getPath();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -