import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main {
static int flag;
public static void main(String[] args) {
JFrame frame = new JFrame("HKY");
frame.setBounds(400, 200, 500, 400);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel northPanel = new JPanel();
JTextField textField = new JTextField(20);
JButton button = new JButton("确认");
northPanel.add(textField);
northPanel.add(button);
frame.add(northPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
JTextArea textArea = new JTextArea(10, 30);
centerPanel.add(textArea);
frame.add(centerPanel, BorderLayout.CENTER);
String[] options = {"新建", "打开", "保存","另存为"};
JComboBox<String> comboBox = new JComboBox<>(options);
frame.add(comboBox, BorderLayout.SOUTH);
comboBox.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
JComboBox<String> cb = (JComboBox<String>) e.getSource();
String option = (String) cb.getSelectedItem();
if(option.equals("新建"))
{
flag = 1;
System.out.println("请输入文件地址");
}
if(option.equals("打开"))
{
flag = 2;
}
if (option.equals("保存")) {
flag = 3;
System.out.println("请输入要保存文件的路径");
}
if (option.equals("另存为")) {
flag = 4;
System.out.println("请输入要另存为的文件路径");
}
}
}
);
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
if(flag==1)
{
String filepath = (String)textField.getText();
File file = new File(filepath);
try {
file.createNewFile();
System.out.println("文件创建成功!");
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
if(flag==2)
{
System.out.println("请输入您要打开文件的路径");
String filepath = (String)textField.getText();
openFile(filepath, textArea);
}
if (flag == 3) {
String filepath = textField.getText();
saveFile(filepath, textArea.getText());
}
if (flag == 4) {
String filepath = textField.getText();
saveFileAs(filepath, textArea.getText());
}
}
});
frame.setVisible(true);
}
private static void openFile(String filePath, JTextArea textArea) {
try {
FileReader reader = new FileReader(filePath);
char[] buffer = new char[1024];
int len = 0;
while ((len = reader.read(buffer)) != -1
textArea.append(new String(buffer, 0, len));
}
reader.close();
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
private static void saveFile(String filePath, String content) {
try {
FileOutputStream outputStream = new FileOutputStream(filePath);
outputStream.write(content.getBytes());
outputStream.close();
System.out.println("文件保存成功!");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void saveFileAs(String filePath,String content)
{ JFrame saveFrame = new JFrame("另存为");
saveFrame.setBounds(400, 200, 400, 200);
saveFrame.setLayout(new BorderLayout());
JTextField saveTextField = new JTextField(20);
JButton saveButton = new JButton("保存");
JPanel savePanel = new JPanel();
savePanel.add(saveTextField);
savePanel.add(saveButton);
saveFrame.add(savePanel, BorderLayout.NORTH);
saveButton.addActionListener(new ActionListener()
{ @Override public void actionPerformed(ActionEvent e)
{ String filePath = saveTextField.getText();
saveFile(filePath, content);
saveFrame.dispose();
System.out.println("文件另存为成功!");
} });
saveFrame.setVisible(true); }
}