package com.cql.user;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ShowFram extends JFrame implements ActionListener
{
private JButton dOK,tOK;
private JLabel jsum;
private JTextField dF,tF,sumF;
private JPanel pu,pd,pr,p11,p12,p13;
private TextArea sql;
private JLabel dN,tN;
private JLabel name ,type , primary,forign;
int sum = 5;
private JTextField tn1,ty1,pr1,fo1;
private JTextField tn2,ty2,pr2,fo2;
private JTextField tn3,ty3,pr3,fo3;
private JTextField tn4,ty4,pr4,fo4;
private JTextField tn5,ty5,pr5,fo5;
private JTextField tn6,ty6,pr6,fo6;
private JTextField tn7,ty7,pr7,fo7;
private JTextField tn8,ty8,pr8,fo8;
private JTextField tn9,ty9,pr9,fo9;
private JTextField[][] arrayT;
public void init()
{
arrayT = new JTextField[9][4];
pu = new JPanel();
pd = new JPanel();
pr = new JPanel();
Font f = new Font("宋体",Font.BOLD,22);
//------------p u-------------------
dN = new JLabel("数据库的名字: ");
tN = new JLabel("表名: ");
pu.setLayout(new GridLayout(3,1));
p11 = new JPanel();
p12 = new JPanel();
p13 = new JPanel();
dF = new JTextField(20);
tF = new JTextField(20);
dOK = new JButton("创建数据库");
tOK = new JButton("创建表");
jsum = new JLabel("输入字段的个数(1-9)");
sumF = new JTextField(3);
dOK.addActionListener(this);
tOK.addActionListener(this);
dN.setFont(f);tN.setFont(f);dF.setFont(f);tF.setFont(f);dOK.setFont(f);tOK.setFont(f);
p11.add(dN); p11.add(dF);p11.add(dOK);
p12.add(tN); p12.add(tF);p12.add(tOK);
p13.add(jsum);p13.add(sumF);
pu.add(p11);
pu.add(p12);
pu.add(p13);
//-------------pd--------------
name = new JLabel("字段名");
type = new JLabel("字段数据类型");
primary = new JLabel("是否是主键PRIMARY(1/0) 1代表是主键");
forign = new JLabel("外键的表名FOREIGN KEY");
tn1 = new JTextField(12);ty1 = new JTextField(12);
pr1 = new JTextField(12);fo1 = new JTextField(12);
tn2 = new JTextField(12);ty2 = new JTextField(12);
pr2 = new JTextField(12);fo2 = new JTextField(12);
tn3 = new JTextField(12);ty3 = new JTextField(12);
pr3 = new JTextField(12);fo3 = new JTextField(12);
tn4 = new JTextField(12);ty4 = new JTextField(12);
pr4 = new JTextField(12);fo4 = new JTextField(12);
tn5 = new JTextField(12);ty5 = new JTextField(12);
pr5 = new JTextField(12);fo5 = new JTextField(12);
tn6 = new JTextField(12);ty6 = new JTextField(12);
pr6 = new JTextField(12);fo6 = new JTextField(12);
tn7 = new JTextField(12);ty7 = new JTextField(12);
pr7 = new JTextField(12);fo7 = new JTextField(12);
tn8 = new JTextField(12);ty8 = new JTextField(12);
pr8 = new JTextField(12);fo8 = new JTextField(12);
tn9 = new JTextField(12);ty9 = new JTextField(12);
pr9 = new JTextField(12);fo9 = new JTextField(12);
pd.setLayout(new GridLayout(10,4));
pd.add(name);pd.add(type);pd.add(primary);pd.add(forign);
pd.add(tn1);pd.add(ty1);pd.add(pr1);pd.add(fo1);
pd.add(tn2);pd.add(ty2);pd.add(pr2);pd.add(fo2);
pd.add(tn3);pd.add(ty3);pd.add(pr3);pd.add(fo3);
pd.add(tn4);pd.add(ty4);pd.add(pr4);pd.add(fo4);
pd.add(tn5);pd.add(ty5);pd.add(pr5);pd.add(fo5);
pd.add(tn6);pd.add(ty6);pd.add(pr6);pd.add(fo6);
pd.add(tn7);pd.add(ty7);pd.add(pr7);pd.add(fo7);
pd.add(tn8);pd.add(ty8);pd.add(pr8);pd.add(fo8);
pd.add(tn9);pd.add(ty9);pd.add(pr9);pd.add(fo9);
//--------------------pr -----------
sql = new TextArea(6,90);
sql.setEditable(true);
sql.setText("");
sql.setFont(new Font("宋体",Font.BOLD,24));
pr.add(sql);
Container c =this.getContentPane();
c.setLayout(new GridLayout(3,1));
c.add(pu);
c.add(pd);
c.add(pr);
this.setBounds(10, 20, 600,600);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
this.pack();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == dOK)
{
checkDataBase();
}
if(e.getSource() == tOK)
{
checkTable();
checkSum();
Tables t = new Tables();
t.setName(tF.getText());
List<Duan> duans= new ArrayList<Duan>();
Duan d[] = new Duan[sum];
arrayT[0][0]=tn1; arrayT[0][1]=ty1; arrayT[0][2]=pr1; arrayT[0][3]=fo1;
arrayT[1][0]=tn2; arrayT[1][1]=ty2; arrayT[1][2]=pr2; arrayT[1][3]=fo2;
arrayT[2][0]=tn3; arrayT[2][1]=ty3; arrayT[2][2]=pr3; arrayT[2][3]=fo3;
arrayT[3][0]=tn4; arrayT[3][1]=ty4; arrayT[3][2]=pr4; arrayT[3][3]=fo4;
arrayT[4][0]=tn5; arrayT[4][1]=ty5; arrayT[4][2]=pr5; arrayT[4][3]=fo5;
arrayT[5][0]=tn6; arrayT[5][1]=ty6; arrayT[5][2]=pr6; arrayT[5][3]=fo6;
arrayT[6][0]=tn7; arrayT[6][1]=ty7; arrayT[6][2]=pr7; arrayT[6][3]=fo7;
arrayT[7][0]=tn8; arrayT[7][1]=ty8; arrayT[7][2]=pr8; arrayT[7][3]=fo8;
arrayT[8][0]=tn9; arrayT[8][1]=ty9; arrayT[8][2]=pr9; arrayT[8][3]=fo9;
for(int i=0;i<sum;i++)
{
d[i] = new Duan();
String name =arrayT[i][0].getText();
String type =arrayT[i][1].getText();
String PirmaryKey =arrayT[i][2].getText();
String ForeignKey =arrayT[i][3].getText();
d[i].setName(name);
d[i].setType(type);
d[i].setPirmaryKey(Integer.parseInt(PirmaryKey));
d[i].setForeignKey(ForeignKey);
}
for(int i=0;i<sum;i++){
duans.add(d[i]);
System.out.println(d[i].getName());
}
t.setDuans(duans);
Iterator i = duans.iterator();
while(i.hasNext())
{
Duan ddd = (Duan)i.next();
}
show(t);
}
}
public void checkTable()
{
String tf = tF.getText();
if(tf == null || tf.equals(""))
{
JOptionPane.showMessageDialog(this, "请输入表名");
}
}
public void show(Tables t)
{
//System.out.print(t.getCreateSQL());
sql.append(t.getCreateSQL());
sql.append("\n");
}
public void checkSum()
{
String str = sumF.getText();
if(str ==null || str.equals(""))
{
JOptionPane.showMessageDialog(this, "请输入字段个数1-9的数");
}else
{
int sums;
try {
sums = Integer.parseInt(str);
if(sums<1 || sums>9)
{
JOptionPane.showMessageDialog(this, "请输入字段个数1-9的数");
}else
{
this.sum = sums;
}
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(this, "请输入字段个数1-9的数");
e1.printStackTrace();
}
}
}
public void checkDataBase()
{
String mysql = dF.getText();
if(mysql == null || mysql.equals(""))
{
JOptionPane.showMessageDialog(this, "请输入数据库的名称");
}else{
DataBases data = new DataBases();
data.setName(mysql);
sql.append(data.getCreateSQL());
}
}
public static void main(String[] args) {
new ShowFram().init();
}
}
- 1
- 2
前往页