常用的事件

package 常用事件;
/**
 * 滚动条的滚动
 */
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;


import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class AdjustmentListenerDemo extends JFrame implements AdjustmentListener{
JTextArea area;
JScrollBar  jb;

public void init() {
// 定义窗体的特征
// 窗体标题
this.setTitle("我的第一个JFrame程序");
// 窗体大小
this.setSize(300, 300);
// 禁用放大按钮
//this.setResizable(false);
// 窗体位置(居中显示)
this.setLocationRelativeTo(null);
// 窗体关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public AdjustmentListenerDemo() {
init();
area = new JTextArea();
jb = new JScrollBar(JScrollBar.HORIZONTAL);
jb.addAdjustmentListener(this);
this.getContentPane().add(jb);
this.setVisible(true);
}

public static void main(String[] args) {
new AdjustmentListenerDemo();
}


@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println("==========");
}

}

---------------------------------------------------------------------

package 常用事件;
/**
 * 各种组件的焦点变化
 */
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class FocusListenerDemo extends JFrame implements FocusListener{
//下拉列表
JTextField text1,text2;
public void init() {
// 定义窗体的特征
// 窗体标题
this.setTitle("我的第一个JFrame程序");
// 窗体大小
this.setSize(300, 300);
// 禁用放大按钮
//this.setResizable(false);
// 窗体位置(居中显示)
this.setLocationRelativeTo(null);
// 窗体关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public FocusListenerDemo() {
this.init();
text1 = new JTextField(20);
text2 = new JTextField(20);

//绑定事件
text1.addFocusListener(this);
//设置布局
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
//添加组件
this.getContentPane().add(text1);
this.getContentPane().add(text2);
//显示窗体
this.setVisible(true);
}

public static void main(String[] args) {
new FocusListenerDemo();
}




@Override
public void focusGained(FocusEvent e) {
System.out.println("获取焦点");
}


@Override
public void focusLost(FocusEvent e) {
System.out.println("失去焦点");
}



}

-------------------------------------------------------------------------

package 常用事件;
/**
 * 各种按钮的点击,列表框的选择
 */
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


import javax.swing.JComboBox;
import javax.swing.JFrame;
public class ItemListenerDemo extends JFrame implements ItemListener{
//下拉列表
JComboBox box;
public void init() {
// 定义窗体的特征
// 窗体标题
this.setTitle("我的第一个JFrame程序");
// 窗体大小
this.setSize(300, 300);
// 禁用放大按钮
//this.setResizable(false);
// 窗体位置(居中显示)
this.setLocationRelativeTo(null);
// 窗体关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public ItemListenerDemo() {
this.init();
box = new JComboBox();
box.addItem("请选择");
box.addItem("大学");
box.addItem("高中");
box.addItem("初中");

//绑定事件
box.addItemListener(this);
//设置布局
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
//添加组件
this.getContentPane().add(box);
//显示窗体
this.setVisible(true);
}

public static void main(String[] args) {
new ItemListenerDemo();
}


@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
System.out.println(e.getItem());
}
}

}

-----------------------------------------------------------------------------------

package 常用事件;
/**
 * 在各种组件上敲击键盘
 */
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


import javax.swing.JFrame;
import javax.swing.JTextField;


public class KeyListenerDemo extends JFrame implements KeyListener{
//下拉列表
JTextField text1;
public void init() {
// 定义窗体的特征
// 窗体标题
this.setTitle("我的第一个JFrame程序");
// 窗体大小
this.setSize(300, 300);
// 禁用放大按钮
//this.setResizable(false);
// 窗体位置(居中显示)
this.setLocationRelativeTo(null);
// 窗体关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public KeyListenerDemo() {
this.init();
text1 = new JTextField(20);

//绑定事件
text1.addKeyListener(this);
//设置布局
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
//添加组件
this.getContentPane().add(text1);
//显示窗体
this.setVisible(true);
}

public static void main(String[] args) {
new KeyListenerDemo();
}



@Override
public void keyTyped(KeyEvent e) {
//System.out.println("键入某个键时调用此方法");
}


@Override
public void keyPressed(KeyEvent e) {
System.out.println("按下某个键时调用此方法");
}


@Override
public void keyReleased(KeyEvent e) {
//System.out.println("释放某个键时调用此方法");
}


}

----------------------------------------------------------------------

package 常用事件;
/**
 * 在任何组件上点击鼠标、将光标移动进来或出去
 */
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


import javax.swing.JFrame;
import javax.swing.JTextField;
public class MouseListenerDemo extends JFrame implements MouseListener{
//下拉列表
JTextField text1;
public void init() {
// 定义窗体的特征
// 窗体标题
this.setTitle("我的第一个JFrame程序");
// 窗体大小
this.setSize(300, 300);
// 禁用放大按钮
//this.setResizable(false);
// 窗体位置(居中显示)
this.setLocationRelativeTo(null);
// 窗体关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public MouseListenerDemo() {
this.init();
text1 = new JTextField(20);
//绑定事件
text1.addMouseListener(this);
//设置布局
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
//添加组件
this.getContentPane().add(text1);
//显示窗体
this.setVisible(true);
}

public static void main(String[] args) {
new MouseListenerDemo();
}




//单击鼠标时执行的操作  
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("单击鼠标时执行的操作  ");
}
// 鼠标在组件上按下时执行的操作  
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标在组件上按下时执行的操作  ");
}
// 鼠标释放时执行的操作  
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标释放时执行的操作  ");
}
// 鼠标进入组件时执行的操作 
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标进入组件时执行的操作 ");
}
// 鼠标离开组件时执行的操作  
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标离开组件时执行的操作  ");
}

}

