技术开发 频道

AWT摘要

     清单 6 为您提供了一个完整的示例来试验所有这些新功能。修正鼠标滚轮侦听器后,您将可以不用按钮和滚动窗格。

  清单 6 完整的示例

1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 public class AwtTest {
5   private static final Color colors[] = {
6     Color.BLACK,
7     Color.BLUE,
8     Color.CYAN,
9     Color.DARK_GRAY,
10     Color.GRAY,
11     Color.GREEN,
12     Color.LIGHT_GRAY,
13     Color.MAGENTA,
14     Color.ORANGE,
15     Color.PINK,
16     Color.RED,
17     Color.WHITE,
18     Color.YELLOW
19   };
20   static int colorCounter;
21   private static final int UP = 1;
22   private static final int DOWN = 2;
23   // See bug 4475240
24   static JButton button = new JButton();
25   static JScrollPane pane = new JScrollPane(button);
26   private static void sizeScreen(JFrame frame) {
27     Toolkit kit = Toolkit.getDefaultToolkit();
28     Dimension screenSize = kit.getScreenSize();
29     GraphicsConfiguration config = frame.getGraphicsConfiguration();
30     Insets insets = kit.getScreenInsets(config);
31     screenSize.width -= (insets.left + insets.right);
32     screenSize.height -= (insets.top + insets.bottom);
33     frame.setSize(screenSize);
34     frame.setLocation(insets.left, insets.top);
35   }
36   private static void changeBackground(JFrame frame, int direction) {
37     // See bug 4475240
38     // w/o bug, change background of getContentPane()
39     button.setBackground(colors[colorCounter]);
40     // Update counter based on direction
41     if (direction == UP) {
42       colorCounter++;
43     } else {
44       --colorCounter;
45     }
46     // Wrap colors
47     if (colorCounter == colors.length) {
48       colorCounter = 0;
49     } else if (colorCounter < 0) {
50       colorCounter = colors.length-1;
51     }
52   }
53   private static void attachMouseWheelListener(final JFrame frame) {
54     MouseWheelListener listener = new MouseWheelListener() {
55       public void mouseWheelMoved(MouseWheelEvent e) {
56         int count = e.getWheelRotation();
57         int direction = (Math.abs(count) > 0) ? UP : DOWN;
58         changeBackground(frame, direction);
59       }
60     };
61     // See bug 4475240
62     // w/o bug, add to frame
63     button.addMouseWheelListener(listener);
64   }
65   private static void attachKeyListener(final JFrame frame) {
66     KeyListener listener = new KeyAdapter() {
67       public void keyPressed(KeyEvent e) {
68         if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
69           int location = e.getKeyLocation();
70           int direction = (location == KeyEvent.KEY_LOCATION_LEFT) ?
71             UP : DOWN;
72           changeBackground(frame, direction);
73         }
74       }
75     };
76     // See bug 4475240
77     // w/o bug, add to frame
78     button.addKeyListener(listener);
79   }
80   public static void main(String args[]) {
81     JFrame frame = new JFrame("AwtTest");
82     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
83     // See bug 4475240
84     frame.getContentPane().add(button, BorderLayout.CENTER);
85     sizeScreen(frame);
86     changeBackground(frame, UP);
87     attachMouseWheelListener(frame);
88     attachKeyListener(frame);
89     frame.show();
90   }
91 }
92
0
相关文章