package com.boyce.view;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import org.omg.CORBA.Request;
import com.boyce.dao.TDao;
import com.boyce.dao.impl.TDaoImpl;
import com.boyce.util.DBUtil;
import java.awt.Window.Type;
import java.util.Vector;
import javax.swing.JCheckBox;
import javax.swing.ScrollPaneConstants;
public class MainFrame {
private JFrame frmjava;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame window = new MainFrame();
window.frmjava.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmjava = new JFrame();
frmjava.setResizable(false);
frmjava.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
TDao tDao = new TDaoImpl();
try {
Vector v = new Vector();
table.setModel(tDao
.getT_tableData("select * from t_tab", v));
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
frmjava.setTitle("我的java窗体");
frmjava.setBounds(100, 100, 518, 530);
frmjava.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmjava.getContentPane().setLayout(
new FlowLayout(FlowLayout.CENTER, 5, 5));
JButton btnNewButton = new JButton("删除选中行");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt((String) table.getValueAt(
table.getSelectedRow(), 0));
TDao tDao = new TDaoImpl();
try {
tDao.deleteById(id, "delete from t_tab where id = ?");
System.out.println("删除ID为:[" + id + "]这条数据成功!");
Vector v = new Vector();
table.setModel(tDao
.getT_tableData("select * from t_tab", v));
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frmjava.getContentPane().add(scrollPane);
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int index = table.getSelectedRow();// 鼠标点击的行数
int id = Integer.parseInt((String) table.getValueAt(index, 0));
System.out.println("您选中了第" + index + "行,数据库ID为" + id + "!");
}
});
scrollPane.setViewportView(table);
table.setModel(new DefaultTableModel(new Object[][] {
{ null, null, null, null, null },
{ null, null, null, null, null },
{ null, null, null, null, null },
{ null, null, null, null, null },
{ null, null, null, null, null }, }, new String[] {
"New column", "New column", "New column", "New column",
"New column" }));
frmjava.getContentPane().setFocusTraversalPolicy(
new FocusTraversalOnArray(new Component[] { table }));
JButton button = new JButton("增加新数据");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddFrame aF = new AddFrame();
aF.setVisible(true);
}
});
frmjava.getContentPane().add(button);
frmjava.getContentPane().add(btnNewButton);
final JCheckBox checkBox = new JCheckBox("中英文显示字段");
checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TDao dao = new TDaoImpl();
if (checkBox.isSelected()) {
System.out.println("被选中");
Vector v = new Vector();// 设置自定义字段名存入Vector集合中
v.add("ID");
v.add("姓名");
v.add("年龄");
v.add("性别");
v.add("电话");
try {
table.setModel(dao.getT_tableData(
"select * from t_tab", v));
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
Vector v = new Vector();// 设置空的集合返回数据库默认字段名
try {
table.setModel(dao.getT_tableData(
"select * from t_tab", v));
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("没选中");
}
}
});
frmjava.getContentPane().add(checkBox);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Demo01.zip (34个子文件)
Demo01
.project 382B
bin
org
eclipse
wb
swing
FocusTraversalOnArray.class 2KB
com
boyce
bean
T.class 3KB
dao
TDao.class 357B
impl
TDaoImpl.class 3KB
test
Test.class 621B
view
AddFrame$1.class 747B
AddFrame$3.class 795B
MainFrame$6.class 2KB
AddFrame.class 3KB
MainFrame$2.class 1KB
MainFrame$5.class 809B
AddFrame$2.class 795B
MainFrame$1.class 852B
MainFrame$3.class 2KB
MainFrame.class 3KB
AddFrame$4.class 3KB
MainFrame$4.class 1KB
util
DBUtil.class 1KB
db.properties 107B
t_tab.sql 1KB
.settings
org.eclipse.core.resources.prefs 57B
org.eclipse.jdt.core.prefs 598B
src
org
eclipse
wb
swing
FocusTraversalOnArray.java 3KB
com
boyce
bean
T.java 2KB
dao
TDao.java 607B
impl
TDaoImpl.java 3KB
test
Test.java 169B
view
MainFrame.java 5KB
AddFrame.java 4KB
util
DBUtil.java 1KB
db.properties 107B
t_tab.sql 1KB
.classpath 401B
共 34 条
- 1
资源评论
boycerodman
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功