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

mgu*_*gus 3 java swing jbutton jslider 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;
}
Run Code Online (Sandbox Code Playgroud)

和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;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

该程序工作正常,并且只要用户在画布上单击鼠标,就会使用GStar类构造函数创建一个星形.但是,有一个问题:"JCheckBox和JButton永远不会在视觉上改变!".当我按下"清除"JButton时,画布变空,但按钮似乎没有切换.类似地,程序绘制填充星和空星,但"填充"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下载

tra*_*god 5

ACM的Java工作组框架设计"教Java的第一年计算的学生,而不必通过其复杂不堪重负的学生." 为实现这一目标,它以一种排除干扰正常交互的方式拦截所有鼠标和键盘事件.请注意,其他示例表现出相同的行为.此示例是使用Swing API的替代方法.JApplet

附录:在Java 1.5下编译似乎恢复了预期的功能.

在此输入图像描述

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 {
        ...  
    }
}
Run Code Online (Sandbox Code Playgroud)