JButton, JCheckBox e interatores similares não mudam visualmente
Aqui está um programa gráfico simples que adiciona algumas estrelas na tela.
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;
}
E a classe 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;
}
}
}
O programa funciona bem e usa um construtor de classe GStar para criar uma estrela sempre que o usuário clicar com o mouse na tela. Mas há um problema: "O JCheckBox e o JButton nunca mudam visualmente!". Quando eu pressiono o botão "Clear" J, a tela fica vazia, mas o botão não parece alternar. Da mesma forma, o programa desenha estrelas cheias e vazias, mas o JCheckBox "Cheio" permanece sempre selecionado, mas não muda. O problema se torna ainda maior com o JSlider que uso em outros programas. O controle deslizante permanece sempre na posição inicial, mesmo que funcione em algum sentido: seu valor muda. Eu uso Eclipse, versão 2011 e a última biblioteca JRE (v.7u6http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1637588.html). Não encontrei informações suficientes na Internet. Qual é o problema? Obrigado pela ajuda!! O pacote acm pode ser baixado aquihttp://jtf.acm.org/acm.jar