十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
package com.ie;
创新互联是一家集网站建设,高密企业网站建设,高密品牌网站建设,网站定制,高密网站建设报价,网络营销,网络优化,高密网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Test1 {
public static void main(String args[]){
Test1 t = new Test1();
t.practice();
}
public void practice(){
JFrame jf = new JFrame("监听器");
jf.setLayout(null);//关闭布局管理器
jf.setBounds(100, 100, 400, 400);//指定程序窗口的初始位置及大小
JTextField jtf = new JTextField("单行文本输入框");
jf.add(jtf);
jtf.setBounds(10, 10, 100, 20);//指定单行文本输入框在窗口中位置及大小
jtf.addFocusListener(new FocusListenerImpl());
JButton b = new JButton("确定");
jf.add(b);
b.setBounds(10, 40, 100, 20);
jf.setVisible(true);
}
class FocusListenerImpl implements FocusListener{
@Override
public void focusGained(FocusEvent e) {
System.out.println("获得焦点");
}
@Override
public void focusLost(FocusEvent e) {
System.out.println("失去焦点");
}
}
}
JTextFiled获得焦点简单来说就是选中时有输入光标闪动,失去焦点就是输入光标消失,
失焦获焦的用输出语句简单写的,看控制台。
首先写一个IListener接口,IListener.java:
package listenertest3;
public interface IListener {
public void afterDownLoad(DownLoadEvent event);
}
然后是实现了IListener接口的DownLoadListener.java:
package listenertest3;
public class DownLoadListener implements IListener {
@Override
public void afterDownLoad(DownLoadEvent event) {
System.out.println(event.getFileName() + " download finished");
}
}
写一个下载事件类DownLoadEvent.java,里边有文件名称以及是否下载完成两个属性:
package listenertest3;
public class DownLoadEvent {
private String fileName;
private boolean isFinish = false;
public DownLoadEvent(String fileName) {
this.fileName = fileName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public boolean isFinish() {
return isFinish;
}
public void setFinish(boolean isFinish) {
this.isFinish = isFinish;
}
}
接下来是事件发生类DownLoadFile.java,在其中的download方法中首先打印出start download,然后暂停1000ms代表下载,之后下载完成,将下载事件中的下载是否完成属性设成true,然后就会执行listerner中的afterDownLoad方法:
package listenertest3;
public class DownLoadFile {
private IListener listener;
public IListener getListener() {
return listener;
}
public void setListener(IListener listener) {
this.listener = listener;
}
public void download() throws InterruptedException {
DownLoadEvent event = new DownLoadEvent("No Man Land");
System.out.println("start download");
Thread.sleep(1000);
event.setFinish(true);
listener.afterDownLoad(event);
}
}
最后是main方法:
package listenertest3;
public class MainTest {
public static void main(String[] args) throws InterruptedException {
DownLoadFile downloadFile = new DownLoadFile();
downloadFile.setListener(new DownLoadListener());
downloadFile.download();
这个 真的没啥意思 给按钮添加一个监听 监听调用 button的另一个调用图片的构造方法 很方便\
package com.test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ButtonDemo {
URL url = this.getClass().getResource("poto.jpg");
public ButtonDemo() {
}
public void showMe() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
final JButton button;
button = new JButton("点击我!");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button.setIcon(new ImageIcon(url));
}
});
frame.add(panel);
panel.add(button);
frame.setSize(600, 500);
frame.setVisible(true);
}
public static void main(String[] args) {
new ButtonDemo().showMe();
}
}
这是基本的 你看看吧
package 实验;
import javax.swing.*;
import java.awt.event.*;
public class haha {
public static void main(String[] args) {
//调用c来启动
new c();
}
}
class a extends JFrame{
//第一个按钮
JButton s=new JButton("ha");
a(){
this.setTitle("a!");
this.setVisible(true);
this.setLayout(null);
this.setBounds(100,100,600,600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(s);
s.setBounds(100,100,100,50);
}
}
class b extends JFrame{
//第二个按钮
JButton s=new JButton("ha");
b(){
this.setTitle("b!");
this.setVisible(true);
this.setLayout(null);
this.setBounds(100,100,600,600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(s);
s.setBounds(100,100,100,50);
}
}
class c implements ActionListener{
//启动a,b
a aa=new a();
b bb=new b();
c(){
//为两个按钮加监听器
aa.s.addActionListener(this);
bb.s.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
//如果是a的按钮
if(e.getSource()==aa.s){
System.out.println("我点了a的s");
}
//如果是b的按钮
if(e.getSource()==bb.s){
System.out.println("我点了b的s");
}
}
}