2019.11.14
Posted quan-2723365710
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2019.11.14相关的知识,希望对你有一定的参考价值。
动作事件
1、P255例13.22,以及P256例13.23,注意比较用 普通当前类和匿名内部类两种方式实现按钮对象的事件监听器。(必做题,20分)
代码1:
package MyAdvancedEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import MyAdvancedEvent.SimpleEvent.jbAction;
public class SimpleEvents extends JFrame implements ActionListener{
private JButton jb=new JButton("我是按钮,单击我");
public SimpleEvents() {
setLayout(null);
setBounds(100,100,300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(jb);
jb.setBounds(10, 10, 150, 30);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
jb.setText("我被单击了");
}
public static void main(String[] args) {
new SimpleEvents();
}
}
代码2:
package MyAdvancedEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import MyAdvancedEvent.SimpleEvent.jbAction;
public class SimpleEvent2 extends JFrame{
private JButton jb=new JButton("我是按钮,单击我");
public SimpleEvent2() {
setLayout(null);
setBounds(100,100,300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(jb);
jb.setBounds(10, 10, 150, 30);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jb.setText("我被单击了");
}
});
}
public static void main(String[] args) {
new SimpleEvent2();
}
}
运行截图:
鼠标事件
1、P460【例25.2】一个用来演示鼠标事件的典型示例MouseEvent_Example(必做题,20分)
代码:
package MyAdvancedEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class MouseEvent1 {
JFrame f;
JLabel l;
public MouseEvent1() {
f=new JFrame();
l=new JLabel();
f.add(l);
l.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.print("单击了鼠标按键,");
int i=e.getButton();
if(i==MouseEvent.BUTTON1) {
System.out.println("单击的是鼠标左键,");
}
if(i==MouseEvent.BUTTON2) {
System.out.println("单击的是鼠标滚轮,");
}
if(i==MouseEvent.BUTTON3) {
System.out.println("单击的是鼠标右键键,");
}
int clickcount=e.getClickCount();
System.out.println("单击次数为"+clickcount+"下");
}
public void mousePressed(MouseEvent e) {
System.out.print("鼠标按键被按下,");
int i=e.getButton();
if(i==MouseEvent.BUTTON1) {
System.out.println("按下的是鼠标左键,");
}
if(i==MouseEvent.BUTTON2) {
System.out.println("按下的是鼠标滚轮,");
}
if(i==MouseEvent.BUTTON3) {
System.out.println("按下的是鼠标右键键,");
}
}
public void mouseReleased(MouseEvent e) {
System.out.print("鼠标按键被释放,");
int i=e.getButton();
if(i==MouseEvent.BUTTON1) {
System.out.println("释放的是鼠标左键,");
}
if(i==MouseEvent.BUTTON2) {
System.out.println("释放的是鼠标滚轮,");
}
if(i==MouseEvent.BUTTON3) {
System.out.println("释放的是鼠标右键键,");
}
}
public void mouseEntered(MouseEvent e) {
System.out.println("光标移入组件");
}
public void mouseExited(MouseEvent e) {
System.out.println("光标移入组件");
}
});
f.setBounds(100, 100, 200, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MouseEvent1();
}
}
运行截图:
2、鼠标选色卡MouseEventColor,运行结果如下,通过鼠标点击不同的颜色,将窗体的背景色设置成不同。试用普通类、匿名内部类、鼠标适配器三种方式分别实例化监听器对象。
(必做题,20分)
代码1:
package MyAdvancedEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class MouseEventColor implements MouseListener{
JFrame f;
JPanel p;
Box box;
JButton[]Button=new JButton[7];
Color[]color= {Color.CYAN,Color.BLUE,Color.GRAY,Color.DARK_GRAY,Color.MAGENTA,Color.YELLOW,Color.PINK};
public MouseEventColor() {
f=new JFrame("选");
p=new JPanel();
box=Box.createHorizontalBox();
for(int i=0;i<7;i++) {
Button[i]=new JButton(" ");
Button[i].setBackground(color[i]);
Button[i].setFont(new Font("",Font.PLAIN,15));
Button[i].addMouseListener(this);
box.add(Button[i]);
}
p.add(box);
f.add(p);
f.setBounds(100, 100, 310, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MouseEventColor();
}
public void mouseClicked(MouseEvent e) {
if(e.getSource()==Button[0]) {
p.setBackground(color[0]);
}else if(e.getSource()==Button[1]) {
p.setBackground(color[1]);
}else if(e.getSource()==Button[2]) {
p.setBackground(color[2]);
}else if(e.getSource()==Button[3]) {
p.setBackground(color[3]);
}else if(e.getSource()==Button[4]) {
p.setBackground(color[4]);
}else if(e.getSource()==Button[5]) {
p.setBackground(color[5]);
}else{
p.setBackground(color[6]);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
代码2:
package MyAdvancedEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class MouseEventColor1 {
JFrame f;
JPanel p;
Box box;
JButton[]Button=new JButton[7];
Color[]color= {Color.CYAN,Color.BLUE,Color.GRAY,Color.DARK_GRAY,Color.MAGENTA,Color.YELLOW,Color.PINK};
public MouseEventColor1() {
f=new JFrame("选");
p=new JPanel();
box=Box.createHorizontalBox();
for(int i=0;i<7;i++) {
Button[i]=new JButton(" ");
Button[i].setBackground(color[i]);
Button[i].setFont(new Font("",Font.PLAIN,15));
Button[i].addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
if(e.getSource()==Button[0]) {
p.setBackground(color[0]);
}else if(e.getSource()==Button[1]) {
p.setBackground(color[1]);
}else if(e.getSource()==Button[2]) {
p.setBackground(color[2]);
}else if(e.getSource()==Button[3]) {
p.setBackground(color[3]);
}else if(e.getSource()==Button[4]) {
p.setBackground(color[4]);
}else if(e.getSource()==Button[5]) {
p.setBackground(color[5]);
}else{
p.setBackground(color[6]);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
box.add(Button[i]);
}
p.add(box);
f.add(p);
f.setBounds(100, 100, 310, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MouseEventColor1();
}
}
代码3:
package MyAdvancedEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class MouseEventColor3 {
JFrame f;
JPanel p;
Box box;
Color[]color= {Color.CYAN,Color.BLUE,Color.GRAY,Color.DARK_GRAY,Color.MAGENTA,Color.YELLOW,Color.PINK};
public MouseEventColor3() {
f=new JFrame("");
p=new JPanel();
box=Box.createHorizontalBox();
for(int i=0;i<color.length;i++) {
final JButton button=new JButton(" ");
button.setBackground(color[i]);
button.setFont(new Font("",Font.PLAIN,15));
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
p.setBackground(button.getBackground());
}
});
box.add(button);
}
p.add(box);
f.add(p);
f.setBounds(100, 100, 310, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MouseEventColor3();
}
}
运行截图:
3、鼠标事件响应练习MouseEventapp.java。当鼠标进入窗口时,窗口的标题显示“欢迎”;当鼠标离开窗口时,窗口的标题显示“再见”;鼠标在窗体单击时,就在鼠标单击所在位置显示“开始”。(必做题,20分)
代码:
package MyAdvancedEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class MouseEventapp {
JFrame f;
JLabel l;
public MouseEventapp() {
f=new JFrame("再见!");
l=new JLabel("开始");
f.add(l);
l.setVisible(false);
f.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
l.setVisible(true);
int x=e.getX(),y=e.getY()-30;
l.setBounds(x, y, 100, 20);
}
public void mouseEntered(MouseEvent e) {
f.setTitle("欢迎!");
}
public void mouseExited(MouseEvent e) {
f.setTitle("再见!");
l.setVisible(false);
}
});
f.setBounds(200, 200, 300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MouseEventapp();
}
}
运行截图:
2、鼠标移动监听练习MyMouseMotionListener.java。当鼠标在窗体内移动和拖动时,分别显示如下信息:最前面是移动或拖动次数;最后面的是发生事件时鼠标所在的x坐标和y坐标(选做题,20分)
代码:
package MyAdvancedEvent;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class MyMouseMotionLister {
JFrame f;
JLabel l;
JButton b;
JTextArea ta;
JScrollPane sp;
int count;
public MyMouseMotionLister() {
f=new JFrame("MyMouseMotionLister");
l=new JLabel("click and drag the mouse");
b=new JButton("退出");
ta=new JTextArea();
sp=new JScrollPane(ta);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
f.add(l,BorderLayout.NORTH);
f.add(sp,BorderLayout.CENTER);
f.add(b,BorderLayout.SOUTH);
f.setSize(400, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ta.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
int xx=e.getX(),yy=e.getY();
count++;
String counts=count+"",x=xx+"",y=yy+"";
String content=counts+"the mouse is drag:x="+x+",y="+y;
ta.append(content+"
");
}
public void mouseMoved(MouseEvent e) {
int xx=e.getX(),yy=e.getY();
count++;
String counts=count+"",x=xx+"",y=yy+"";
String content=counts+"the mouse is moving:x="+x+",y="+y;
ta.append(content+"
");
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==b){
System.exit(0);
}
}
});
}
public static void main(String[] args){
new MyMouseMotionLister();
}
}
运行截图:
以上是关于2019.11.14的主要内容,如果未能解决你的问题,请参考以下文章