控制台程序。
语义事件与程序GUI上的组件操作有关。例如,如果选择菜单项或单击按钮,就会生成语义事件。
对组件执行操作时,例如选择菜单项或单击按钮,就会生成ActionEvent事件。在选择或取消选择某个组件时,会生成ItemEvent事件。在调整可调整的对象(如滚动条)时,就生成AdjustmentEvent事件。
上面的每一种语义事件类型都定义了相应的监听器接口,这些接口都只声明了一个方法:
1、ActionListener定义了actionPerformed(ActionEvent e)方法。
2、ItemListener定义了itemStateChanged(ItemEvent e)方法。
3、AdjustmentListener定义了adjustmentValueChanged(AdjustmentEvent e)方法。
1 // Applet to generate lottery entries 2 import javax.swing.*; 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.Random; // For random number generator 6 7 @SuppressWarnings("serial") 8 public class Lottery extends JApplet { 9 // Generate NUMBER_COUNT random selections from the VALUES array 10 private static int[] getNumbers() { 11 int[] numbers = new int[NUMBER_COUNT]; // Store for the numbers to be returned 12 int candidate = 0; // Stores a candidate selection 13 for(int i = 0; i < NUMBER_COUNT; ++i) { // Loop to find the selections 14 15 search: 16 // Loop to find a new selection different from any found so far 17 while(true) { 18 candidate = VALUES[choice.nextInt(VALUES.length)]; 19 for(int j = 0 ; j < i ; ++j) { // Check against existing selections 20 if(candidate==numbers[j]) { // If it is the same 21 continue search; // get another random selection 22 } 23 } 24 numbers[i] = candidate; // Store the selection in numbers array 25 break; // and go to find the next 26 } 27 } 28 return numbers; // Return the selections 29 } 30 31 // Initialize the applet 32 @Override 33 public void init() { 34 SwingUtilities.invokeLater( // Create interface components 35 new Runnable() { // on the event dispatching thread 36 public void run() { 37 createGUI(); 38 } 39 }); 40 } 41 42 // Create User Interface for applet 43 public void createGUI() { 44 // Set up the selection buttons 45 Container content = getContentPane(); 46 content.setLayout(new GridLayout(0,1)); // Set the layout for the applet 47 48 // Set up the panel to hold the lucky number buttons 49 JPanel buttonPane = new JPanel(); // Add the pane containing numbers 50 51 // Let's have a fancy panel border 52 buttonPane.setBorder(BorderFactory.createTitledBorder( 53 BorderFactory.createEtchedBorder(Color.cyan, 54 Color.blue), 55 "Every One a Winner!")); 56 57 int[] choices = getNumbers(); // Get initial set of numbers 58 for(int i = 0 ; i < NUMBER_COUNT ; ++i) { 59 luckyNumbers[i] = new Selection(choices[i]); 60 luckyNumbers[i].addActionListener(luckyNumbers[i]); // Button is it's own listener 61 buttonPane.add(luckyNumbers[i]); 62 } 63 content.add(buttonPane); 64 65 // Add the pane containing control buttons 66 JPanel controlPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 10)); 67 68 // Add the two control buttons 69 JButton button; // A button variable 70 Dimension buttonSize = new Dimension(100,20); // Button size 71 72 controlPane.add(button = new JButton("Lucky Numbers!")); 73 button.setBorder(BorderFactory.createRaisedBevelBorder()); 74 button.addActionListener(new HandleControlButton(PICK_LUCKY_NUMBERS)); 75 button.setPreferredSize(buttonSize); 76 77 controlPane.add(button = new JButton("Color")); 78 button.setBorder(BorderFactory.createRaisedBevelBorder()); 79 button.addActionListener(new HandleControlButton(COLOR)); 80 button.setPreferredSize(buttonSize); 81 82 content.add(controlPane); 83 } 84 85 // Class defining custom buttons showing lottery selection 86 private class Selection extends JButton implements ActionListener { 87 // Constructor 88 public Selection(int value) { 89 super(Integer.toString(value)); // Call base constructor and set the label 90 this.value = value; // Save the value 91 setBackground(startColor); 92 setBorder(BorderFactory.createRaisedBevelBorder()); // Add button border 93 setPreferredSize(new Dimension(80,20)); 94 } 95 96 // Handle selection button event 97 public void actionPerformed(ActionEvent e) { 98 // Change this selection to a new selection 99 int candidate = 0; 100 while(true) { // Loop to find a different selection 101 candidate = VALUES[choice.nextInt(VALUES.length)]; 102 if(!isCurrentSelection(candidate)) { // If it is different 103 break; // end the loop 104 } 105 } 106 setValue(candidate); // We have one so set the button value 107 } 108 // Set the value for the selection 109 public void setValue(int value) { 110 setText(Integer.toString(value)); // Set value as the button label 111 this.value = value; // Save the value 112 } 113 114 // Check the value for the selection 115 boolean hasValue(int possible) { 116 return value==possible; // Return true if equals current value 117 } 118 119 // Check the current choices 120 boolean isCurrentSelection(int possible) { 121 for(int i = 0 ; i < NUMBER_COUNT ; ++i) { // For each button 122 if(luckyNumbers[i].hasValue(possible)) { // check against possible 123 return true; // Return true for any = 124 } 125 } 126 return false; // Otherwise return false 127 } 128 129 private int value; // Value for the selection button 130 } 131 132 // Class defining a handler for a control button 133 private class HandleControlButton implements ActionListener { 134 // Constructor 135 public HandleControlButton(int buttonID) { 136 this.buttonID = buttonID; // Store the button ID 137 } 138 139 // Handle button click 140 public void actionPerformed(ActionEvent e) { 141 switch(buttonID) { 142 case PICK_LUCKY_NUMBERS: 143 int[] numbers = getNumbers(); // Get maxCount random numbers 144 for(int i = 0; i < NUMBER_COUNT; ++i) { 145 luckyNumbers[i].setValue(numbers[i]); // Set the button VALUES 146 } 147 break; 148 case COLOR: 149 Color color = new Color( 150 flipColor.getRGB()^luckyNumbers[0].getBackground().getRGB()); 151 for(int i = 0; i < NUMBER_COUNT; ++i) 152 luckyNumbers[i].setBackground(color); // Set the button colors 153 break; 154 } 155 } 156 157 private int buttonID; 158 } 159 160 final static int NUMBER_COUNT = 6; // Number of lucky numbers 161 final static int MIN_VALUE = 1; // Minimum in range 162 final static int MAX_VALUE = 49; // Maximum in range 163 final static int[] VALUES = new int[MAX_VALUE-MIN_VALUE+1]; // Array of possible VALUES 164 static { // Initialize array 165 for(int i = 0 ; i < VALUES.length ; ++i) 166 VALUES[i] = i + MIN_VALUE; 167 } 168 169 // An array of custom buttons for the selected numbers 170 private Selection[] luckyNumbers = new Selection[NUMBER_COUNT]; 171 final public static int PICK_LUCKY_NUMBERS = 1; // Select button ID 172 final public static int COLOR = 2; // Color button ID 173 174 // swap colors 175 Color flipColor = new Color(Color.YELLOW.getRGB()^Color.RED.getRGB()); 176 177 Color startColor = Color.YELLOW; // start color 178 179 private static Random choice = new Random(); // Random number generator 180 }
还需要一个HTML文件:
1 <html> 2 <head> 3 </head> 4 <body bgcolor="000000"> 5 <center> 6 <applet 7 code = "Lottery.class" 8 width = "300" 9 height = "200" 10 > 11 </applet> 12 </center> 13 </body> 14 </html>