您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页代码生成器

代码生成器

来源:爱玩科技网


项目:代码生成器

Swing与AWT的结合使用

Swing组件的使用

弹出菜单的使用

常用的使用

设计要求:

要求在一个应用程序中,能过录入类名,属性类型,属性名,来生成一个具体的类代码,代码中包含构造器及每个属性所对应的set和get方法。这部分代码要求显示在一个文本区中,这部分代码要求有复制,粘贴功能。

CodeCreate类,生成代码的JFame

MousePopMenu类,弹出菜单

VarCode类的属性封装类

运行效果如下:

初始界面

在下面的文本框中输入类名,类型,类变量,后点击增加类变量按钮,每个类变量就会在右面的区域显示出来。点击生成代码按钮后在主界面中生成了相应的代码。

点击鼠标右键的弹出菜单中有一系列的功能

代码如下:

public class VarCode

{

private String type;

private String field;

public void setType(String type)

{

this.type=type;

}

public String getType()

{

return type;

}

public void setField(String field)

{

this.field=field;

}

public String getField()

{

return this.field;

}

public static String toFirsttoUpper(String str)

{

String first=str.substring(0,1);

String last=str.substring(1);

return first.toUpperCase()+last;

}

}

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.*;

public class MousePopMenu extends JPopupMenu

{

private JTextComponent jtc;

public MousePopMenu(JTextComponent jtxc)

{

Toolkit kit=Toolkit.getDefaultToolkit();

this.jtc=jtxc;

JMenuItem copy=new JMenuItem(\"ImageIcon(kit.getImage(\"018.gif\")));

copy.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

复制\

{

jtc.copy();

}

});

JMenuItem cut=new JMenuItem(\"ImageIcon(kit.getImage(\"018.gif\")));

cut.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

jtc.cut();

}

});

JMenuItem copyall=new JMenuItem(\"全部剪选中切并复\

制\

ImageIcon(kit.getImage(\"018.gif\")));

copyall.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

jtc.selectAll();

jtc.copy();

}

});

JMenuItem paste=new JMenuItem(\"ImageIcon(kit.getImage(\"018.gif\")));

paste.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

粘贴\

{

jtc.paste();

}

});

JMenuItem selectall=new JMenuItem(\"ImageIcon(kit.getImage(\"018.gif\")));

selectall.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

jtc.selectAll();

}

});

// setBorderPainted(false);

全部选中\

this.add(copy);

// this.addSeparator();

this.add(cut);

this.addSeparator();

this.add(selectall);

this.add(copyall);

this.addSeparator();

this.add(paste);

}

}

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.util.*;

public class CodeCreate extends JFrame

