这是一个简单的图形程序,可以在屏幕上添加一些星星。
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
答案 0 :(得分:5)
ACM Java Task Force框架旨在“向一年级计算机学生传授Java,而不会让那些学生因其复杂性而不知所措。”为实现此目的,它以排除干扰正常JApplet
交互的方式拦截所有鼠标和键盘事件。请注意,其他示例表现出相同的行为。这个example是使用Swing API的替代方案。
附录:在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 {
...
}
}
答案 1 :(得分:2)
我正在搞清楚......
add(fillCheckBox, NORTH); // SOUTH to NORTH
add(new JButton("Clear"), NORTH); // SOUTH to NORTH
如何将位置从SOUTH
切换为NORTH
效果很好..
更新:
以及EAST
约束没有正常工作。
可能存在SOUTH
和EAST
约束的一些错误。
输出: