我的代码生成器无法正常工作

时间:2014-10-16 01:53:44

标签: java arrays swing file-io jtextarea

所以我正在尝试创建一个代码生成器,如果输入变量名称,将生成setter和getter方法:“int nums”或“String words”等。

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

如果您输入多个变量,它会给我这个:

输入

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>>;
}

我知道setter不应该返回任何东西,但我只是想在我继续前进这个第一步。

然后我将这些存储在一个字符串中并从用户那里获得输入并使用他们的输入替换&lt;&gt;和&lt;&gt;字符串的元素。 这是我的代码注意:我使用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());
        }
    });
  }
}

如何解决问题?

1 个答案:

答案 0 :(得分:2)

问题的原因是......

 String[] temp = inputTextArea.getText().split(" ");

如果您使用

的输入
int nums
String words

然后你会得到一些类似......的数组

[0] int
[1] nums\nString
[2] words

......这不是你想要的......

首先需要将输入拆分为行...

 String[] lines = inputTextArea.getText().split("\n");

然后,对于每一行,将其拆分为单词......

for (String line : lines) {
    String[] temp = line.split(" ");
}

然后我会使用output.append(methods)或更改replace方法以返回String ...

ps:我知道这可能是一个&#34;个人&#34;项目,但你真的应该避免使用null布局,像素完美的布局是现代ui设计中的错觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的结束,您将花费越来越多的时间来纠正。

有关详细信息,请参阅Why is it frowned upon to use a null layout in SWING? ...