JButton, JCheckBox and similar interactors do not change visually

前端 未结 2 1800
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 03:07

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.sw         


        
相关标签:
2条回答
  • 2020-12-04 03:36

    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.

    enter image description here

    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 {
            ...  
        }
    }
    
    0 讨论(0)
  • 2020-12-04 03:42

    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 :

    0 讨论(0)
提交回复
热议问题