📄 liujiao.java
字号:
bCancel = new JButton("取消", new ImageIcon("ico/rq.gif"));
// 设置取消按钮事件响应
bCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
this.addWindowListener(this);
inputPane.add(lServer);
inputPane.add(tServer);
inputPane.add(lDbName);
inputPane.add(tDbName);
inputPane.add(lSql);
inputPane.add(tSql);
inputPane.add(lUsername);
inputPane.add(tUsername);
inputPane.add(lPassword);
inputPane.add(password);
contentPane.add(buttonPane, BorderLayout.SOUTH);
buttonPane.add(bConection);
buttonPane.add(bCancel);
setSize(390, 180);
setResizable(false);
// 设置确定按钮事件响应
bConection.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DatabaseQuery dbq = new DatabaseQuery(Database.this);
dbq.getDatabaseInfo();
dbq.setVisible(true);
}
});
// 增加文本域事件响应
tServer.addKeyListener(this);
tDbName.addKeyListener(this);
tSql.addKeyListener(this);
tUsername.addKeyListener(this);
password.addKeyListener(this);
}
// 数据库实现
class DatabaseQuery extends JDialog {
JScrollPane scroll;
JPanel shutDownPanel = new JPanel();
JButton bShut;
Statement stat;
ResultSet rs;
final JDialog dbDialog;
Connection conn;
public DatabaseQuery(JDialog dbDialog) {
super(dbDialog, "查询结果", true);
this.dbDialog = dbDialog;
setTitle("查询结果");
setDefaultCloseOperation(HIDE_ON_CLOSE);
setSize(300, 250);
}
//
public void getDatabaseInfo() {
//获取文本域输入的信息
strServer = tServer.getText().trim();
strDbName = tDbName.getText().trim();
strSql = tSql.getText().trim();
strUsername = tUsername.getText().trim();
strPassword = password.getText().trim();
getContentPane().add(shutDownPanel, BorderLayout.SOUTH);
bShut = new JButton("关闭", new ImageIcon("ico/exit.gif"));
shutDownPanel.add(bShut);
bShut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
Vector cloumn = new Vector();
Vector rows = new Vector();
try {
//System.out.println(strSql);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://"+strServer+":1433;databaseName="+strDbName+";";
String username = strUsername;
String password = strPassword;
conn = DriverManager.getConnection(url, username, password);
//conn = getConnection();
stat = conn.createStatement();
rs = stat.executeQuery(strSql);
ResultSetMetaData meta = rs.getMetaData();
for (int i = 1; i <= meta.getColumnCount(); i++)
cloumn.addElement(meta.getColumnName(i));
while (rs.next()) {
rows.addElement(getRowNext(rs, meta));
}
JTable table = new JTable(rows, cloumn);
scroll = new JScrollPane(table);
getContentPane().add(scroll, BorderLayout.CENTER);
} catch (Exception e) {
JOptionPane.showMessageDialog(DatabaseQuery.this, "数据库连接错误",
"错误", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
public Vector getRowNext(ResultSet rs, ResultSetMetaData meta)
throws Exception {
Vector currentRow = new Vector();
for (int i = 1; i <= meta.getColumnCount(); i++) {
currentRow.addElement(rs.getString(i));
}
return currentRow;
}
//从当前目录中获取"database.properties"的文件
/* public Connection getConnection() throws Exception {
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url") + strServer
+ ":1433;databaseName=" + strDbName + ";";
//System.out.println(url);
String username = props.getProperty("jdbc.username") + strUsername;
String password = props.getProperty("jdbc.password") + strPassword;
return DriverManager.getConnection(url, username, password);
}*/
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getSource() == password || e.getSource() == tUsername)
bConection.setEnabled(true);
}
public void windowOpened(WindowEvent e) {
bConection.setEnabled(false);
}
public void windowClosing(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowClosed(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowIconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeiconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowActivated(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeactivated(WindowEvent e) {
// TODO 自动生成方法存根
}
}
//查找实现
class FindDialog extends JDialog implements ActionListener, WindowListener,
KeyListener {
final JTextArea text;
JLabel lfind, lreplace;
JTextField tfind, treplace;
JButton bfind, breplace, bnext, bcancel;
JPanel fieldPane, buttonPane;
String strFind, strReplace;
int txtPlace, txtLen, strNext;
FindDialog(final JTextArea text, JFrame findDialog) {
super(findDialog, "查找/替换", false);
this.text = text;
Container findPane = getContentPane();
lfind = new JLabel("查找内容");
lreplace = new JLabel("替换");
tfind = new JTextField();
treplace = new JTextField();
bfind = new JButton("查找", new ImageIcon("ico/Find16.gif"));
breplace = new JButton("替换", new ImageIcon("ico/Replace16.gif"));
bcancel = new JButton("取消", new ImageIcon("ico/exit.gif"));
bnext = new JButton("下一个", new ImageIcon("ico/FindAgain16.gif"));
fieldPane = new JPanel();
buttonPane = new JPanel();
fieldPane.setLayout(new GridLayout(2, 2));
findPane.add(fieldPane, BorderLayout.CENTER);
findPane.add(buttonPane, BorderLayout.SOUTH);
fieldPane.add(lfind);
fieldPane.add(tfind);
fieldPane.add(lreplace);
fieldPane.add(treplace);
buttonPane.add(bfind);
buttonPane.add(breplace);
buttonPane.add(bnext);
buttonPane.add(bcancel);
bfind.addActionListener(this);
breplace.addActionListener(this);
bcancel.addActionListener(this);
this.addWindowListener(this);
tfind.addKeyListener(this);
treplace.addKeyListener(this);
bnext.addActionListener(this);
setSize(370, 120);
setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bfind) {
find();
}
if (e.getSource() == breplace) {
replace();
}
if (e.getSource() == bcancel) {
setVisible(false);
}
if (e.getSource() == bnext) {
findNext();
}
}
//查找处理
private void find() {
strFind = tfind.getText();
strNext = 0; //开始位置
txtPlace = text.getText().indexOf(strFind, strNext);
txtLen = txtPlace + strFind.length();
text.select(txtPlace, txtLen);
strNext = text.getSelectionEnd(); //选中内容的最后位置
if (txtPlace == -1) /*没有找到的处理*/ {
JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找",
JOptionPane.INFORMATION_MESSAGE);
}
bnext.setEnabled(true);
}
//替换处理
private void replace() {
strReplace = treplace.getText();
text.replaceRange(strReplace, txtPlace, txtPlace + strFind.length());
}
//查找下一个处理
private void findNext() {
bfind.setEnabled(false);
txtPlace = text.getText().indexOf(strFind, strNext);
txtLen = txtPlace + strFind.length();
text.select(txtPlace, txtLen);
strNext = text.getSelectionEnd(); //获取查找下一个内容的最后位置
if (txtPlace == -1) /*没有找到的处理*/ {
JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找下一个",
JOptionPane.INFORMATION_MESSAGE);
strNext = 0; //没有找到初始化位置,以变重新查找
tfind.setText("");
bnext.setEnabled(false);
breplace.setEnabled(false);
}
}
public void windowOpened(WindowEvent e) {
bfind.setEnabled(false);
breplace.setEnabled(false);
bnext.setEnabled(false);
}
public void windowClosing(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowClosed(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowIconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeiconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowActivated(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeactivated(WindowEvent e) {
// TODO 自动生成方法存根
}
public void keyTyped(KeyEvent e) {
// TODO 自动生成方法存根
}
public void keyPressed(KeyEvent ke) {
if (ke.getSource() == tfind)
bfind.setEnabled(true);
if (ke.getSource() == treplace)
breplace.setEnabled(true);
}
public void keyReleased(KeyEvent e) {
// TODO 自动生成方法存根
}
}
// 帮助的关于对话框行为
class ActionAbout extends JDialog {
public ActionAbout(JFrame dialog) {
super(dialog, "关于", true);
Container contentPane = getContentPane();
contentPane.add(new JLabel("<html><h1>欢迎使用JAVA字处理软件</h1><hr>"
+ "<h2>版本号(Beta):1.0</h2><hr>" + "<h3>******</h3></html>"),
BorderLayout.CENTER);
JPanel p = new JPanel();
JButton ok = new JButton("确定", new ImageIcon("ico/ok.gif"));
setResizable(false);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
p.add(ok);
contentPane.add(p, BorderLayout.SOUTH);
contentPane.add(new JLabel(new ImageIcon("ico/duke.gif")),
BorderLayout.WEST);
setSize(400, 250);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -