📄 serverframe.java
字号:
//本类是考试系统的初始界面,主要进行考试系统数据库的选择,数据库用户和密码的输入。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class ServerFrame extends JFrame implements ItemListener,ActionListener{
public static void main(String args[]){
ServerFrame q=new ServerFrame();
}
String dbna="test";
String user="sa";
String pass="123456";
String db1="sqlserver";
String ss="";
//北
JLabel tishi;//标题
JPanel p;
//中
JLabel db;JComboBox comboBox;
JLabel dbname;JTextField dbta; //输入数据库名
JButton b;private JFileChooser chooser;
JLabel dbname1;JTextField dbta1;//用户名
JLabel dbname2;JTextField dbta2;//密码
//南
JButton b1; //提交答案按狃
JButton b2; //显示成绩按钮
/** 构造方法:初始化面板中的图形组件、数据流、计时器、线程*/
//初始化套接字和接收、发送数据的数据流
public void itemStateChanged(ItemEvent event){
if(comboBox.getSelectedItem().equals("access"))
{dbname.setText("odbc数据源");b.setVisible(false);
}
else {if(comboBox.getSelectedItem().equals("access数据库直连"))
{dbname.setText("access数据库名");
b.setVisible(true);}
else
{dbname.setText("jdbc数据库");b.setVisible(false);}
}
}
//**按钮事件动作响应
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1) {
startButtonPerformed();//
}
if(e.getSource()==b2) {
System.exit(0);
}
if(e.getSource()==b){
chooseButton();}
if(comboBox.getSelectedIndex()==0)
System.out.println("sqlserver");
if(comboBox.getSelectedIndex()==1){
System.out.println("access");
}
}
public void chooseButton(){
int state; //文件选择器返回状态
chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter()); //移去所有文件过滤器
chooser.addChoosableFileFilter(new MyFileFilter("mdb","数据库文件")); //增加文件过滤器,接爱gif文件
if (comboBox.getSelectedIndex()==3) //组合框为"打开"
{ state=chooser.showOpenDialog(null); //显示打开文件对话框
//显示保存文件对话框
File file = chooser.getSelectedFile(); //得到选择的文件
if(file != null && state == JFileChooser.APPROVE_OPTION) { //选择了文件并点击了打开可保存按钮
JOptionPane.showMessageDialog(null, file.getPath()); //显示提示信息
ss=file.getPath();
ss=ss.replaceAll("\\\\","\\\\\\\\");
dbta.setText(file.getPath());
}
else if(state == JFileChooser.CANCEL_OPTION) { //点击了撤销按钮
JOptionPane.showMessageDialog(null, "退出!"); //显示提示信息
}
else if(state == JFileChooser.ERROR_OPTION) {
JOptionPane.showMessageDialog(null, "错误!"); //显示提示信息
}}
}
/*线程启动后执行run()方法,接收服务器发送回来的信息*/
/**初始化面板中的图形组件*/
public ServerFrame() {
p=new JPanel();
setContentPane(p);
p.setLayout(new BorderLayout());
//北—添加"开始考试"按钮和剩余时间标签
JPanel northPanel=new JPanel();
northPanel.setLayout(new GridLayout(1,1));
tishi=new JLabel("欢迎进入考试系统服务器端配置",JLabel.CENTER);
northPanel.add(tishi);
p.add(northPanel,BorderLayout.NORTH);
//中—添加试题显示区
JPanel centerPanel=new JPanel();chooser=new JFileChooser();
JPanel centerPanel1=new JPanel();
centerPanel1.setLayout(new GridLayout(1,2));
centerPanel.setLayout(new GridLayout(4,2,2,2));
db=new JLabel("数据库类型"); comboBox=new JComboBox();comboBox.addItem("sqlserver");comboBox.addItem("access");
comboBox.addItem("mysql");comboBox.addItem("access数据库直连");
comboBox.addItemListener(this);
dbname=new JLabel("数据库");
dbta=new JTextField();
dbname1=new JLabel("用户名:");
dbta1=new JTextField();
dbname2=new JLabel("密码");
dbta2=new JTextField();
b=new JButton("浏览");b.addActionListener(this);
centerPanel1.add(dbname);
centerPanel1.add(b);
centerPanel.add(db);
centerPanel.add(comboBox);
centerPanel.add(centerPanel1);
centerPanel.add(dbta);
centerPanel.add(dbname1);
centerPanel.add(dbta1);
centerPanel.add(dbname2);
centerPanel.add(dbta2);
centerPanel.setSize(300,100);
p.add(centerPanel,BorderLayout.CENTER);
//南—4个单选按钮
JPanel southPanel=new JPanel();
//初始化各按钮,增加鉴听,设置为不可点击
b1=new JButton("下一步");
b1.addActionListener(this);
b2=new JButton("退出");
b2.addActionListener(this);
southPanel.add(b1);
southPanel.add(b2);
p.add(southPanel,BorderLayout.SOUTH);
b.setVisible(false);
this.setSize(300,300);
this.setLocation(400,300);
this.setVisible(true);
setTitle("标准化考试系统");
}
/**点击"下一步"按钮后要执行的任务*/
private void startButtonPerformed(){
if(!(dbta.getText().toString()).equals(""))
{this.dbna=dbta.getText();//数据库名
this.user=dbta1.getText();//用户名
this.pass=dbta2.getText();//密码
this.db1=comboBox.getSelectedItem().toString();//数据库类型
if(db1.equals("access数据库直连"))
if((ss.substring(ss.lastIndexOf(".")+1)).equals("mdb"))
{
this.dbna=ss;}
else
this.dbna=dbta.getText().trim()+".mdb";
System.out.print(dbna);
ConnTest c=new ConnTest();
String re=c.connresult(db1,dbna,user,pass);
dispose();
if(!(re.equals("true")))
{JOptionPane.showMessageDialog(ServerFrame.this,re);
new ServerFrame();
}
else
{try{
new Server(dbna,user,pass,db1);//db1代表数据库类型
}
catch(Exception eeee)
{}
}
}
else
{ String message="请输入数据库名"; //消息
JOptionPane.showMessageDialog(ServerFrame.this, message); //显示消息
dbta1.setText("");}
dbta2.setText("");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -