JButton, JCheckBox and similar interactors do not change visually

北战南征 提交于 2019-11-27 16:16:32
trashgod

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.

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

I am figuring this out ....

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

how come switching the position from SOUTH to NORTH works great ..

UPDATE :
As well as EAST constraint is not properly working.
May be there is some bug with SOUTH and EAST constraints.

OUTPUT :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!