********* AdapterDemo.java*********
import java.awt.event.*;
import javax.swing.JFrame;
public class AdapterDemo extends JFrame{
public static void main(String[] args){
AdapterDemo frame = new AdapterDemo();
frame.setSize(220, 80);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("适配器演示");
frame.setVisible(true);
}
public AdapterDemo(){
addWindowListener(
new WindowAdapter(){
public void windowActivated(WindowEvent event){
System.out.println("窗口激活");
}
}
);
}
}
*********** AnimationDemo.java*****
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimationDemo extends JFrame{
public AnimationDemo(){
add(new MovingMessagePanel("消息移动"));
}
public static void main(String[] args){
AnimationDemo frame = new AnimationDemo();
frame.setSize(280, 100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("消息移动演示");
frame.setVisible(true);
}
static class MovingMessagePanel extends JPanel {
private String message = "Java欢迎您";
private int xCoordinate = 0;
private int yCoordinate = 20;
public MovingMessagePanel(String message){
ssage = message;
Timer timer = new Timer(1000, new TimerListener());
timer.start();
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (xCoordinate > getWidth()) {
xCoordinate = -20;
}
xCoordinate += 5;
g.drawString(message, xCoordinate, yCoordinate);
}
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
}
}//class TimerListener implements ActionListener
}// static class MovingMessagePanel extends JPanel
}//public class AnimationDemo extends JFrame
********* AnonymousListenerDemo.java********
import javax.swing.*;
import java.awt.event.*;
public class AnonymousListenerDemo extends JFrame{
public AnonymousListenerDemo(){
JButton jbtNew = new JButton("新建");
JButton jbtOpen = new JButton("打开");
JButton jbtSave = new JButton("保存");
JButton jbtPrint = new JButton("打印");
JPanel panel = new JPanel();
panel.add(jbtNew);
panel.add(jbtOpen);
panel.add(jbtSave);
panel.add(jbtPrint);
add(panel);
jbtNew.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("响应新建按钮被点击的动作");
}
}
);
jbtOpen.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("响应打开按钮被点击的动作");
}
}
);
jbtSave.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("响应保存按钮被点击的动作");
}
}
);
jbtPrint.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("响应打印按钮被点击的动作");
}
javaweb编程技术 }
);
}//public class AnonymousListenerDemo extends JFrame
public static void main(String[] args){
JFrame frame = new AnonymousListenerDemo();
frame.setTitle("匿名内部类演示");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();//依据放在框架中组件的大小来自动调整框架的大小
frame.setVisible(true);
}
}
************* ClockAnimation.java*******
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
public class ClockAnimation extends JFrame{
private StillClock clock = new StillClock();
public ClockAnimation(){
add(clock);
Timer timer = new Timer(1000, new TimerListener());
timer.start();
}
private class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
clock.setCurrentTime();
paint();
}
}
public static void main(String[] args){
JFrame frame = new ClockAnimation();
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("动态时钟");
frame.setVisible(true);
}
private class StillClock extends JPanel{
private int hour;
private int minute;
private int second;
public StillClock(){
setCurrentTime();
}
public StillClock(int hour, int minute, int second){
this.hour = hour;
this.minute = minute;
this.second = second;
}
public int getHour(){
return hour;
}
public void setHour(int hour){
this.hour = hour;
repaint();
}
public int getMinute(){
return minute;
}
public void setMinute(int minute){
this.minute = minute;
repaint();
}
public int getSecond(){
return second;
}
public void setSecond(int second){
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论