试图模仿 Jbuilder拖拽生成代码功能

来源:互联网 发布:c语言计算代数表达式 编辑:程序博客网 时间:2024/06/07 23:59
说实话,写的挺烂的,本来想做好点,半途而废了,懒,

里面最复杂的功能是菜单栏目,点击menu标签,在点 menu更改, 弹出一个框,如图:

里面输入这种格式。

文件(新建,另存&)
编辑
查看

懂了吧,另存& 表示这个子item被默认添加了click监听器,

好了,自己试试吧。

运行,只要在mian中 new GUIBuilder();  只有一个类,无需部署.
就ok 了。

import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.*;import java.util.ArrayList;import java.util.HashMap;@SuppressWarnings("serial")public class GUIBuilder extends JFrame implements ChangeListener{final int NUM=16;private final int ROW=59;//y坐标private final int COL=70;//x坐标//////////////////////////////////////////////////////////////////////////////////////final Sort SORT=new Sort();final CmpSort SORTSTRING=new CmpSort();final Listeners LISTENER=new Listeners();final Relations RELATION=new Relations();final ListenerMethod METHOD =new ListenerMethod();//////////////////////////////////////////////////////////////////////////////////////private MyPanel componentPane=null;    private JPanel centerPane=null;    private JPanel attributePane=null;    private JTabbedPane tab=new JTabbedPane();    MainPane drawPane=new MainPane(this);    JPanel codePane=new JPanel();    JTextArea tea=new JTextArea();///////////////////////////代码写这里    Range range=new Range();    MyPoint [][] point=new MyPoint[COL][ROW];    ////////////////////////////////////////////////////////////////////        ArrayList<MyComponent> cmp=new ArrayList<MyComponent>(); //////////////////组件       HashMap<MyComponent,ItemClass> item=new HashMap<MyComponent,ItemClass>();//组件-------item    ///////////////////    HashMap<String,ItemClass>itemsitem=new HashMap<String,ItemClass>();///子菜单    HashMap<MyComponent,ArrayList<Integer>> listener=    new  HashMap<MyComponent,ArrayList<Integer>>();///////////////组件-----监听器    Image img=null;    boolean dragRange=false;    Point2D rangeLastLocation=new Point2D.Double(300,300);    Rectangle2D put=new Rectangle2D.Double();///放置矩形    boolean setSizeFlag=false;    MyComponent draging=null;    MyComponent dragingSize=null;    ////////////////////    Line2D [] rows=new Line2D.Double[59];    Line2D [] cols=new Line2D.Double[70];    ///////////////////////////////    Rectangle2D [] components=new Rectangle2D.Double[16];    Point2D nearest=new Point2D.Double(40,40);///最近点    int willPut=-1;///将要放置的    boolean dragingCMP=false;    int maxID=1;////最大ID    MyComponent focusCMP=null;/////////////////////当前焦点组件    Color B=new Color(100,200,100);//一种颜色    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    JLabel label_1=null;    JLabel label_2=null;    JLabel label_3=null;    JButton label_4=null;    JLabel label_5=null;    JLabel lbl6=null;        JTextField text_1=null;    JTextField text_2=null;    JComboBox cbo_1=null;    JButton cbo_2=null;    JButton btn2=null;    JButton btn_tab=null;    JButton btn_list=null;    ////////////////////////////////////////////////////////////////////////////////////////////////    boolean addListener(MyComponent e,String str)    {        int num=-1;    boolean flag=false;    for(int i=0;i<this.LISTENER.NAME.length;i++)    {    if(str.equals(LISTENER.NAME[i]))    {    num=i;    flag=true;    break;    }    }    if(flag==false)    {    return false;    }        if(this.listener.get(e)==null)    {    this.listener.put(e, new ArrayList<Integer>());    this.listener.get(e).add(num);    return true;    }    else    {    ArrayList<Integer> tem=this.listener.get(e);    for(int i=0;i<tem.size();i++)    {    if(num==tem.get(i).intValue())    {    return true;    }    }    this.listener.get(e).add(num);    return true;    }    }   class ItemClass    {    String codeString=null;    ArrayList<String> item=null;    ItemClass(String a,ArrayList<String> b)    {    codeString=a;    item=b;    }    }    class ItemFrame extends JFrame implements ActionListener{private static final long serialVersionUID = 122L;JScrollPane js=new JScrollPane();  private JTextArea area=null;  private JButton button_1=null;  public ItemFrame(){};  public ItemFrame(ArrayList<String> args)  {    super("ADDTION");    this.setDefaultCloseOperation(0);    setBounds(200,200,400,330);    JPanel jp=new JPanel();    jp.setLayout(null);    init();    area.setText("");    if(args!=null)    {    String tem="";    int len=args.size();    for(int i=0;i<len;i++)    {    tem+=args.get(i)+"\n";    }    area.setText(tem);    }        js.getViewport().add(area);    jp.add(js);    button_1.addActionListener(this);    jp.add(button_1);    setContentPane(jp);    setVisible(true);  }  private void init()  {area=new JTextArea();    js.setBounds(52,10,274,248);    button_1=new JButton("确定");    button_1.setBounds(144,261,94,28);  }public void actionPerformed(ActionEvent e) {String tem=area.getText().toString();int count=0;int n=0;ArrayList<String> arr=new ArrayList<String>();for(int i=0,len=tem.length();i<len;i++){if(tem.charAt(i)=='\n'){arr.add(tem.substring(n,i));n=i+1;count++;}}String codes="";String cboName=focusCMP.name;for(int i=0;i<count;i++){codes+="\t\t"+cboName+".addItem(\""+arr.get(i)+"\");\n";}if(item.get(focusCMP)==null){item.put(focusCMP,new ItemClass(codes,arr) );if(focusCMP.sort==SORT.JMENUBAR){for(int k=0;k<arr.size();k++){itemsitem.put(arr.get(k), compile(arr.get(k)));}}}else{item.get(focusCMP).codeString=codes;item.get(focusCMP).item=arr;if(focusCMP.sort==SORT.JMENUBAR){for(int k=0;k<arr.size();k++){itemsitem.put(arr.get(k), compile(arr.get(k)));}}}System.out.println(codes);this.dispose();}}        private ItemClass compile(String str)    {    if(str.charAt(str.length()-1)!=')')    return null;    else    {    String str1=null;    String str2=null;    ArrayList<String> list=new ArrayList<String>();    for(int i=0;i<str.length();i++)    {    if(str.charAt(i)=='(')    {    str1=str.substring(0,i);    str2=str.substring(i+1,str.length()-1);    }    }    int start=0;    int end=0;    for(int i=0;i<str2.length();i++)    {    if(str2.charAt(i)==',')    {    end=i;    list.add(str2.substring(start,end));    start=i+1;    }    }    if(str2.substring(start).length()>0)    {    list.add(str2.substring(start));    }    ItemClass itm=new ItemClass(str1,list);    return itm;    }    }    ///////////////////////////////////////////////////////////////////////////////////////////////    void drawLine(Graphics2D g)    {    g.setColor(new Color(200,200,210));    for(int i=0;i<COL;i++)    {    g.draw(cols[i]);    }    for(int i=0;i<ROW;i++)    {    g.draw(rows[i]);    }    }    void removeCMP(MyComponent e)    {    for(int i=0;i<cmp.size();i++)    {    if(cmp.get(i)==this.focusCMP)    {    focusCMP=null;    cmp.remove(i);    break;    }    }    this.item.remove(e);/////////////////////////////////////    this.listener.remove(e);///////////////////////////    }    void initComponents()    {    for(int i=0;i<16;i++)    {    components[i]=new Rectangle2D.Double(25,30+30*i,60,25);    }    }        void drawComponents(Graphics2D g)    {    g.setColor(Color.blue);    for(int i=0;i<16;i++)    {    if(i==willPut)    {    g.setColor(Color.red);    g.fill(components[i]);    g.setColor(Color.blue);    }    else    {    g.fill(components[i]);    }    }    g.setColor(Color.white);        for(int i=0;i<16;i++)    {    g.drawString(SORTSTRING.STRINGS[i], 27, 47+30*i);    }    }        boolean  getNearestPoint()    {    for(int x=0;x<COL;x++)    {    for(int y=0;y<ROW;y++)        {            if(put.contains(point[x][y].point))        {        //System.out.println(y);        nearest=point[x][y].point;        return true;        }        }    }    return false;    }    void drawViewComponent(Graphics2D g)    {    for(int i=0;i<cmp.size();i++)    {    if(cmp.get(i)==focusCMP)    {    g.setColor(Color.orange);    g.fill(cmp.get(i).border);    g.setColor(B);    }    else    {    g.fill(cmp.get(i).border);    }    }    g.setColor(Color.white);    for(int i=0;i<cmp.size();i++)    {    Rectangle2D tem=cmp.get(i).border;    g.drawString(cmp.get(i).text,(int)tem.getX(), (int)(tem.getY()+tem.getHeight()/2+5));    }    }    boolean setCmpName(MyComponent me,String name)    {    if(me==null)    {    return false;    }    for(int i=0;i<cmp.size();i++)    {    if(me!=cmp.get(i))    {    if(name.equals(cmp.get(i).name))    {    return false;    }    }    }    me.name=name;    return true;    }    void setText(MyComponent me,String t)    {    if(me!=null)    me.text=t;    }    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    public GUIBuilder()///////////////////////////////////////////////////////////////////////////////////    {        this.setTitle("GUI图形用户界面生成器");                for(int x=0;x<COL;x++)        {                for(int y=0;y<ROW;y++)        {        point[x][y]=new MyPoint(new Point2D.Double(x*10,y*10));        }        }        for(int i=0;i<COL;i++)        {        cols[i]=new Line2D.Double(i*10,0,i*10,580);        }                for(int i=0;i<ROW;i++)        {        rows[i]=new Line2D.Double(0,i*10,690,i*10);        }              initComponents();        this.setDefaultCloseOperation(3);        setBounds(10,50,1000,645);        JPanel jp=new JPanel();        jp.setLayout(null);        init();        jp.add(componentPane);        jp.add(centerPane);        //////////////////////////////////////////////        attributePane.setLayout(null);        label_1=new JLabel("");        label_1.setBounds(4,5,173,37);        label_1.setFont(new Font("黑体",Font.BOLD,16));        label_2=new JLabel("Name");        label_2.setBounds(4,48,55,20);        label_3=new JLabel("Text");        label_3.setBounds(4,76,55,20);        label_4=new JButton("add");//////////////////////////////////////////////////////////////按钮        label_4.addActionListener(new ActionListener()        {public void actionPerformed(ActionEvent e) {//System.out.println(cbo_1.getSelectedItem().toString()+"******");int count=cbo_1.getItemCount();if(count>0){if(focusCMP!=null){addListener(focusCMP,cbo_1.getSelectedItem().toString());if(listener.get(focusCMP)!=null)System.out.println(listener.get(focusCMP).toString());else{System.out.println("null");}}tab.setSelectedIndex(1);}}        });        label_4.setBounds(0,103,55,21);        label_5=new JLabel("Items");        label_5.setBounds(4,130,55,20);        text_1=new JTextField();        text_1.setBounds(55,46,115,24);        text_2=new JTextField();        text_2.setBounds(55,75,114,23);        cbo_1=new JComboBox();        cbo_1.setBounds(56,102,115,23);        cbo_2=new JButton(" 添 加 ");        cbo_2.setBounds(55,129,115,23);        this.lbl6=new JLabel("Menu");        lbl6.setBounds(4, 157, 55, 21);        this.btn2=new JButton(" 更 改 ");        btn2.setBounds(55,157,115,22);        btn2.addActionListener(new ActionListener()        {public void actionPerformed(ActionEvent e) {if(focusCMP!=null){if(focusCMP.sort==SORT.JMENUBAR){ItemClass tem=null;if(item.get(focusCMP)==null){new ItemFrame(null);}else{tem=item.get(focusCMP);new ItemFrame(tem.item);}}}}        });        FocusListener l=new FocusHandler();        text_1.addFocusListener(l);        text_2.addFocusListener(l);        cbo_2.addActionListener(new ActionListener()        {public void actionPerformed(ActionEvent e) {if(focusCMP!=null){if(focusCMP.sort==SORT.COMBOBOX){ItemClass tem=null;if(item.get(focusCMP)==null){new ItemFrame(null);}else{tem=item.get(focusCMP);new ItemFrame(tem.item);}}}}                });        JLabel lbl_tab =new JLabel("选项卡");        lbl_tab.setBounds(4, 185, 55, 21);        btn_tab=new JButton("添加卡片");        btn_tab.setBounds(55,185,115,22);        btn_tab.addActionListener(new ActionListener()        {public void actionPerformed(ActionEvent e) {if(focusCMP!=null){if(focusCMP.sort==SORT.TABBEDPANE){ItemClass tem=null;if(item.get(focusCMP)==null){new ItemFrame(null);}else{tem=item.get(focusCMP);new ItemFrame(tem.item);}}}}});        JLabel lbl_list=new JLabel("列表");        lbl_list.setBounds(4, 213, 55, 21);        btn_list=new JButton("添加项");        btn_list.setBounds(55,213,115,22);        btn_list.addActionListener(new ActionListener()        {public void actionPerformed(ActionEvent e) {if(focusCMP!=null){if(focusCMP.sort==SORT.LIST){ItemClass tem=null;if(item.get(focusCMP)==null){new ItemFrame(null);}else{tem=item.get(focusCMP);new ItemFrame(tem.item);}}}}});        attributePane.add(lbl_list);        attributePane.add(btn_list);        attributePane.add(btn_tab);        attributePane.add(lbl_tab);        attributePane.add(label_1);        attributePane.add(label_2);        attributePane.add(label_3);        attributePane.add(label_4);        attributePane.add(label_5);        attributePane.add(text_1);        attributePane.add(text_2);        attributePane.add(cbo_1);        attributePane.add(cbo_2);        attributePane.add(this.lbl6);        attributePane.add(this.btn2);        ////////////////////////////////////////////////        jp.add(attributePane);        setContentPane(jp);             setVisible(true);        this.setResizable(false);        /////////////////////////////////////////////////////////////////////////////////////////////////    }    private void init()    {    componentPane=new MyPanel(this);    componentPane.setBounds(0,0,113,615);    centerPane=new JPanel();    centerPane.setBounds(115,0,700,615);    centerPane.setLayout(new BorderLayout());        tab.add("视图", drawPane);        JScrollPane js=new JScrollPane();        js.getViewport().add(tea);        tea.setFont(new Font("宋体",Font.BOLD,16));        tea.setTabSize(3);        tea.setForeground(Color.blue);        codePane.setLayout(new BorderLayout());        codePane.add(js);        tab.add("代码",codePane);        tab.addChangeListener(this);    centerPane.add(tab);    //codePane.setBackground(Color.pink);            attributePane=new JPanel();        attributePane.setBounds(817,0,171,615);    }        public void cross(){this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));}public void changesize(){this.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));}public void deletecross(){this.setCursor(Cursor.getDefaultCursor());}boolean findRange(Point2D e){if(range.dragArea.contains(e)){return true;}return false;}MyComponent findComponent(Point2D e){for(int i=0;i<cmp.size();i++){if(cmp.get(i).dragSize.contains(e)){setSizeFlag=true;return cmp.get(i);}if(cmp.get(i).border.contains(e)){setSizeFlag=false;return cmp.get(i);}}return null;}boolean setBorderLocation(){for(int x=0;x<COL;x++){for(int y=0;y<ROW;y++){if(range.dragArea.contains(point[x][y].point)){range.setRange(point[x][y].point);rangeLastLocation=point[x][y].point;repaint();//System.out.println(x +"   "+y);return true;}}}range.setRange(rangeLastLocation);repaint();return false;}MyComponent findComponentById(int id){for(int i=0;i<cmp.size();i++){if(cmp.get(i).id==id){return cmp.get(i);}}return null;}String getName(MyComponent myComponent){int count=1;for(int i=0;i<cmp.size();i++){if(cmp.get(i).sort==myComponent.sort){if(count<=cmp.get(i).sortID){count=cmp.get(i).sortID+1;}}}myComponent.sortID=count;return SORT.NAME[myComponent.sort]+count;}class FocusHandler implements FocusListener{public void focusGained(FocusEvent e){}public void focusLost(FocusEvent e) {setCmpName(focusCMP,text_1.getText());setText(focusCMP,text_2.getText());}}String createCode(){String code1="import javax.swing.event.*;\nimport javax.swing.*;\n"+"import java.awt.*;\n"+"import java.awt.event.*;\n\n"+"class MyFrame extends JFrame\n"+"{\n";///////////////差后扩号String code2="";String code20="";///////////////////////////////////////  addListenerString code21="";////////////////////////String code200="";///////////////////////////////        addItemString code3="";////////////////////////////////   jp.add()String code4="\tprivate void init()\n\t{\n";/////////////////////////////////  init()for(int i=0;i<cmp.size();i++){code2+="\tprivate "+SORT.CMP[cmp.get(i).sort]+" "+cmp.get(i).name+" =null;\n";if(this.listener.get(cmp.get(i))!=null){ArrayList<Integer> lis=this.listener.get(cmp.get(i));for(int j=0;j<lis.size();j++){code20+="\t\t"+cmp.get(i).name+".add"+this.LISTENER.NAME[lis.get(j)]+"(new "+cmp.get(i).name+"_"+this.LISTENER.NAME[lis.get(j)]+"());\n";code21+="\tclass "+cmp.get(i).name+"_"+this.LISTENER.NAME[lis.get(j)]+METHOD.TEXT[lis.get(j)];}}if(this.item.get(cmp.get(i))!=null){ItemClass te1=this.item.get(cmp.get(i));ArrayList<String> tem=te1.item;for(int j=0;j<tem.size();j++){if(cmp.get(i).sort==SORT.COMBOBOX){code200+="\t\t"+cmp.get(i).name+".addItem(\""+tem.get(j)+"\");\n";}else if(cmp.get(i).sort==SORT.JMENUBAR){code200+="\t\tJMenu "+cmp.get(i).name+"_item"+(j+1)+" =new JMenu(\""+tem.get(j)+"\");\n" ;code200+="\t\t"+cmp.get(i).name+".add("+cmp.get(i).name+"_item"+(j+1)+");\n";if(itemsitem.get(tem.get(j))!=null){ItemClass mui=itemsitem.get(tem.get(j));code200+="\t\t"+cmp.get(i).name+"_item"+(j+1)+".setText(\""+mui.codeString+"\");\n";for(int k=0;k<mui.item.size();k++){String temStr=mui.item.get(k);boolean hasListener=false;if(temStr.charAt(temStr.length()-1)=='&'){temStr=temStr.substring(0,temStr.length()-1);hasListener=true;}code200+="\t\tJMenuItem "+cmp.get(i).name+"_item"+(j+1)+"_it"+(k+1)+" =new JMenuItem(\""+temStr+"\");\n";if(hasListener)code200+="\t\t"+cmp.get(i).name+"_item"+(j+1)+"_it"+(k+1)+".addActionListener(new ActionListener()\n\t\t{\n"+"\t\t\t"+"public void actionPerformed(ActionEvent e)\n\t\t\t{\n\t\t\t"+"\t\t\t\tSystem.out.println(98765);\n\t\t\t"+"}\n\t\t});\n";code200+="\t\t"+cmp.get(i).name+"_item"+(j+1)+".add("+cmp.get(i).name+"_item"+(j+1)+"_it"+(k+1)+");\n";}}}else if(cmp.get(i).sort==SORT.TABBEDPANE){code200+="\t\t"+cmp.get(i).name+".add(\""+tem.get(j)+"\",new JPanel());\n";}}}code3+="\t\tjp.add("+cmp.get(i).name+");\n";Rectangle2D tem2=cmp.get(i).border;int x=(int)tem2.getX();int y=(int)tem2.getY();int w=(int)tem2.getWidth();int h=(int )tem2.getHeight();String temStr="";if(cmp.get(i).sort<=5){temStr="\""+cmp.get(i).text+"\"";}if(cmp.get(i).sort==SORT.LIST){ArrayList<String> list001=this.item.get(cmp.get(i)).item;temStr="new String[]{";for(int k=0;k<list001.size()-1;k++){temStr+="\""+list001.get(k)+"\",";}temStr+="\""+list001.get(list001.size()-1)+"\"}";}code4+="\t\t"+cmp.get(i).name+"=new "+SORT.CMP[cmp.get(i).sort]+"("+temStr+");\n";code4+="\t\t"+cmp.get(i).name+".setBounds("+x+","+y+","+w+","+h+");\n";}code4+="\t}\n";Rectangle2D tem1=this.range.border;int w=(int)tem1.getWidth();int h=(int)tem1.getHeight();String code50="\tpublic MyFrame()\n"+"\t{" +"\n\t\tsuper(\"MyFrame\");\n"+"\t\tthis.setDefaultCloseOperation(3);\n"+"\t\tsetBounds(200,150,"+w+","+(h+20)+");\n"+"\t\tJPanel jp=new JPanel();\n"+"\t\tjp.setLayout(null);\n"+"\t\tinit();\n";//////////////////差后扩号String codingString=code1+code2+code50+code20+code200+code3+"\t\tsetContentPane(jp);\n"+        "\t\tsetVisible(true);\n\t}\n"+code4+code21+"}";return codingString;}public void stateChanged(ChangeEvent e){if(this.tab.getSelectedIndex()==1){this.tea.setText(this.createCode());this.text_1.setEditable(false);this.text_2.setEditable(false);this.cbo_1.setEnabled(false);this.label_4.setEnabled(false);this.btn2.setEnabled(false);this.cbo_2.setEnabled(false);this.btn_tab.setEnabled(false);this.btn_list.setEnabled(false);}else{this.text_1.setEditable(true);this.text_2.setEditable(true);this.cbo_1.setEnabled(true);this.label_4.setEnabled(true);this.btn2.setEnabled(true);this.cbo_2.setEnabled(true);this.btn_tab.setEnabled(true);this.btn_list.setEnabled(true);}}}class MyPanel extends JPanel implements MouseListener,MouseMotionListener{private static final long serialVersionUID = 100L;Graphics2D g=null;GUIBuilder outer=null;MyPanel(GUIBuilder e){outer=e;this.addMouseListener(this);this.addMouseMotionListener(this);}int find(Point2D p){for(int i=0;i<outer.NUM;i++){if(outer.components[i].contains(p)){return i;}}return -1;}public void paintComponent(Graphics e){super.paintComponent(e);g=(Graphics2D)e;outer.drawComponents(g);}public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e){outer.willPut=find(e.getPoint());outer.focusCMP=null;//System.out.println(outer.willPut);outer.label_1.setText("");outer.text_1.setText("");outer.text_2.setText("");outer.cbo_1.removeAllItems();outer.repaint();}public void mouseReleased(MouseEvent e) {}public void mouseDragged(MouseEvent e) {}public void mouseMoved(MouseEvent e) {if(find(e.getPoint())!=-1){outer.cross();}else{outer.deletecross();}}}class MainPane extends JPanel implements MouseListener,MouseMotionListener{private static final long serialVersionUID = 199L;Graphics2D g=null;GUIBuilder outer=null;Color b=new Color(100,200,100);MainPane(GUIBuilder e){outer=e;this.addMouseListener(this);this.addMouseMotionListener(this);this.setBackground(Color.white);}public void paintComponent(Graphics e){super.paintComponent(e);g=(Graphics2D)e;outer.range.draw(g);outer.drawLine(g);g.setColor(b);outer.drawViewComponent(g);}public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e) {outer.label_1.requestFocus();///////////////////////////////////////////////////int count =e.getClickCount();if(outer.willPut==-1){if(outer.findRange(e.getPoint())){outer.dragRange=true;}else {if((outer.dragingSize=outer.findComponent(e.getPoint()))!=null){if(count<2){if(outer.setSizeFlag==true){outer.focusCMP=outer.dragingSize;}else{outer.focusCMP=outer.dragingSize;outer.draging=outer.dragingSize;outer.dragingSize=null;}outer.label_1.setText("   "+outer.SORT.CMP[outer.focusCMP.sort]);outer.text_1.setText(outer.focusCMP.name);outer.text_2.setText(outer.focusCMP.text);int st=outer.focusCMP.sort;ArrayList<Integer> temlist=outer.RELATION.CMP[st];outer.cbo_1.removeAllItems();for(int i=0;i<temlist.size();i++){outer.cbo_1.addItem(outer.LISTENER.NAME[temlist.get(i)]);}}else{System.out.println(outer.focusCMP.id);//System.out.println(outer.itemsitem.size());if(outer.focusCMP.sort==outer.SORT.JMENUBAR){GUIBuilder.ItemClass itm=outer.item.get(outer.focusCMP);if(itm!=null){ArrayList<String>temList=itm.item;for(int k=0;k<temList.size();k++){if(outer.itemsitem.get(temList.get(k))!=null)outer.itemsitem.remove(temList.get(k));}}}outer.removeCMP(outer.focusCMP);outer.label_1.setText("");outer.text_1.setText("");outer.text_2.setText("");outer.cbo_1.removeAllItems();outer.repaint();//System.out.println(outer.itemsitem.size());}}else{}}}else{Point2D p=e.getPoint();outer.put.setFrame(p.getX()-5, p.getY()-5, 10, 10);outer.getNearestPoint();outer.cmp.add((outer.focusCMP=new MyComponent(outer.nearest,outer.willPut,outer.maxID,outer)));outer.maxID++;outer.willPut=-1;outer.label_1.setText("   "+outer.SORT.CMP[outer.focusCMP.sort]);outer.text_1.setText(outer.focusCMP.name);outer.text_2.setText(outer.focusCMP.text);int st=outer.focusCMP.sort;ArrayList<Integer> temlist=outer.RELATION.CMP[st];outer.cbo_1.removeAllItems();for(int i=0;i<temlist.size();i++){outer.cbo_1.addItem(outer.LISTENER.NAME[temlist.get(i)]);}outer.repaint();}}public void mouseReleased(MouseEvent e){if(outer.dragRange==true){outer.setBorderLocation();}else{if(outer.draging!=null){Point2D p=e.getPoint();outer.put.setFrame(p.getX()-5, p.getY()-5, 10, 10);outer.getNearestPoint();if(outer.dragingCMP==true){outer.draging.setLocation(outer.nearest);}}else if(outer.dragingSize!=null){Point2D p=e.getPoint();outer.put.setFrame(p.getX()-5, p.getY()-5, 10, 10);outer.getNearestPoint();outer.dragingSize.setSize(outer.nearest);}}outer.draging=null;outer.dragingSize=null;outer.dragRange=false;outer.dragingCMP=false;repaint();}public void mouseDragged(MouseEvent e) {//outer.put.setFrame(e.getPoint().getX()-5, e.getPoint().getY()-5, 10, 10);if(outer.dragRange==true){outer.range.setRange(e.getPoint());outer.repaint();}else{if(outer.draging!=null){outer.draging.setLocation(e.getPoint());outer.dragingCMP=true;outer.repaint();}else if(outer.dragingSize!=null){outer.dragingSize.setSize(e.getPoint());outer.dragingCMP=true;outer.repaint();}}}public void mouseMoved(MouseEvent e) {//outer.put.setFrame(e.getPoint().getX()-5, e.getPoint().getY()-5, 10, 10);if(outer.findRange(e.getPoint())){outer.changesize();}else if(outer.findComponent(e.getPoint())!=null){if(outer.setSizeFlag==true){outer.changesize();}else{outer.cross();}}else{outer.deletecross();}}}class Range{Rectangle2D border=new Rectangle2D.Double(0,0,300,300);Color cor=new Color(150,150,150);Rectangle2D dragArea=new Rectangle2D.Double(290,290,10,10);public void setRange(Point2D p){double x=p.getX();double y=p.getY();if(x>100&&y>100){border.setFrame(0,0,x,y);dragArea.setFrame(x-10, y-10, 10, 10);}else{border.setFrame(0,0,100,100);dragArea.setFrame(90, 90, 10, 10);}}public void draw(Graphics2D g){g.setColor(cor);g.fill(this.border);}}class MyPoint{Point2D point=null;Color cor=Color.orange;Ellipse2D circle=new Ellipse2D.Double();MyPoint(Point2D p ){point=p;double x=p.getX();double y=p.getY();circle.setFrameFromCenter(x, y, x+2, y+2);}void draw(Graphics2D g){g.setColor(Color.pink);g.draw(circle);}}class MyComponent{int id=0;int sortID=1;String name=null;String text=null;int sort=-1;Rectangle2D border=null;Point2D point=null;Rectangle2D dragSize=null;GUIBuilder outer=null;MyComponent(Point2D p,int theSort,int ID,GUIBuilder out){point=p;sort=theSort;id=ID;outer=out;border=new Rectangle2D.Double(p.getX()-20,p.getY()-10,40,20);dragSize=new Rectangle2D.Double(border.getMaxX()-10,border.getMaxY()-10,10,10);setName();setText(name);}void setName(){name=outer.getName(this);}void setText(String t){text=t;}void setLocation(Point2D p){double x=p.getX();double y=p.getY();double w=border.getWidth();double h=border.getHeight();double t1=w/2;double t2=h/2;if(t1%10==5){t1+=5;}if(t2%10==5){t2+=5;}border.setFrame(x-t1, y-t2, w, h);dragSize=new Rectangle2D.Double(border.getMaxX()-10,border.getMaxY()-10,10,10);}void setSize(Point2D p){double x=border.getX();double y=border.getY();if(p.getX()-x>10&&p.getY()-y>10){border.setFrame(x, y, p.getX()-x, p.getY()-y);dragSize=new Rectangle2D.Double(border.getMaxX()-10,border.getMaxY()-10,10,10);}else{border.setFrame(x, y, 10, 10);dragSize=new Rectangle2D.Double(x,y,10,10);}}}class Sort{final int LABEL=0;//标签final int TEXT=1;//文本框final int TEXTAREA=2;//区域final int BUTTON=3;//按钮final int RADIO=4;//单选final int CHECKBOX=5;//复选final int COMBOBOX=6;//下拉final int SCROLLPANE=7;//JScrollPanefinal int PANEL=8;//JPanelfinal int PASSWORD=9;//JPasswordFieldfinal int LIST=10;//JListfinal int SPINNER=11;//JSpinnerfinal int SLIDER=12;//JSliderfinal int PROGRESS=13;//JProgressBarfinal int TABBEDPANE=14;//JTabbedPanefinal int JMENUBAR=15;///////////////////////////////////////////////////////////////final String[] NAME=new String[16];////名字final String[] CMP=new String[16];/////组件名字Sort(){NAME[0]="lb";NAME[1]="tx";NAME[2]="ta";NAME[3]="bt";NAME[4]="rad";NAME[5]="chx";NAME[6]="cbo";NAME[7]="scr";NAME[8]="pan";NAME[9]="pass";NAME[10]="list";NAME[11]="spd";NAME[12]="sld";NAME[13]="pro";NAME[14]="tab";NAME[15]="menu";//////////////////////////////////////////////////////////CMP[0]="JLabel";CMP[1]="JTextField";CMP[2]="JTextArea";CMP[3]="JButton";CMP[4]="JRadioButton";CMP[5]="JCheckBox";CMP[6]="JComboBox";/////////////////////////下拉列表CMP[7]="JScrollPane";CMP[8]="JPanel";CMP[9]="JPasswordField";CMP[10]="JList";CMP[11]="JSpinner";CMP[12]="JSlider";CMP[13]="JProgressBar";CMP[14]="JTabbedPane";CMP[15]="JMenuBar";}}class CmpSort{final String[] STRINGS=new String[16];////////////////////////////final String LABEL="标签";final String TEXT="文本框";final String TEXTAREA="TextArea";final String BUTTON="按钮";final String RADIO="单选按钮";final String CHECKBOX="复选框";final String COMBOBOX=    "组合框";final String SCROLLPANE="ScrollP";final String PANEL="Panel";final String PASSWORD="密码框";final String LIST="ListBox";final String SPINNER="微调按钮";final String SLIDER="滑块";final String PROGRESS="进度条";final String TABBEDPANE="选项卡";final String JMENUBAR="菜单栏";CmpSort(){STRINGS[0]=LABEL;STRINGS[1]=TEXT;STRINGS[2]=TEXTAREA;STRINGS[3]=BUTTON;STRINGS[4]=RADIO;STRINGS[5]=CHECKBOX;STRINGS[6]=COMBOBOX;STRINGS[7]=SCROLLPANE;STRINGS[8]=PANEL;STRINGS[9]=PASSWORD;STRINGS[10]=LIST;STRINGS[11]=SPINNER;STRINGS[12]=SLIDER;STRINGS[13]=PROGRESS;STRINGS[14]=TABBEDPANE;STRINGS[15]=JMENUBAR;}}class Listeners{final String[] NAME=new String[7];Listeners(){NAME[0]="ActionListener";NAME[1]="FocusListener";NAME[2]="ChangeListener";NAME[3]="MouseListener";NAME[4]="KeyListener";NAME[5]="ListSelectionListener";NAME[6]="ItemListener";}final int ACT=0;final int FOC=1;final int CHA=2;final int MOU=3;final int KEY=4;final int LIS=5;final int ITM=6;}class ListenerMethod{final String [] TEXT=new String[7];final String HEAD="\tclass ";ListenerMethod(){TEXT[0]= " implements ActionListener\n"+    "\t{\n"+"\t\tpublic void actionPerformed(ActionEvent e)\n"+"\t\t{\n\t\t\tSystem.out.println(111111);\n"+"\t\t}\n"+    "\t}\n";TEXT[1]=  " implements FocusListener\n"+"\t{\n"+"\t\tpublic void focusGained(FocusEvent e)\n"+ "\t\t{\n"+"\t\t}\n"+"\t\tpublic void focusLost(FocusEvent e)\n"+"\t\t{\n"+"\t\t}\n"+"\t}\n";TEXT[2]=" implements ChangeListener\n"+"\t{\n"+"\t\tpublic void stateChanged(ChangeEvent e)\n"+ "\t\t{\n"+"\t\t}\n"+    "\t}\n";TEXT[3]=" implements MouseListener\n"+"\t{\n"+"\t\tpublic void mouseClicked(MouseEvent e)\n"+ "\t\t{\n"+"\t\t}\n"+"\t\tpublic void mouseEntered(MouseEvent e)\n"+"\t\t{\n"+"\t\t}\n"+"\t\tpublic void mouseExited(MouseEvent e)\n"+ "\t\t{\n"+"\t\t}\n"+"\t\tpublic void mousePressed(MouseEvent e)\n"+ "\t\t{\n"+"\t\t}\n"+"\t\tpublic void mouseReleased(MouseEvent e)\n"+ "\t\t{\n"+"\t\t}\n"+"\t}\n";TEXT[4]=" implements KeyListener\n"+    "\t{\n"+"\t\tpublic void keyPressed(KeyEvent e)\n"+ "\t\t{\n"+"\t\t}\n"+"\t\tpublic void keyReleased(KeyEvent e)\n"+"\t\t{\n"+"\t\t}\n"+"\t\tpublic void keyTyped(KeyEvent e)\n"+ "\t\t{\n"+"\t\t}\n"+    "\t}\n";TEXT[5]=" implements ListSelectionListener\n"+    "\t{\n"+"\t\tpublic void valueChanged(ListSelectionEvent e)\n"+ "\t\t{\n"+"\t\t}\n"+    "\t}\n";TEXT[6]= " implements ItemListener\n"+    "\t{\n"+"\t\tpublic void itemStateChanged(ItemEvent e)\n"+"\t\t{\n\t\t\tSystem.out.println(111111);\n"+"\t\t}\n"+    "\t}\n";}}@SuppressWarnings("unchecked")class Relations{ArrayList<Integer> [] CMP=new ArrayList[16];Relations(){CMP[0]=new ArrayList<Integer>();(CMP[1]=new ArrayList<Integer>()).add(1);(CMP[2]=new ArrayList<Integer>()).add(1);(CMP[3]=new ArrayList<Integer>()).add(0);CMP[4]=new ArrayList<Integer>();CMP[4].add(0);CMP[4].add(1);CMP[4].add(2);CMP[4].add(3);CMP[4].add(4);CMP[5]=new ArrayList<Integer>();CMP[5].add(0);CMP[5].add(1);CMP[5].add(2);CMP[5].add(3);CMP[5].add(4);CMP[6]=new ArrayList<Integer>();CMP[6].add(6);CMP[7]=new ArrayList<Integer>();CMP[8]=new ArrayList<Integer>();CMP[8].add(3);CMP[8].add(4);CMP[9]=new ArrayList<Integer>();CMP[9].add(1);CMP[10]=new ArrayList<Integer>();CMP[10].add(5);CMP[11]=new ArrayList<Integer>();CMP[11].add(2);CMP[12]=new ArrayList<Integer>();CMP[12].add(2);CMP[13]=new ArrayList<Integer>();CMP[13].add(2);CMP[14]=new ArrayList<Integer>();//"JTabbedPane";CMP[14].add(2);CMP[15]=new ArrayList<Integer>();//"JMenuBar";CMP[15]=new ArrayList<Integer>();}}

点击左边的按钮,变成红色后,点右边的画布 ,就放上去了。
所有组建,右下角可以拖拽大小。。。

生成的代码, 类叫MyFrame,只要建立一个MYFrame.java,代码放进去,
直接在mian 中new MyFrame(); 运行生成的代码。

这玩意儿怎么用的话,运行了,乱点一气,就知道了。。


原创粉丝点击