📄 decompressjpanel.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.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class DecompressJPanel {
// The name of the decompressed file
JTextField textFieldOfDecompress;
// The name of the target file
JTextField textFieldOfDecompressTo;
// Show the status of the process
JTextArea decompressStatusArea;
JPanel panel1, panel2, panel3;
DecompressJPanel() {
init();
}
private void init() {
// The panel1 contains the components which determine the file
// decompressed
JLabel label1 = new JLabel("Choose source:");
JButton button1 = new JButton("Select...");
textFieldOfDecompress = new JTextField();
panel1 = new JPanel();
panel1.setLayout(new BorderLayout(10, 10));
panel1.add(label1, BorderLayout.WEST);
panel1.add(button1, BorderLayout.EAST);
panel1.add(textFieldOfDecompress, BorderLayout.SOUTH);
// The panel2 contains the components which determine the folder which
// the file is decompressed to
JLabel label2 = new JLabel("Decompress to:");
JButton button2 = new JButton("Decompress to...");
textFieldOfDecompressTo = new JTextField();
panel2 = new JPanel();
panel2.setLayout(new BorderLayout(10, 10));
panel2.add(label2, BorderLayout.WEST);
panel2.add(button2, BorderLayout.EAST);
panel2.add(textFieldOfDecompressTo, BorderLayout.SOUTH);
// panel3 contains the components which are used to control the process
// and show the status
JLabel label7 = new JLabel("Status:");
JButton button6 = new JButton("OK");
panel3 = new JPanel();
decompressStatusArea = new JTextArea();
decompressStatusArea.setBackground(Color.LIGHT_GRAY);
decompressStatusArea.setEditable(false);
decompressStatusArea.setText("PREPARE TO DECOMPRESS");
JPanel p23 = new JPanel();
p23.setLayout(new BorderLayout(10, 10));
p23.add(label7, BorderLayout.WEST);
p23.add(button6, BorderLayout.EAST);
panel3.setLayout(new BorderLayout(10, 10));
panel3.add(p23, BorderLayout.NORTH);
panel3.add(decompressStatusArea, BorderLayout.CENTER);
// the actionListener for decompress Panel
ButtonListener2 btListener2 = new ButtonListener2();
button1.addActionListener(btListener2);
button2.addActionListener(btListener2);
button6.addActionListener(btListener2);
}
public JPanel getDefaultJPanel() {
// construct the panel of the decompression
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(2, 1, 20, 20));
p2.add(panel1);
p2.add(panel2);
JPanel decompressPanel = new JPanel();
decompressPanel.setLayout(new GridLayout(2, 1, 20, 20));
decompressPanel.add(p2);
decompressPanel.add(panel3);
return decompressPanel;
}
// 解压面的listener
String decompressFile, decompressToFile;// The name of the 2 files
String f2;// The path
String fileName;
int mode;
private class ButtonListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Select...")) {
// Get the name of the decompressed file and show the default
// path
mode = JFileChooser.FILES_AND_DIRECTORIES;
if ((decompressFile = showLoad()) != null) {
while (decompressFile.charAt(decompressFile.length() - 1) != '~') {
JOptionPane
.showMessageDialog(null,
"Please choose the correct file,whose name with \'~\'");
decompressFile = showLoad();
}
textFieldOfDecompress.setText(decompressFile);
f2 = decompressFile.substring(0, decompressFile
.lastIndexOf("~"));
textFieldOfDecompressTo.setText(f2);
}
} else if (e.getActionCommand().equals("Decompress to...")) {
// Get the path of the decompression and the name of the file
// which is the target file
mode = JFileChooser.DIRECTORIES_ONLY;
if ((decompressToFile = showLoad()) != null)
textFieldOfDecompressTo.setText(decompressToFile + "\\"
+ fileName.substring(0, fileName.length() - 1));
} else if (e.getActionCommand().equals("OK")) {
try {
// String s = textFieldOfDecompressTo.getText();
long start = System.currentTimeMillis();
new logical.Decompression(textFieldOfDecompress.getText(),
textFieldOfDecompressTo.getText());
long end = System.currentTimeMillis();
decompressStatusArea.setText("DECOMPRESS DONE!\n"
+ "DECOMPRESS TIME IS " + (end - start) + "Millis");
} catch (Exception ex) {
}
;
}
}
}
// 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_AND_DIRECTORIES)
fileName = result.getName();
return result.getPath();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -