java语言的科学与艺术-编程练习---创建简单的GUI
1 import acm.graphics.*; 2 import acm.program.*; 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 /** 7 * This program creates a five-pointed star every time the 8 * user clicks the mouse on the canvas. 9 * @author Administrator 10 * 11 */ 12 public class DrawStarMap extends GraphicsProgram { 13 /* Initializes the mouse listeners */ 14 public void init(){ 15 add(new JButton("Clear"), SOUTH); 16 fillCheckBox = new JCheckBox("Filled"); 17 fillCheckBox.setSelected(true); 18 add(fillCheckBox, SOUTH); 19 20 smallButton = new JRadioButton("Small"); 21 mediumButton = new JRadioButton("Medium"); 22 largeButton = new JRadioButton("Large"); 23 ButtonGroup sizeGroup = new ButtonGroup(); 24 sizeGroup.add(smallButton); 25 sizeGroup.add(mediumButton); 26 sizeGroup.add(largeButton); 27 mediumButton.setSelected(true); 28 add(smallButton, SOUTH); 29 add(mediumButton, SOUTH); 30 add(largeButton, SOUTH); 31 32 addActionListeners(); 33 addMouseListeners(); 34 } 35 public double getCurrentSize(){ 36 if (smallButton.isSelected()) return SMALL_SIZE; 37 if (largeButton.isSelected()) return LARGE_SIZE; 38 return MEDIUM_SIZE; 39 } 40 public void actionPerformed(ActionEvent e){ 41 if (e.getActionCommand().equals("Clear")) removeAll(); 42 } 43 /* Called whenever the user clicks the mouse */ 44 public void mouseClicked(MouseEvent e){ 45 GStar star = new GStar(getCurrentSize()); 46 star.setFilled(fillCheckBox.isSelected()); 47 add(star, e.getX(), e.getY()); 48 } 49 /* Private constants */ 50 private static final double SMALL_SIZE = 10.0; 51 private static final double MEDIUM_SIZE = 20.0; 52 private static final double LARGE_SIZE = 30.0; 53 54 private JCheckBox fillCheckBox; 55 private JRadioButton smallButton, mediumButton, largeButton; 56 }
posted on 2012-12-17 20:10 mybluecode 阅读(254) 评论(0) 编辑 收藏 举报