?? dbconui.java
字號:
package view;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import logic.logic;
//數據庫連接窗口
public class dbconUI extends JFrame implements ActionListener{
JTextField tfhostname = new JTextField();//主機名
JTextField tfport = new JTextField();//端口號
JTextField tfdbuname = new JTextField();// 數據庫用戶名
JTextField tfdbupwd = new JPasswordField();//數據庫密碼
JCheckBox saveconn = new JCheckBox();//保存復選框
//--------------------------按鈕
JButton btconnect = new JButton();//連接
JButton btdefault = new JButton();//默認
JButton btunconect = new JButton();//斷開
JButton btexitdb = new JButton();//退出
logic ld=new logic();//實例化業務
//初始化監聽
public void init(){
btconnect.addActionListener(this);
btdefault.addActionListener(this);
btunconect.addActionListener(this);
btexitdb.addActionListener(this);
}
public dbconUI() {
init();//調用初始化監聽器
this.setSize(333,251); //窗口大小
int cx=(int) ((Toolkit.getDefaultToolkit().getScreenSize().width-this.getWidth()))/2;
int cy=(int) ((Toolkit.getDefaultToolkit().getScreenSize().height-this.getHeight()))/2;
setLocation(cx,cy);//窗口起始坐標
setResizable(false);//不能改變窗口大小
getContentPane().setLayout(null);
final JLabel label = new JLabel();
label.setHorizontalAlignment(SwingConstants.RIGHT);
label.setText("主機名稱或 IP:");
label.setBounds(10, 24, 100, 18);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setHorizontalTextPosition(SwingConstants.RIGHT);
label_1.setHorizontalAlignment(SwingConstants.RIGHT);
label_1.setText("數據庫用戶名:");
label_1.setBounds(10, 76, 100, 18);
getContentPane().add(label_1);
final JLabel label_2 = new JLabel();
label_2.setHorizontalAlignment(SwingConstants.RIGHT);
label_2.setText("數 據 庫 密 碼:");
label_2.setBounds(10, 100, 100, 18);
getContentPane().add(label_2);
final JLabel label_3 = new JLabel();
label_3.setHorizontalAlignment(SwingConstants.RIGHT);
label_3.setText("數據庫端口號:");
label_3.setBounds(10, 48, 100, 18);
getContentPane().add(label_3);
tfhostname.setText("localhost");
tfhostname.setBounds(116, 22, 149, 22);
getContentPane().add(tfhostname);
tfport.setText("1433");
tfport.setBounds(116, 48, 149, 22);
getContentPane().add(tfport);
tfdbuname.setText("sa");
tfdbuname.setBounds(116, 74, 149, 22);
getContentPane().add(tfdbuname);
tfdbupwd.setText("sa");
tfdbupwd.setBounds(116, 98, 149, 22);
getContentPane().add(tfdbupwd);
setTitle("不暈校園管理系統->數據庫連接");
btconnect.setText("連接");
btconnect.setBounds(29, 159, 60, 28);
getContentPane().add(btconnect);
btdefault.setText("默認");
btdefault.setBounds(95, 159, 60, 28);
getContentPane().add(btdefault);
btunconect.setText("斷開");
btunconect.setBounds(161, 159, 60, 28);
getContentPane().add(btunconect);
btexitdb.setText("退出");
btexitdb.setBounds(231, 159, 60, 28);
getContentPane().add(btexitdb);
saveconn.setSelected(true);
saveconn.setText("保存此信息下次不再輸入");
saveconn.setBounds(116, 124, 175, 23);
getContentPane().add(saveconn);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //關閉方式
this.setVisible(true);//顯示窗口
}//end struct
//start main
public static void main(String[] args) {
new dbconUI();
}//end main
//------------event
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btexitdb){
dispose();
}//關閉按鈕
if(e.getSource()==btdefault){
tfhostname.setText("localhost");
tfport.setText("1433");
tfdbuname.setText("sa");
tfdbupwd.setText("sa");
}
if(e.getSource()==btunconect){
ld.closeDB();
}
if(e.getSource()==btconnect){
if(ld.connectDB(tfhostname.getText(), tfport.getText(), tfdbuname.getText(), tfdbupwd.getText())==true){
new loginUI();
dispose();
}
}
}//end actionlistener
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -