点击这里给我发消息 点击这里给我发消息

swing布局设计及事件监听和javabean数据管理事例

添加时间:2010-6-22
    相关阅读: 设计 数据库 程序 SQL Access

 /*
*这是一个关于菜单以及事件监听器的例子
*其中包含了 JMenu JTable JToolBar 等控件的使用
*本例子用的是access,请先建立txb.mdb
*由不足之处请指教:shengshuai@126.com
*/
import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

public class txb
{
public txb()
{
fsframe Frame1 = new fsframe();

Frame1.validate();

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = Frame1.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
Frame1.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
Frame1.setDefaultCloseOperation(3);
Frame1.show();
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}catch(Exception e)
{
e.printStackTrace();
}
new txb();
}
}

class fsframe extends JFrame implements ActionListener
{

 

JLabel nemelabel = new JLabel("名字:");
JLabel passlabel = new JLabel("密码:");
JLabel zhuzhi = new JLabel("家庭住址:");

JButton insertbutton = new JButton("添加");
JButton exitbutton = new JButton("退出");

JTextField namefield = new JTextField("圣帅");
JTextField passfield = new JTextField("7820131");
JTextField addr = new JTextField("东营职业学院学2(410)宿舍");

JTextArea log = new JTextArea(10,0);
JTextArea input = new JTextArea(4,0);

JPanel inputpanel = new JPanel();
JPanel buttonpanel = new JPanel();

JScrollPane logscrollpane = new JScrollPane();
JScrollPane inputsrollpane = new JScrollPane();

JMenuBar Bare = new JMenuBar();
JMenu filemenu = new JMenu("文件(F)");

JMenuItem jianjie = new JMenuItem("关于本例");
JMenuItem creattablemenuitem = new JMenuItem("建表");
JMenuItem exitmenuitem = new JMenuItem("退出");
JMenu sqlmenu = new JMenu("SQL");

JMenuItem select = new JMenuItem("查看");
JMenuItem del = new JMenuItem("删除所有数据");
JMenuItem deltable = new JMenuItem("删除表");
JMenu about = new JMenu("关于(A)");
JMenuItem aboutme = new JMenuItem("关于作者");

JToolBar toolbar = new JToolBar();

public fsframe()
{

setSize(Toolkit.getDefaultToolkit().getScreenSize().width/2,380);
this.setResizable(false);
this.getContentPane().setLayout(new BorderLayout());
filemenu.setMnemonic(KeyEvent.VK_F);
sqlmenu.setMnemonic(KeyEvent.VK_S);
about.setMnemonic(KeyEvent.VK_A);


Bare.add (filemenu);
filemenu.add (creattablemenuitem);
filemenu.add (exitmenuitem);
exitmenuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK));// 快捷键

Bare.add(sqlmenu);
sqlmenu.add(select);
select.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
select.addActionListener(this);
sqlmenu.add(del);
del.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,ActionEvent.CTRL_MASK));
del.addActionListener(this);
sqlmenu.addSeparator();
sqlmenu.add(deltable);
deltable.addActionListener(this);

Bare.add(about);
//about.add (jianjie);
jianjie.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK));
about.add(aboutme);
aboutme.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,ActionEvent.ALT_MASK));

this.setJMenuBar(Bare);

//-----menu action --------


exitmenuitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent exi){System.exit(0);}});
jianjie.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent jiaj){JOptionPane.showMessageDialog(null,"这是一个关于布局和监听器的例子本例子的操作都对表user执行请先建立数据库txb.mdb");}});
aboutme.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent jiaj){JOptionPane.showMessageDialog(null,"姓名:范圣帅 Email:shengshuai@126.com 网址:http://www.shengshuai.260.cn http://www.myjavaserver.com/~shengshuai");}});
creattablemenuitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent creatt){creattablemenuitemframe creattablemenuitem1 = new creattablemenuitemframe(); creattablemenuitem1.show();creattablemenuitem1.hide();}});


//----end----menu action -//

Container contentpane = getContentPane();
contentpane.setLayout(new BorderLayout());

toolbar.add(jianjie);
//toolbar.add(insertbutton);
contentpane.add(toolbar,BorderLayout.NORTH);


contentpane.add(inputpanel,BorderLayout.CENTER);
inputpanel.setLayout(new GridLayout(3,2));
inputpanel.add(nemelabel);
inputpanel.add(namefield);
inputpanel.add(passlabel);
inputpanel.add(passfield);
inputpanel.add(zhuzhi);
inputpanel.add(addr);addr.addActionListener(this);


contentpane.add(buttonpanel,BorderLayout.SOUTH);
buttonpanel.add(insertbutton);insertbutton.addActionListener(this);
buttonpanel.add(exitbutton);exitbutton.addActionListener(this);

//-------------button action-------------


}
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
JFrame resultframe = new JFrame();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();
// --------查询数据--------------------------------------
if (source==select)
{
access ctm = new access();
ResultSet rs = ctm.executeQuery("SELECT * From user");
try{
getContentPane().add(jScrollPane1);
Vector vcol = new Vector(); //列名
Vector vrow = new Vector(); //内容
vcol.addElement("名字");
vcol.addElement("密码");
vcol.addElement("住址");
while (rs.next())
{
Vector vr1 = new Vector();
vr1.addElement(rs.getString("username"));
vr1.addElement(rs.getString("userpass"));
vr1.addElement(rs.getString("address"));
vrow.addElement(vr1);
}


DefaultTableModel dtm = new DefaultTableModel(vrow, vcol);
jTable1 = new JTable(vrow, vcol);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //滚动条设置左右滚
jScrollPane1.getViewport().add(jTable1, null); //在滚动条中放入表
resultframe.getContentPane().add(jScrollPane1);
resultframe.setSize(400,400);
resultframe.setLocation(200,20);
resultframe.show();
}catch(SQLException sqa){System.out.print(sqa);}
//--------删除数据----------
}else if(source == del)
{
access ctm = new access();
ctm.executeUpdate("DELETE * FROM USER");

//---------删除表-----------

}else if(source == deltable)
{
String tablename = JOptionPane.showInputDialog("请输入要删除的表名","user");
access ctm = new access();
ctm.executeUpdate("DROP TABLE "+tablename);


//-------插入数据------------
}else if (source==insertbutton)
{
access ctm = new access();
String name = namefield.getText().trim();
String pass = passfield.getText().trim();
String address = addr.getText().trim();
//System.out.print(name+pass);
ctm.executeUpdate("INSERT INTO user (username,userpass,address) VALUES ('"+name+"','"+pass+"','"+address+"')");

}else if(source == exitbutton)
{
System.exit(0);
}

}

}

class creattablemenuitemframe extends JFrame
{
public creattablemenuitemframe()
{
String tablename = JOptionPane.showInputDialog("请输入表名","user");
access ctm = new access();
ctm.executeUpdate("CREATE TABLE "+tablename+"(username string ,userpass string,address string )");
hide();
}
}

class access
{
String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String strCon = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=";
Connection con = null;
ResultSet rs = null;
Statement stmt = null;
public access()
{
try
{
Class.forName(dbDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("数据库驱动程序加载失败,异常为:" + e.getMessage());
}
finally
{
System.out.println("驱动程序加载成功");
}
}
public ResultSet executeQuery(String strSQL)
{
rs = null;
try
{
con = DriverManager.getConnection(strCon+"txb.mdb");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(strSQL);

}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"executeQuery在执行"+strSQL+"失败了,异常为: " +ex.getMessage());
}
finally
{
System.out.println("executeQuery--数据库连接成功");
}
return rs;
}

public void executeUpdate(String strSQL)
{
try
{
con = DriverManager.getConnection(strCon+"txb.mdb");
stmt = con.createStatement();
stmt.executeUpdate(strSQL);


}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"executeUpdate在执行"+strSQL+"失败了,异常为: " +ex.getMessage());
}
}

本文作者:
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册