{

private ArrayList fieldList;

private JPopupMenu pop;

public CodeCreate()

{

this.setTitle(\"代码生成器\");

fieldList=new ArrayList();

JPanel p=new JPanel();

p.setLayout(new BorderLayout());

final JTextArea area=new JTextArea(30,50);

area.setTabSize(4);

area.addMouseListener(new MouseAdapter()

{

public void mouseReleased(MouseEvent e)

{

// System.out.println(e.isPopupTrigger());

if(e.isPopupTrigger())

{

pop=new MousePopMenu(area);

// pop.setLabel(\"弹出菜单\");

pop.show(area, e.getX(),e.getY());

// System.out.println(pop.getLabel());

}

}

});

JScrollPane jsp=new JScrollPane(area);

JLabel lname=new JLabel(\"类名\");

final JTextField name=new JTextField(10);

JLabel ltype=new JLabel(\"类型\");

final JTextField type=new JTextField(10);

type.addMouseListener(new MouseAdapter()

{

public void mouseEntered(MouseEvent e)

{

type.setFocusable(true);

type.setFocusTraversalKeysEnabled(true);

type.selectAll();

}

});

JLabel lfield=new JLabel(\"类变量\");

final JTextField field=new JTextField(10);

final JTextArea fieldArea=new JTextArea();

fieldArea.setEditable(false);

fieldArea.setLineWrap(true);

fieldArea.setForeground(Color.pink);

JScrollPane fieldjsp=new JScrollPane(fieldArea);

JButton add=new JButton(\"增加类变量\");

JButton create=new JButton(\"生成代码\");

JButton clear=new JButton(\"清除所有\");

clear.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

fieldList.clear();

fieldArea.setText(\"\");

area.setText(\"\");

}

});

add.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

addField(field,type,fieldArea);

}

});

field.addKeyListener(new KeyAdapter()

{

public void keyReleased(KeyEvent e)

{

if(e.getKeyCode()==KeyEvent.VK_ENTER)

{

addField(field,type,fieldArea);

}

}

});

create.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String classname=name.getText().trim();

String classf=\"public class \"+classname+\"\\n{\\n\";

String s=\"\";//所有的私有类变量

String method=\"\";//所有的set和get方法

String constr=\" public \"+classname+\"()\\n {\\n }\\n\";//空的构造器

String constrByarg=\"\";//构造器里面的参数

String methodInConstr=\"\";//构器里面调用的方法

for(int i=0;i{

VarCode var=(VarCode)fieldList.get(i);

String varType=var.getType();

String varField=var.getField();

//类变量

s=s+\" private \"+varType+\" \"+varField+\";\\n\";

constrByarg=constrByarg+varType+\" \"+varField+\

methodInConstr=methodInConstr+\"

this.set\"+VarCode.toFirsttoUpper(varField)+\"(\"+varField+\");\\n\";

//方法

method=method+\" public void

set\"+VarCode.toFirsttoUpper(varField)+\"(\"+varType+\" \"+varField+\")\\n {\\n\"+\" \"+\"

this.\"+varField+\"=\"+varField+\";\\n }\\n\"

+\" public \"+varType+\" get\"+VarCode.toFirsttoUpper(varField)+\"()\\n {\\n\"+\"

\"+\"

return this.\"+varField+\";\\n

}\\n\";

}

constrByarg=constrByarg.substring(0,constrByarg.lastIndexOf(\

constrByarg=\" public \"+classname+\"(\"+constrByarg+\")\\n

+methodInConstr+\" }\\n\";

{\\n\"

String classl=\

area.setForeground(Color.blue);

area.setFont(new Font(\"Courier New\

area.setText(classf+s+constr+constrByarg+method+classl);

}

});

JPanel fieldPanel=new JPanel(new BorderLayout());

fieldPanel.add(fieldjsp,BorderLayout.CENTER);

fieldPanel.add(new JLabel(\"已搞定的类变量\"),BorderLayout.NORTH);

JPanel buttonPanel=new JPanel();

buttonPanel.add(lname);

buttonPanel.add(name);

buttonPanel.add(ltype);

buttonPanel.add(type);

buttonPanel.add(lfield);

buttonPanel.add(field);

buttonPanel.add(add);

buttonPanel.add(create);

buttonPanel.add(clear);

p.add(jsp,BorderLayout.CENTER);

p.add(fieldPanel,BorderLayout.EAST);

p.add(buttonPanel,BorderLayout.SOUTH);

this.setSize(800,650);

this.getContentPane().add(p);

}

public static void main(String ar[])

{

CodeCreate c=new CodeCreate();

c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

c.setVisible(true);

}

private void addField(JTextField field,JTextField type,JTextArea fieldArea)

{

String str_field=field.getText().trim();

String str_type=type.getText().trim();

if(!isEmp(str_field)&& !isEmp(str_type))

{

VarCode var=new VarCode();

var.setField(str_field);

var.setType(str_type);

fieldList.add(var);

fieldArea.append(str_type+\" \"+str_field+\";\\n\");

field.setText(\"\");

}

}

public static boolean isEmp(String str)

{

if(str==null || str.trim().equals(\"\"))

{

return true;

}

else

{

return false;

}

}

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务