JButton,JCheckBox和类似的交互者不会在视觉上改变

这是一个简单的图形程序,可以在屏幕上添加一些星星。

import acm.graphics.*; import acm.program.*; import java.awt.event.*; import javax.swing.*; /** * This program creates a five-pointed star every time the * user clicks the mouse on the canvas. */ public class DrawStarMap1 extends GraphicsProgram { public void init() { /* Initializes the mouse listeners */ addMouseListeners(); /* The check box starts out in the "on" position */ fillCheckBox = new JCheckBox("Filled"); fillCheckBox.setSelected(true); add(fillCheckBox, SOUTH); /* Clears the screen with a button */ add(new JButton("Clear"), SOUTH); addActionListeners(); } /* Called whenever the user clicks the mouse.*/ public void mouseClicked(MouseEvent e) { GStar star = new GStar(STAR_SIZE); star.setFilled(fillCheckBox.isSelected()); add (star, e.getX(), e.getY()); } /* Removes all the graphical objects from the canvas */ public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("Clear")) removeAll(); } /* Private constants */ private static final double STAR_SIZE = 20; private JCheckBox fillCheckBox; } 

和GStar类:

 import acm.graphics.*; /** Defines a new GObject class t:hat appears as a five-pointed star. */ public class GStar extends GPolygon { /** Creates a new GStar centered at the origin with the specified * horizontal width. * @param width The width of the star */ public GStar(double width) { double dx = width / 2; double dy = dx * GMath.tanDegrees(18); double edge = width / 2 - dy * GMath.tanDegrees(36); addVertex(-dx, -dy); int angle = 0; for (int i = 0; i < 5; i++) { addPolarEdge(edge, angle); addPolarEdge(edge, angle + 72); angle -= 72; } } } 

该程序工作正常,并且只要用户在canvas上单击鼠标,就会使用GStar类构造函数创建一个星形。 但是,有一个问题:“JCheckBox和JButton永远不会在视觉上改变!”。 当我按下“清除”JButton时,canvas变空,但按钮似乎没有切换。 类似地,程序绘制填充星和空星,但“填充”JCheckBox始终保持选中状态,但不会更改。 我在其他程序中使用的JSlider问题变得更大。 滑块始终保持在初始位置,即使它在某种意义上起作用:它的值也会改变。 我使用Eclipse,2011版本和最新的JRE库(v.7u6 http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1637588.html )。 我没有在互联网上找到足够的信息。 问题是什么? 感谢您的帮助!! acm包可以从http://jtf.acm.org/acm.jar下载

ACM Java Task Force框架旨在“向一年级计算机学生传授Java,而不会让那些学生因其复杂性而不知所措。” 为实现此目的,它以一种阻止正常JApplet交互干扰的方式拦截所有鼠标和键盘事件。 请注意,其他示例表现出相同的行为。 此示例是使用Swing API的替代方法。

附录:在Java 1.5下编译似乎恢复了预期的function。

在此处输入图像描述

 import acm.graphics.GMath; import acm.graphics.GPolygon; import acm.program.*; import java.awt.event.*; import javax.swing.*; /** * This program creates a five-pointed star every time the user clicks the mouse * on the canvas. */ public class DrawStarMap extends GraphicsProgram { public void init() { addMouseListeners(); add(new JButton("ClearN"), NORTH); add(new JButton("ClearW"), WEST); add(new JButton("ClearE"), EAST); add(new JButton("ClearS"), SOUTH); addActionListeners(); } /* * Called whenever the user clicks the mouse. */ public void mouseClicked(MouseEvent e) { GStar star = new GStar(STAR_SIZE); star.setFilled(true); add(star, e.getX(), e.getY()); } /* * Removes all the graphical objects from the canvas */ public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); if (e.getActionCommand().startsWith("Clear")) { removeAll(); } } /* * Private constants */ private static final double STAR_SIZE = 20; private static class GStar extends GPolygon { ... } } 

我正在搞清楚……

 add(fillCheckBox, NORTH); // SOUTH to NORTH add(new JButton("Clear"), NORTH); // SOUTH to NORTH 

怎么把位置SOUTHNORTH切换很好..

更新:
以及EAST约束不正常工作。
可能存在一些关于SOUTHEAST约束的错误。

输出: