Java 我的代码生成器赢了';我不能正常工作

Java 我的代码生成器赢了';我不能正常工作,java,arrays,swing,file-io,jtextarea,Java,Arrays,Swing,File Io,Jtextarea,因此,我尝试制作一个代码生成器,如果您输入变量名,比如:“intnums”或“stringwords”等,它将生成setter和getter方法 我的问题是它只能接受一个输入 如果您输入多个变量,它将给出以下结果: 输入 int nums String words public int set nums string() { return nums string; } public int get nums string() { return nums string; }

因此,我尝试制作一个代码生成器,如果您输入变量名,比如:“intnums”或“stringwords”等,它将生成setter和getter方法

我的问题是它只能接受一个输入

如果您输入多个变量,它将给出以下结果:

输入

int nums
String words
public int set nums
string()
{
    return nums
string;
}

public int get nums
string()
{
    return nums
string;
}
输出

int nums
String words
public int set nums
string()
{
    return nums
string;
}

public int get nums
string()
{
    return nums
string;
}
我的方法是使用静态和非静态方法的模板创建两个文本文件,例如,它们如下所示:

public <<Variable>> set <<Name>>()
{
    return <<Name>>;
}

public <<Variable>> get <<Name>>()
{
    return <<Name>>;
}
公共集()
{
返回;
}
公共获取()
{
返回;
}
我知道二传手不应该返回任何东西,但我只是想在我继续前进之前迈出第一步。

然后我将它们存储在一个字符串中,从用户那里获取输入,并使用它们的输入替换字符串的和元素。 以下是我的代码注意:我正在使用Swing使其成为GUI

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.*;
import java.io.*;

public class CodeGenerator {


//Private Variables and Methods

private boolean checked = false;
private String staticTemplate = null;
private String nonStaticTemplate = null;
private ArrayList<String> input = new ArrayList<String>();






 /**
  * Will Replace The Parts Of The Text Documents With the Variable Type And Name And Repeat For All Of The Variables In The Text Area. 
  * It Will Then Put The Result In The Output Text Area For The User To Copy And Paste
  */
 public static void replace(ArrayList<String> inputs, String methods, JTextArea output)
 {
     for(int i = 0; i < inputs.size(); i+=2)
     {
         methods = methods.replaceAll("<<Variable>>", inputs.get(i));
     }

     for(int i = 1; i < inputs.size(); i+=2)
     {
         methods = methods.replaceAll("<<Name>>", inputs.get(i));
     }

     output.setText(methods);


 }





private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CodeGenerator window = new CodeGenerator();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public CodeGenerator() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setResizable(false);
    frame.setBounds(100, 100, 526, 617);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    //This is the place where the generated code will be output
    JTextArea generatedCodeArea = new JTextArea();
    generatedCodeArea.setEditable(true);
    generatedCodeArea.setBounds(10, 140, 490, 367);
    frame.getContentPane().add(generatedCodeArea);

    //This is where text will be entered
    JTextArea inputTextArea = new JTextArea();
    inputTextArea.setBounds(10, 11, 490, 123);
    frame.getContentPane().add(inputTextArea);

    //This is the checkbox for static and non static setter/getter
    JCheckBox nonStatic = new JCheckBox("Non-Static Setter/Getter");
    nonStatic.setBounds(20, 514, 150, 23);
    frame.getContentPane().add(nonStatic);
    nonStatic.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //set the checked variable to use in the action event for the generate button
            boolean selected = nonStatic.isSelected();

            if(selected == true)
            {
                checked = true;
            }else if(selected == false)
            {
                checked = false;
            }
        }

    });


    //Generate Button
    JButton btnGenerate = new JButton("Generate");
    btnGenerate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


            generatedCodeArea.setText("");


            //Store the tempaltes into Strings to be edited later
            try
            {
                staticTemplate = new Scanner(new File("staticGetterSetter.txt")).useDelimiter("\\Z").next();
                nonStaticTemplate = new Scanner(new File("nonStaticGetterSetter.txt")).useDelimiter("\\Z").next();
            } catch(IOException q)
            {
                generatedCodeArea.setText("Something went wrong... " + q.getMessage());
            }
            //Store the input into a String array
            String[] temp = inputTextArea.getText().split(" ");

            //Then add it to an ArrayList
            for(String word : temp)
            {
                input.add(word);
            }


            //Check to see if the checkbox is checked or not
            if(checked == true)
            {
                //do the non-static setter/getter --> WORKS
                replace(input, staticTemplate, generatedCodeArea);



            }else if(checked == false)
            {
                //do the static setter/getter -- WORKS
                replace(input, nonStaticTemplate, generatedCodeArea);



            }
        }
    });
    btnGenerate.setBounds(215, 518, 179, 49);
    frame.getContentPane().add(btnGenerate);




}
private static void addPopup(Component component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        private void showMenu(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
  }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.*;
导入java.io.*;
公共类代码生成器{
//私有变量和方法
私有布尔值选中=false;
私有字符串staticTemplate=null;
私有字符串非静态模板=null;
私有ArrayList输入=新建ArrayList();
/**
*将用变量类型和名称替换部分文本文档,并对文本区域中的所有变量重复此操作。
*然后,它会将结果放入输出文本区域,供用户复制和粘贴
*/
公共静态void replace(ArrayList输入、字符串方法、JTextArea输出)
{
对于(int i=0;i有效吗
替换(输入、staticTemplate、generatedCodeArea);
}else if(选中==false)
{
//静态setter/getter——有效吗
替换(输入、非静态模板、generatedCodeArea);
}
}
});
b产生的挫折(215518179,49);
frame.getContentPane().add(btnGenerate);
}
专用静态void addPopup(组件组件,最终JPOppMenu弹出窗口){
addMouseListener(新的MouseAdapter(){
公共无效鼠标按下(MouseEvent e){
如果(如isPopupTrigger()){
showMenu(e);
}
}
公共无效MouseEvent(MouseEvent e){
如果(如isPopupTrigger()){
showMenu(e);
}
}
专用void显示菜单(MouseEvent e){
show(e.getComponent(),e.getX(),e.getY());
}
});
}
}

如何解决此问题?

问题的原因是

 String[] temp = inputTextArea.getText().split(" ");
如果您使用

int nums
String words
然后你会得到一个数组,比如

[0] int
[1] nums\nString
[2] words
…这不是你真正想要的<