📄 guibuilder.java
字号:
package pro;
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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -