abstractAction

abstractAction主要是可以统一定制,不需要像actionListener要一个一个去注册。

 

具体代码如下:

Jframe中一共3个地方用到了abstractAction:jmenuitem,jtoobar,jbutton

 

 

Java代码   收藏代码
  1. package testAbstractClass;  
  2.   
  3. import java.awt.*;    
  4. import java.awt.event.*;    
  5. import javax.swing.*;    
  6. import javax.swing.event.*;    
  7.     
  8. public class AbstractActionDemo1 extends JFrame {    
  9.     JTextArea theArea = null;    
  10.     ToolBarAction pushAction = null;    
  11.         
  12.     public AbstractActionDemo1(){    
  13.         super("AbstractAction");    
  14.         theArea = new JTextArea();    
  15.         theArea.setEditable(true);    
  16.         this.getContentPane().add(new JScrollPane(theArea));    
  17.             
  18.             
  19.         pushAction = new ToolBarAction("按我",null);    
  20.             
  21.         JMenuBar menuBar = buildJMenuBar();    
  22.         menuBar.setOpaque(true);    
  23.         JToolBar toolBar = buildJToolBar();    
  24.         this.getContentPane().add(toolBar,BorderLayout.NORTH);    
  25.           
  26.         JButton jb=new JButton(pushAction);  
  27.         this.getContentPane().add(jb,BorderLayout.WEST);    
  28.           
  29.         this.setJMenuBar(menuBar);    
  30.         this.setVisible(true);    
  31.         this.setBounds(new Rectangle(200,200,400,200));    
  32.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  33.     }    
  34.         
  35.     public static void main(String...args){    
  36.         new AbstractActionDemo1();    
  37.     }    
  38.         
  39.     public JMenuBar buildJMenuBar(){    
  40.         JMenu fileMenu = new JMenu("File");    
  41.         fileMenu.setMnemonic('F');    
  42.         fileMenu.add(pushAction);    
  43.         JMenuBar menuBar = new JMenuBar();    
  44.         menuBar.add(fileMenu);  
  45.         return menuBar;    
  46.     }    
  47.         
  48.     public JToolBar buildJToolBar(){    
  49.         JToolBar toolBar = new JToolBar();    
  50.         toolBar.add(pushAction);    
  51.         toolBar.setFloatable(true);    
  52.         return toolBar;    
  53.             
  54.     }    
  55.         
  56.     class ToolBarAction extends AbstractAction{    
  57.         public ToolBarAction(String name,Icon icon){    
  58.             super(name,icon);    
  59.         }    
  60.         @Override    
  61.         public void actionPerformed(ActionEvent e){    
  62.             theArea.append("這是Action的好處\n");    
  63.         }    
  64.     }    
  65. }    
修改这段代码实现选择表格一行信息删除package frame; import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; //添加页面 public class EditDialog extends BaseDialog{ DataChangelisenter dataChangelisenter; public void setDataChangelisenter(DataChangelisenter dataChangelisenter) { this.dataChangelisenter = dataChangelisenter; } @Override public void init() { setLayout(new FlowLayout()); //布局 setTitle("修改图书信息"); JPanel jp0=new JPanel(); JLabel l0 = new JLabel("id"); JTextField t0 = new JTextField(); t0.setPreferredSize(new Dimension(300,30)); jp0.add(l0); jp0.add(t0); JPanel jp1=new JPanel(); JLabel l1 = new JLabel("书名"); JTextField t1 = new JTextField(); t1.setPreferredSize(new Dimension(300,30)); jp1.add(l1); jp1.add(t1); JPanel jp2=new JPanel(); JLabel l2 = new JLabel("作者"); JTextField t2 = new JTextField(); t2.setPreferredSize(new Dimension(300,30)); jp2.add(l2); jp2.add(t2); JPanel jp3=new JPanel(); JLabel l3 = new JLabel("分类"); JTextField t3 = new JTextField(); t3.setPreferredSize(new Dimension(300,30)); jp3.add(l3); jp3.add(t3); JPanel jp4=new JPanel(); JLabel l4 = new JLabel("出版社"); JTextField t4 = new JTextField(); t4.setPreferredSize(new Dimension(300,30)); jp4.add(l4); jp4.add(t4); JPanel jp5=new JPanel(); JLabel l5 = new JLabel("数量"); JTextField t5 = new JTextField(); t5.setPreferredSize(new Dimension(300,30)); jp5.add(l5); jp5.add(t5); add(jp0); add(jp1); add(jp2); add(jp3); add(jp4); add(jp5); JButton b1 = new JButton("保存"); b1.setPreferredSize(new Dimension(150,30)); b1.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { String id = t0.getText(); String sname = t1.getText(); String author = t2.getText(); String classify = t3.getText(); String press = t4.getText(); String quantity = t5.getText(); Connection con = DBUtil.getConnect(); String sql = "UPDATE tushu SET sname=?,author=?,classify=?,press=?,quantity=? WHERE id=?"; try { PreparedStatement ps = con.prepareStatement(sql); ps.setString(1,sname); ps.setString(2,author); ps.setString(3,classify); ps.setString(4,press); ps.setString(5,quantity); ps.setString(6,id); int result = ps.executeUpdate(); if (result>0){ JOptionPane.showMessageDialog(EditDialog.this,"修改成功"); // new StudentFrame(); dataChangelisenter.dataChange(); dispose(); }else { JOptionPane.showMessageDialog(EditDialog.this,"修改失败"); } } catch (SQLException throwables) { throwables.printStackTrace(); } } }); add(b1); } }
最新发布
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值