-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_Frame.java
More file actions
201 lines (180 loc) · 5.75 KB
/
Copy pathMain_Frame.java
File metadata and controls
201 lines (180 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package Win_SQL;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main_Frame extends JFrame{
private JFrame insert_frame;
private JTextArea textarea;
private int default_width=600;
private int default_height=400;
private JDialog init_frame;
public Main_Frame(){
textarea=new JTextArea(10,20);
textarea.setEditable(false);
JScrollPane pane=new JScrollPane(textarea);
add(pane,BorderLayout.CENTER);
setTitle("VisualSQL");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setshow(Main_Frame.this,default_width,default_height);
JMenuBar bar=new JMenuBar();
JMenu start_menu=new JMenu("开始");
bar.add(start_menu);
JMenu operate=new JMenu("操作");
JMenuItem add=new JMenuItem("增加");
add.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(insert_frame==null) {
insert_frame=new MyFrame();
insert_frame.setLocation(200,300);
insert_frame.setVisible(true);
}else {
}
}
});
JMenuItem delete=new JMenuItem("删除");
JMenuItem modify=new JMenuItem("修改");
JMenuItem query=new JMenuItem("查询");
query.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// Statement statement=Service.createStatement();
// TODO Auto-generated method stub
// Connection con=null;
// try {
// con=MyFrame.getConnection();
// String sql=JOptionPane.showInputDialog(Main_Frame.this, "输入查询语句");
// Statement state=con.createStatement();
// if (sql!="") {
// ResultSet set=state.executeQuery(sql);
// while (set.next()){
// Model hero=new Model();
// hero.setId(set.getInt("id"));
// hero.setName(set.getString("name"));
// hero.setAttack(set.getInt("gongji"));
// hero.setDefend(set.getInt("fangyu"));
// hero.setSex(set.getString("sex"));
// hero.setCountry(set.getString("country"));
// textarea.setText(hero.toString()+"\n"+textarea.getText());
// }}
// textarea.setText("");
// } catch (SQLException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }finally{
// try {
// con.close();
// } catch (SQLException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
// }
}
}
);
JMenuItem quit=new JMenuItem("退出");
JMenu connect=new JMenu("连接");
JMenuItem local=new JMenuItem("本地");
//local菜单项添加监听器
local.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String user=JOptionPane.showInputDialog(Main_Frame.this,"请输入用户名");
String passwd=JOptionPane.showInputDialog(Main_Frame.this,"请输入密码");
String db_name=JOptionPane.showInputDialog(Main_Frame.this,"请输入数据库名");
System.out.println(user+" "+passwd+" "+db_name);
File file=new File("./config.properties");
FileOutputStream fos=null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
OutputStreamWriter osw=null;
try {
osw=new OutputStreamWriter(fos,"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
osw.write("username:"+user+"\n");
osw.write("passwd:"+passwd+"\n");
osw.write("db_name:"+db_name);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
osw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
JMenuItem remote=new JMenuItem("远程");
connect.add(local);
connect.add(remote);
start_menu.add(connect);
//添加退出的监听器
quit.addActionListener(new ActionListener(
){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}});
//"操作"菜单项添加增删改查功能的子菜单
operate.add(add);
operate.add(delete);
operate.add(modify);
operate.add(query);
insert_frame=new MyFrame();
insert_frame.setTitle("插入操作");
insert_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bar.add(operate);
start_menu.addSeparator();
start_menu.add(quit);
//添加菜单栏
add(bar,BorderLayout.NORTH);
setSize(default_width,default_height);
if(init_frame==null){
init_frame=new InitDialog(Main_Frame.this);
init_frame.setVisible(true);}
}
//设置窗体的显示位置
private void setshow(JFrame frame,int w_x,int w_y){
Dimension size=main.getScreenSize();
//屏幕的宽和高
int loc_x=(int) size.getWidth();
int loc_y=(int) size.getHeight();
System.out.println(loc_x+" "+loc_y+" "+w_x/2+" "+w_y/2);
System.out.print(loc_x/2-w_x/2);
System.out.print(loc_y/2-w_y/2);
//将窗体放置在正中央
frame.setLocation(loc_x/2-300,loc_y/2-200);
}
}