package zwh.db2.six; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.table.*; public class GUI extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; Tool t = new Tool(); private JButton input;//单行插入 private JButton inputs;//多行插入 private JButton query;//子查询插入;表:like_employee_zwh private JButton delete;//删除 private JButton update;//修改 private JTable table;//表 private DefaultTableModel tableModel;//表格模型对象 private String[][] data;//数据存储 private static String [] name = {"EMPNO","FIRSTNME","MIDINIT","LASTNAME","EDLEVEL"}; public static void main(String[] args){//主方法 new GUI(); } public GUI(){//设置整体 super("第六次实验"); Main(); setSize(600,600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//单机窗口关闭按钮时程序执行的操作 setLocationRelativeTo(null);//使窗口显示在屏幕中央 setVisible(true);//窗体可见 } public void Main(){ //向table加入数据 data = t.show(); tableModel = new DefaultTableModel(data,name){ private static final long serialVersionUID = 1L; //表格首列不允许被编辑(任务6修改需要) public boolean isCellEditable(int row,int column){ if(column==0){ return false; }else{ return true; } } }; table = new JTable(tableModel); Container c = getContentPane();//容器 c.setLayout(new FlowLayout());//设置窗体布局为流动布局 ,流动布局按照从左至右,从上至下原则对控件进行排版 JScrollPane scrollPane = new JScrollPane(table);//滚动容器 scrollPane.setAutoscrolls(true);//滚动面板自动滚动 //任务6后加2:排序: table.setRowSorter(new TableRowSorter<DefaultTableModel>(tableModel)); //声明按钮 input = new JButton("单行插入"); inputs = new JButton("多行插入"); query = new JButton("子查询插入"); delete = new JButton("删除行"); update = new JButton("修改保存"); //列式布局 Box box1; box1=Box.createHorizontalBox();//从左到右显示组件 box1.add(input); box1.add(Box.createHorizontalStrut(8)); box1.add(inputs); box1.add(Box.createHorizontalStrut(8)); box1.add(query); Box box2; box2=Box.createHorizontalBox();//从左到右显示组件 box2.add(delete); box2.add(Box.createHorizontalStrut(8)); box2.add(update); c.add(box1);//列式布局放到容器里 c.add(scrollPane);//表格放到容器里 c.add(box2); input.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { new dialog(); dispose(); } }); inputs.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { final JDialog a = new JDialog(); JPanel jp=new JPanel(); jp.setLayout(new FlowLayout()); jp.setBorder(new EmptyBorder(10,10,10,10)); Box b1 = Box.createHorizontalBox(); final JTextField text = new JTextField(10); b1.add(new JLabel("请输入添加几行:")); b1.add(Box.createHorizontalStrut(6)); b1.add(text); Box b2 = Box.createHorizontalBox(); //按钮 JButton okB = new JButton("确认"); okB.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { String count = text.getText(); new dialog1(Integer.parseInt(count)); a.dispose(); } }); JButton cancelB = new JButton("取消"); cancelB.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { new GUI(); dispose(); } }); //列式布局 b2=Box.createHorizontalBox(); b2.add(okB); b2.add(Box.createHorizontalStrut(10)); b2.add(cancelB); jp.add(b1); jp.add(b2); a.add(jp); a.setSize(325,125); a.setLocationRelativeTo(null);//使窗口显示在屏幕中央 a.setVisible(true); dispose(); } }); query.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { final JDialog a = new JDialog(); JPanel jp=new JPanel(); jp.setLayout(new FlowLayout()); jp.setBorder(new EmptyBorder(10,10,10,10)); JLabel label = new JLabel("是否将like_employee_zwh的数据添加到employee中?"); Box b = Box.createHorizontalBox(); //按钮 JButton okB = new JButton("确认"); okB.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { t.workFive_query(); new GUI(); a.dispose(); } }); JButton cancelB = new JButton("取消"); cancelB.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { new GUI(); dispose(); } }); //列式布局 b=Box.createHorizontalBox(); b.add(okB); b.add(Box.createHorizontalStrut(10)); b.add(cancelB); jp.add(label); jp.add(b); a.add(jp); a.setSize(400,125); a.setLocationRelativeTo(null);//使窗口显示在屏幕中央 a.setVisible(true); dispose(); } }); //任务6后加3-删除 delete.addActionListener(new ActionListener(){//删除行 public void actionPerformed(ActionEvent e){ int selectedRow = table.getSelectedRow();//获得选中行的索引 if(selectedRow!=-1){//存在选中行 Object value = table.getValueAt(selectedRow, 0); tableModel.removeRow(selectedRow); //删除行 t.delete(value); }else{ t.errorMessage("请选中一行进行操作");//任务7.7 } } }); //任务8后加-修改 update.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { int rowCount = tableModel.getRowCount(); //获取一行数据 for(int i=0;i<rowCount;i++){ String[] oneRowData = new String[]{ t.ifNull(tableModel.getValueAt(i, 1),1).toString(), t.ifNull(tableModel.getValueAt(i, 2),1).toString(), t.ifNull(tableModel.getValueAt(i, 3),1).toString(), t.ifNull(tableModel.getValueAt(i, 4),1).toString(), t.ifNull(tableModel.getValueAt(i, 0),1).toString() }; t.updateAllTable(oneRowData); } dispose(); new GUI(); } }); } @Override public void actionPerformed(ActionEvent arg0) {} }
最近下载更多