Java应用程序中的活动文本替换
我已经在这方面做了很多年了,最后我很沮丧地问了这个问题。我对应用程序的基本理解已经到了极限。我真的非常感谢任何帮助或建议在任何方面的计划 我在这个程序中的目标是在一个简单的文本编辑器上创建本质上是“自动更正”的功能。在一个名为“commands”的文件夹中,有一系列名为“thiscondm”的文本文件(“包含一个字符串,如“thiscondm(00001112222)”。每当我在文本编辑器中键入被识别为此格式的内容时,我都希望填写同名文件中的内容。例如,在最终应用程序中,如果我键入“ThisCommand”(“在编辑器中,程序会自动将我的条目更改为“ThisCommand(00001112222)” 把它分解成各个部分,我已经知道如何Java应用程序中的活动文本替换,java,eclipse,Java,Eclipse,我已经在这方面做了很多年了,最后我很沮丧地问了这个问题。我对应用程序的基本理解已经到了极限。我真的非常感谢任何帮助或建议在任何方面的计划 我在这个程序中的目标是在一个简单的文本编辑器上创建本质上是“自动更正”的功能。在一个名为“commands”的文件夹中,有一系列名为“thiscondm”的文本文件(“包含一个字符串,如“thiscondm(00001112222)”。每当我在文本编辑器中键入被识别为此格式的内容时,我都希望填写同名文件中的内容。例如,在最终应用程序中,如果我键入“ThisCo
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();
}
}
}
}
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Toolkit;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.Scanner;
导入javax.swing.JFrame;
导入javax.swing.JMenu;
导入javax.swing.JMenuBar;
导入javax.swing.JMenuItem;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.JButton;
导入javax.swing.jsepator;
公共类用户界面{
私有JFrame;
私人JTEXTEXTAREA textArea;
私有静态ArrayList命令;
私有静态ArrayList命令\u覆盖;
公共静态ArrayList getCommands(){return commands;}
公共静态ArrayList getCommands_Override(){return commands_Override;}
/**
*启动应用程序。
*@抛出FileNotFoundException
*/
公共静态void main(字符串[]args)引发FileNotFoundException{
ImportFolderContent();
invokeLater(新的Runnable(){
公开募捐{
试一试{
UI窗口=新UI();
window.frame.setVisible(true);
}catch(异常e){e.printStackTrace();}
}
});
}
/**
*创建应用程序。
*/
公共用户界面(){
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化(){
维度screenSize=Toolkit.getDefaultToolkit().getScreenSize();
frame=新的JFrame();
setExtendedState(JFrame.MAXIMIZED_二者);
框.立根(0,0,屏幕大小.宽度,屏幕大小.高度);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(新的BorderLayout());
JMenuBar menuBar=新的JMenuBar();
JMenu文件=新JMenu(“文件”);
menuBar.add(文件);
JMenuItem NewEvent=新JMenuItem(“新”);
file.add(NewEvent);
jsepator separator 1=新的jsepator();
添加(分隔符1);
JMenuItem SaveEvent=new JMenuItem(“Save”);//TODO JFileChooser
file.add(SaveEvent);
JMenuItem LoadEvent=新的JMenuItem(“加载”);
添加(LoadEvent);
jsepator separator 2=新的jsepator();
添加(分隔符2);
JMenuItem退出=新的JMenuItem(“退出”);
文件。添加(退出);
JMenu help=新JMenu(“help”);
菜单栏。添加(帮助);
JMenuItem快捷方式=新的JMenuItem(“快捷方式”);
帮助。添加(快捷方式);
frame.getContentPane().add(menuBar,BorderLayout.NORTH);
textArea=新的JTextArea();
JScrollPane scroll=新的JScrollPane(textArea);
frame.getContentPane().add(滚动,BorderLayout.CENTER);
JPanel按钮=新的JPanel();
frame.getContentPane().add(按钮,BorderLayout.WEST);
JButton btnNewButton=新JButton(“新按钮”);
按钮。添加(BtnewButton);
}
/**导入可在当前会话中替换的命令列表。
*
*首先梳理“commands”文件夹中每个文件的标题
*创建一个名称数组列表,一个等价的索引数组中填充字符串
*这将覆盖输入的命令。
*
*@throws FileNotFoundException如果这个被抛出,上帝保佑你。
*
*/
私有静态void importFolderContents()引发FileNotFoundException{
commands=newarraylist();
命令_override=new ArrayList();
//导入commands文件夹中所有内容的文件名
File folder1=新文件(“./命令”);
File[]fileList1=folder1.listFiles();
for(int i=0;i
您必须将侦听器添加到文本字段中,并侦听keydown事件(或keydup),然后检查光标在字段上的位置,获取字段后面的文本,直到开始或第一个分隔符(空格、点、逗号等)并替换为从文件中读取的内容。这一行中的内容: