日期:2014-05-20 浏览次数:20786 次
import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.*; public class Demo10 extends JFrame implements KeyListener{ /** * */ private static final long serialVersionUID = 1L; GlobalVari_5 gv = new GlobalVari_5(); public Demo10(){ this.setTitle("键盘事件处理"); this.setSize(300, 200); this.setLocationRelativeTo(this);// 窗口居中 this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗口关闭动作处理 this.addKeyListener(this);// 添加注册键盘事件监听器 this.gv.setVisible(false); } @Override public void keyPressed(KeyEvent arg0){// 敲击 } @Override public void keyReleased(KeyEvent arg0) {// 按下 if (arg0.getKeyCode() == 114) {// 114 F3对应的编码 Demo10.print(); gv.setVisible(true); } } @Override public void keyTyped(KeyEvent arg0) {// 释放 } public static void main(String[] args) { new Demo10(); } public static void print() { System.out.println("Running!"); } }
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; public class GlobalVari_5 extends JFrame{ private static final long serialVersionUID = 1L; private static double select = 0; private static int rowIndex = 0; // /表格方法使用 public GlobalVari_5(){ init(); this.setTitle("Global Variables"); this.setSize(new Dimension(650, 400)); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true); this.setLayout(null); this.setResizable(false); } private void init(){ Vector<String> colHeader = new Vector<String>(); colHeader.add("Variable"); colHeader.add("Value"); colHeader.add("Time"); Vector<Vector<String>> dataVec = new Vector<Vector<String>>(); final JTable table = new JTable(dataVec,colHeader){ private static final long serialVersionUID = 1L; public boolean isCellEditable(int row, int column){ if(column > 1){ return false; } else { return true; } } }; //2设置表头行高 table.getTableHeader().setPreferredSize(new Dimension(0,30)); //3设置表内容行高 table.setRowHeight(25); //4设置单选模式 table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //5设置单元格不可拖动 table.getTableHeader().setReorderingAllowed(false); //6设置不可改变列宽 table.getTableHeader().setResizingAllowed(false); //7设置列宽 table.getColumnModel().getColumn(0).setPreferredWidth(95); table.getColumnModel().getColumn(1).setPreferredWidth(55); table.getColumnModel().getColumn(2).setPreferredWidth(30); //注意索引越界 DefaultTableCellRenderer right = new DefaultTableCellRenderer(); right.setHorizontalAlignment(JLabel.RIGHT); table.getColumnModel().getColumn(1).setCellRenderer(right); table.getColumnModel().getColumn(2).setCellRenderer(right); final JButton buttonAdd = new JButton("Add"); final JButton buttonDel = new JButton("Delete"); final JButton buttonCls = new JButton("Close"); rowIndex = table.getSelectedRow(); System.out.println("index:"+rowIndex); //监听事件 table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent e){ if(e.getValueIsAdjusting()){//连续操作 rowIndex = table.getSelectedRow(); if(rowIndex != -1){ System.out.println("表格行被选中"+rowIndex); buttonDel.setEnabled(true); select = 1; } } } }); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(table); this.add(scrollPane); scrollPane.setSize(515, 350); scrollPane.setLocation(5, 5); buttonAdd.setEnabled(true); buttonDel.setEnabled(false); buttonCls.setEnabled(true); JPanel panel = new JPanel(); panel.setSize(10, 350); panel.setLocation(520, 5); this.add(panel); panel.setLayout(null); panel.add(buttonAdd); buttonAdd.setSize(100, 30); buttonAdd.setLocation(530, 6); panel.add(buttonDel); buttonDel.setSize(100, 30); buttonDel.setLocation(530, 50); panel.add(buttonCls); buttonCls.setSize(100, 30); buttonCls.setLocation(530, 322); buttonAdd.addMouseListener(new MouseListener(){ public void mouseClicked(MouseEvent e){ DefaultTableModel model = (DefaultTableModel)table.getModel(); int rownum = table.getRowCount() + 1; model.addRow(new Object[]{"gvar" + rownum, "", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())}); } public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } }); buttonDel.addMouseListener(new MouseListener(){ public void mouseClicked(MouseEvent e){ if(select == 1){ DefaultTableModel model = (DefaultTableModel)table.getModel(); model.removeRow(rowIndex); select = 0; buttonDel.setEnabled(false); } } public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } }); buttonCls.addMouseListener(new MouseListener(){ public void mouseClicked(MouseEvent e){ setVisible(false); } public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } }); } public static void main(String[] args){ new GlobalVari_5(); } }