JButton,JCheckBox和类似的交互者不会在视觉上改变 [英] JButton, JCheckBox and similar interactors do not change visually

查看:112
本文介绍了JButton,JCheckBox和类似的交互者不会在视觉上改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is a simple graphics programs which adds some stars on the screen.

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;
    }
}
}

该程序运行正常并使用用户在画布上单击鼠标时创建星形的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

The program works fine and uses a GStar class constructor to create a star whenever the user clicks the mouse on the canvas. But, there is one problem: "The JCheckBox and JButton never change visually!". When I press the "Clear" JButton the canvas becomes empty but the button does not seem to toggle. Similarly the program draws both filled and empty stars but the "Filled" JCheckBox remains always selected, it doesn't change. The problem becomes even bigger with the JSlider I use in other programs. The slider remains always at the initial position, even if it works in some sense: its value changes. I use Eclipse, 2011 version and the latest JRE library (v.7u6 http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1637588.html). I haven't found sufficient info on the Internet. What is the problem? Thank you for your help!! The acm package can be downloaded from here http://jtf.acm.org/acm.jar

推荐答案

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

The ACM Java Task Force framework is designed "to teach Java to first-year computing students without having those students overwhelmed by its complexity." To achieve this, it intercepts all mouse and keyboard events in a way that precludes interferes with normal JApplet interaction. Note that the other examples exhibit this same behavior. This example is an alternative using the Swing API.

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

Addendum: Compiling under Java 1.5 seems to restore the expected functionality.

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 {
        ...  
    }
}

这篇关于JButton,JCheckBox和类似的交互者不会在视觉上改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