java用Swing实现简单的点击按钮,效果显示当前时间

废话不多说,代码走起:

备注为了方便,个人新建了一个myFrame类。

 

 

 

//
//swing实现简单的窗口点击事件,点击效果为显示当前时间
//Create by Lee_1310 on2019.3.21 16:57
//Reprint indicating source:https://blog.csdn.net/Lee_1310
//
package SwingDemo;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SwingDemo {

	private static void createGUI()
	{
		//初始化窗口程序
		myFrame frame = new myFrame("Lee");//窗口命名
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//frame.setBackground(Color.red);//暂时没用
		frame.setSize(500,350);
		frame.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run()
			{
				createGUI();
			}
		});

	}

}

以下是个人的myFrame

package SwingDemo;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class myFrame extends JFrame{
	JButton button = new JButton("显示时间");
	JLabel timeLabel= new JLabel("00:00:00");
	public myFrame(String title)
	{
		super(title);
		Container contentPane = getContentPane();
		contentPane.setLayout(new FlowLayout());
		
		//创建一个JLaber和一个JButton
		contentPane.add(button);
		contentPane.add(timeLabel);
		
		//创建监听器对象
		/*
		 * MyButtonListener listener = new MyButtonListener();
		 * private class MyButtonListener implements ActionListener ()
		 * {
		 	 	@Override
				public void actionPerformed(ActionEvent arg0)
				{
					myFrame.this.showTime();
				}
			}
		 */
		//把监听器注册给按钮
		//匿名类
//		button.addActionListener(new ActionListener() {//接口不能直接实例化所以直接在后面加大括号调用方法
//
//			@Override
//			public void actionPerformed(ActionEvent arg0)
//			{
//				myFrame.this.showTime();
//			}
//		});
		button.addActionListener((e)->{//lambda表达式
			showTime();//也可以使用myFrame.this.showTime();//调用外部类的方法
		});
	}
	public void showTime()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String strtime = sdf.format(new Date());
		timeLabel.setText(strtime);
	}
	

	
}

运行结果如图:

每次点击会或得当前时间

其中

button.addActionListener(new ActionListener() {//接口不能直接实例化所以直接在后面加大括号调用方法
            
	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		myFrame.this.showTime();
	}
});

这一段代码可以用lambda表达式简化为:

button.addActionListener((e)->{//lambda表达式
	showTime();//也可以使用myFrame.this.showTime();//调用外部类的方法
});

lambda表达式可以参照:https://www.cnblogs.com/tiantianbyconan/p/3613506.html

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用JavaSwing库中的JFileChooser组件来让用户选择要复制的文件,然后使用Java IO库中的FileInputStream和FileOutputStream来进行文件复制操作。 以下是一个示例代码: ```java import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class FileCopyExample { public static void main(String[] args) { JFrame frame = new JFrame("File Copy Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JButton copyButton = new JButton("Copy File"); copyButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(frame); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); String destinationFilePath = selectedFile.getName() + "_copy"; File destinationFile = new File(destinationFilePath); try { FileInputStream inputStream = new FileInputStream(selectedFile); FileOutputStream outputStream = new FileOutputStream(destinationFile); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); JOptionPane.showMessageDialog(panel, "File copied successfully!"); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(panel, "Error copying file!"); } } } }); panel.add(copyButton); frame.add(panel); frame.pack(); frame.setVisible(true); } } ``` 这个程序创建了一个简单Swing窗口,其中包含一个按钮。单击按钮时,程序会打开文件选择对话框,让用户选择要复制的文件。然后,程序会将选定的文件复制到当前目录下并添加“_copy”后缀。在复制完成后,程序会弹出一个消息框提示用户操作成功或失败。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值