我已经在这里待了好几年了,我终于感到很沮丧,不敢问这个问题。我对应用程序的基本理解能够做到极限。我非常感谢该计划任何方面的任何帮助或建议。
我在这个程序中的目标是创建本质上是"自动更正"简单文本编辑器上的功能。在名为"命令"的文件夹中有一系列文本文件标题为" ThisCommand("包含一个字符串,如" ThisCommand(0000,1111,2222)"。每当我输入一些被识别为在文本编辑器中使用这种格式的东西,我想填写同名文件中的内容。例如,在最终的应用程序中,如果我输入" ThisCommand("在编辑器中)该程序会自动将我的条目更改为" ThisCommand(0000,1111,2222)"。
把它分解成它的各个部分,我已经知道如何......
我挣扎的是......
到目前为止,这是我的相关代码。一切正常,我不知道从哪里开始。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JSeparator;
public class UI {
private JFrame frame;
private JTextArea textArea;
private static ArrayList<String> commands;
private static ArrayList<String> commands_override;
public static ArrayList<String> getCommands() {return commands;}
public static ArrayList<String> getCommands_Override() {return commands_override;}
/**
* Launch the application.
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
importFolderContents();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UI window = new UI();
window.frame.setVisible(true);
} catch (Exception e) {e.printStackTrace();}
}
});
}
/**
* Create the application.
*/
public UI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setBounds(0,0,screenSize.width, screenSize.height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem NewEvent = new JMenuItem("New");
file.add(NewEvent);
JSeparator separator1 = new JSeparator();
file.add(separator1);
JMenuItem SaveEvent = new JMenuItem("Save"); // TODO JFileChooser
file.add(SaveEvent);
JMenuItem LoadEvent = new JMenuItem("Load");
file.add(LoadEvent);
JSeparator separator2 = new JSeparator();
file.add(separator2);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menuBar.add(help);
JMenuItem Shortcuts = new JMenuItem("Shortcuts");
help.add(Shortcuts);
frame.getContentPane().add(menuBar, BorderLayout.NORTH);
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
frame.getContentPane().add(scroll, BorderLayout.CENTER);
JPanel buttons = new JPanel();
frame.getContentPane().add(buttons, BorderLayout.WEST);
JButton btnNewButton = new JButton("New button");
buttons.add(btnNewButton);
}
/** Imports the list of commands that can be replaced in the current session.
*
* Starts by combing the "commands" folder for the titles of each file. When it has
* made an array list of the names, an equivalent index array is filled with strings
* that will override the commands entered.
*
* @throws FileNotFoundException If this somehow gets thrown, god help you.
*
*/
private static void importFolderContents() throws FileNotFoundException {
commands = new ArrayList<String>();
commands_override = new ArrayList<String>();
// Imports the filenames of all of the contents of the commands folder
File folder1 = new File("./commands");
File[] fileList1 = folder1.listFiles();
for (int i = 0; i < fileList1.length; i++) {
String a = fileList1[i].getName();
if (a.contains(".txt")) {
String target = a.replaceFirst(".txt", "");
commands.add(target);
Scanner reader = new Scanner(fileList1[i]);
String replacement = reader.nextLine();
commands_override.add(replacement);
reader.close();
}
}
}
}