--------------------------------------------------------------------------------

package 常用事件;
/**
 * 在任何组件上滚动鼠标中键
 */
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;


import javax.swing.JFrame;
import javax.swing.JTextField;
public class MouseMotionListenerDemo extends JFrame implements MouseMotionListener{
public void init() {
// 定义窗体的特征
// 窗体标题
this.setTitle("我的第一个JFrame程序");
// 窗体大小
this.setSize(300, 300);
// 禁用放大按钮
//this.setResizable(false);
// 窗体位置(居中显示)
this.setLocationRelativeTo(null);
// 窗体关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public MouseMotionListenerDemo() {
this.init();
//绑定事件
this.addMouseMotionListener(this);
//设置布局
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
//添加组件
this.setVisible(true);
}

public static void main(String[] args) {
new MouseMotionListenerDemo();
}



//鼠标被拖动
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("鼠标被拖动");
}
//鼠标在移动
@Override
public void mouseMoved(MouseEvent e) {
System.out.println("鼠标在移动");
}

}

--------------------------------------------------------------------

package 常用事件;
/**
 * 窗口得到或失去焦点
 */
import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;


import javax.swing.JFrame;
public class WindowFocusListenerDemo extends JFrame implements WindowFocusListener{
public void init() {
// 定义窗体的特征
// 窗体标题
this.setTitle("我的第一个JFrame程序");
// 窗体大小
this.setSize(300, 300);
// 禁用放大按钮
// this.setResizable(false);
// 窗体位置(居中显示)
this.setLocationRelativeTo(null);
// 窗体关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


public WindowFocusListenerDemo() {
this.init();
// 绑定事件
this.addWindowFocusListener(this);
// 设置布局
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
// 添加组件
this.setVisible(true);
}


public static void main(String[] args) {
new WindowFocusListenerDemo();
}

//该 Window 被设置为聚焦 Window 时调用
@Override
public void windowGainedFocus(WindowEvent e) {
System.out.println("该 Window 被设置为聚焦 Window 时调用");
}
//该 Window 不再是聚焦 Window 时调用
@Override
public void windowLostFocus(WindowEvent e) {
System.out.println("该 Window 不再是聚焦 Window 时调用");
}

}

----------------------------------------------------------------------

package 常用事件;
/**
 * 窗口发生变化
 */
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;


import javax.swing.JFrame;
public class WindowListenerDemo extends JFrame implements WindowListener{


public void init() {
// 定义窗体的特征
// 窗体标题
this.setTitle("我的第一个JFrame程序");
// 窗体大小
this.setSize(300, 300);
// 禁用放大按钮
// this.setResizable(false);
// 窗体位置(居中显示)
this.setLocationRelativeTo(null);
// 窗体关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


public WindowListenerDemo() {
this.init();
// 绑定事件
this.addWindowListener(this);
// 设置布局
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
// 添加组件
this.setVisible(true);
}


public static void main(String[] args) {
new WindowListenerDemo();
}


// 窗口被完全激活时调用的方法
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗口被完全激活时调用的方法");
}


// 窗口的关闭按钮被点击时调用的方法
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口的关闭按钮被点击时调用的方法");
}


// 窗口被完全关闭时调用的方法
@Override
public void windowClosed(WindowEvent e) {
System.out.println("窗口被完全关闭时调用的方法");
}


// 窗口被最小化时调用的方法
@Override
public void windowIconified(WindowEvent e) {
System.out.println("窗口被最小化时调用的方法");
}


// 窗口被从最小化还原时调用的方法
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("窗口被从最小化还原时调用的方法");
}


// 窗口被完全激活时调用的方法
@Override
public void windowActivated(WindowEvent e) {
System.out.println("窗口被完全激活时调用的方法");
}


// 窗口失去活性时调用的方法
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("窗口失去活性时调用的方法");
}

}

---------------------------------------------------------------------------

package 常用事件;
/**
 * 窗口状态改变
 */
import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowStateListener;


import javax.swing.JFrame;
import javax.swing.JFrame;
public class WindowStateListenerDemo extends JFrame implements WindowStateListener{
public void init() {
// 定义窗体的特征
// 窗体标题
this.setTitle("我的第一个JFrame程序");
// 窗体大小
this.setSize(300, 300);
// 禁用放大按钮
// this.setResizable(false);
// 窗体位置(居中显示)
this.setLocationRelativeTo(null);
// 窗体关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


public WindowStateListenerDemo() {
this.init();
// 绑定事件
this.addWindowStateListener(this);
// 设置布局
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
// 添加组件
this.setVisible(true);
}


public static void main(String[] args) {
new WindowStateListenerDemo();
}


@Override
public void windowStateChanged(WindowEvent e) {
System.out.println("窗口状态改变");
}
}

猜你喜欢

转载自blog.csdn.net/weixin_42044486/article/details/80240943