java lambda 实现事件监听 程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本文由我司收集整编,推荐下载,如有疑问,请与我司联系
java lambda 实现事件监听程序
2017/09/02 0 package NEW_DATE_SEQUENCE_PACKAGE;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.File;public class ButtonFrame { public static void main(String [] args) { EventQueue.invokeLater(()- { JFrame frame=new ButtonFrame_2(); frame.setTitle(“ImageTest...........”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); }); }} class ButtonFrame_2 extends JFrame{ private JPanel buttonPanel; private static final int DEFAULT_WIDTH=300; private static final int DEFAULT_HEIGHT=200; public ButtonFrame_2() { setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); buttonPanel=new JPanel(); add(buttonPanel); JButton yellowButton=new JButton(“yellow”);JButton blueButton=new JButton(“blue”);JButton redButton=new JButton(“red”);JButton what=new JButton(“what”); buttonPanel.add(yellowButton); buttonPanel.add(blueButton); buttonPanel.add(redButton); buttonPanel.add(what); what.addActionListener(event- { buttonPanel.setBackground(Color.PINK); }); ColorAction y=new ColorAction(Color.YELLOW); ColorAction b=new ColorAction(Color.blue); ColorAction r=new ColorAction(Color.RED); yellowButton.addActionListener(y); blueButton.addActionListener(b); redButton.addActionListener(r); makeButton(“1”,Color.GREEN);makeButton(“2”,Color.CYAN);}/*** 辅助方法*/ public void makeButton(String n,Color c) { JButton simpleButton=new JButton(n); buttonPanel.add(simpleButton); simpleButton.addActionListener(event- { buttonPanel.setBackground(c); }); } private class ColorAction implements ActionListener { private Color col; public ColorAction(Color c) { col=c; } public void actionPerformed(ActionEvent event) { buttonPanel.setBackground(col); } }java.awt.event.ActionEvent String getActionCommand(); 返回与这个动作事件关联的命令字符串。如果这个动作源自