Java Swing JButton绑定事件之lambda写法

《Java从入门到精通》(第5版)(明日科技)第255页,常用事件监听器部分写的太复杂了,绑定事件时还要专门写一个类,蛋疼!!!jetbrains idea推荐了lambda 写法。

import javax.swing.*;
import java.awt.*;
public class JPanelTest extends JFrame {
    public JPanelTest() {
        Container c = getContentPane();
        c.setLayout(new GridLayout(2,1,10,10));
        JPanel p1 = new JPanel(new GridLayout(1, 1, 10, 10));
        JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER,10,10));
        JLabel jl = new JLabel("我是小淘气", JLabel.CENTER);
        p1.add(jl);
        JButton b1 = new JButton(" 打屁屁");
        //b1.setSize(5, 20);
        JButton b2 = new JButton("不打屁屁");
        //b2.setSize(5, 20);
        p2.add(b1);
        p2.add(b2);
        c.add(p1);
        c.add(p2);
        b1.addActionListener(e -> jl.setText("不要啊!!!"));  //lambda写法好简单
        b2.addActionListener(e -> jl.setText("感谢不杀之恩!!!"));
        setSize(500, 500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JPanelTest();
    }
}
import javax.swing.*;
import java.awt.*;
public class SimpleEvent extends JFrame {
    public SimpleEvent() {
        setTitle("简单事件");
        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
        Container c = getContentPane();
        JLabel jl = new JLabel("我是好孩子");
        JButton jb = new JButton("我是按钮,点击我");
        c.add(jl);
        c.add(jb);
        //setBounds(10, 10, 100, 100);
        jb.addActionListener(e -> {      //lambda写法中多行命令时
            jb.setText("我被单击了");
            jl.setText("不要啊");
        });
        setSize(500, 500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new SimpleEvent();
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.; import javax.swing.JFileChooser; import java.awt.event.;//导入ActionListener接口所在的包event public class demo extends JFrame{ public static void main(String[] args) { JFrame frame=new JFrame();//创建一个窗口 frame.setTitle("开始你的复制吧!");//设置窗口标题 frame.setBounds(400,400,400,300);//设置窗口的位置和大小 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗口关闭时的操作 frame.setBackground(Color.CYAN);//设置Frame的背景色 frame.setLayout(null);//不适用布局管理器,设置为NULL JTextField textField1=new JTextField(30); JTextField textField2=new JTextField(30); JLabel label1=new JLabel("输入你要拷贝的文件:"); JLabel label2=new JLabel("输入拷到哪里去:"); textField1.setColumns(30); textField2.setColumns(30); label1.setBounds(10,10,120,20); label2.setBounds(10,40,120,20); textField1.setBounds(140,10,200,20); textField2.setBounds(140,40,200,20); frame.add(label1); frame.add(textField1); frame.add(label2); frame.add(textField2); //frame.setSize(400,100); //frame.setLocation(300,200); frame.setVisible(true); JButton button1 =new JButton("copy"); button1.setBounds(270,70,100,20); button1.setBackground(Color.CYAN);//做作的天蓝色 frame.add(button1); JButton button2=new JButton("..."); button2.setBounds(350,12,20,16); frame.add(button2); JButton button3=new JButton("..."); button3.setBounds(350,42,20,16); frame.add(button3); class button2 implements ActionListener { public void actionPerformed(ActionEvent e) {//该方法是触发事件时程序要做什么 JFileChooser fileChooser=new JFileChooser("d://"); fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//可以选择文件和文件夹 if (fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){ String filePath=fileChooser.getSelectedFile().getAbsolutePath(); textField1.setText(filePath); } } } } }为什么点击button不出菜单
05-27

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值