Java 一些组件是';对事件没有反应

Java 一些组件是';对事件没有反应,java,swing,events,focus,Java,Swing,Events,Focus,我的事件控制器类不会在所选颜色发生变化时从JColorChooser接收事件,也不会从JComponent接收KeyEvents(我用来将星星画入的组件)。我已经彻底检查过了,只是找不到问题,下面是我的代码(为了方便起见,我省略了包和导入): 公共类主{ 公共静态void main(字符串[]args){ GestorDeEventos ge=新的GestorDeEventos(); Interfaz f=新的Interfaz(); 塞文塔纳(f); f、 setGestorEventos(通用

我的
事件控制器
类不会在所选颜色发生变化时从
JColorChooser
接收事件,也不会从
JComponent
接收
KeyEvents
(我用来将星星画入的组件)。我已经彻底检查过了,只是找不到问题,下面是我的代码(为了方便起见,我省略了包和导入):

公共类主{
公共静态void main(字符串[]args){
GestorDeEventos ge=新的GestorDeEventos();
Interfaz f=新的Interfaz();
塞文塔纳(f);
f、 setGestorEventos(通用电气);
}
}
公共类Interfaz扩展JFrame{
私人管理公司;
私人货柜c;
迪布霍斯·连佐地区;
私人JLabel lInfo、lCantidadPuntas、lTamanioEstrella、lLargoPuntas、lColor、lRotar;
JSpinner喷丝板;
金丝雀;
双滑块滑块;
JColorChooser ccColor;
布顿·布罗塔里兹基尔达,布罗塔德里查;
公共教育{
setTitle(“Ejempo版本1.1(07/02/2013)”;
设置大小(1024720);
可设置大小(假);
c=getContentPane();
c、 挫折背景(新颜色(240250245));
c、 setLayout(空);
lienzo=新的迪比约斯地区(600600);
lienzo.设定位置(400,15);
c、 添加(lienzo);
//国际社会秘书处提案的非正式文件
lInfo=新的JLabel(“赞成,请选择estrella a dibujar的参数:”);
林佛.立根(10,10350,20);
c、 添加(lInfo);
//埃斯特雷拉角酒店
lCantidadPuntas=新的JLabel(“estrella的数字”);
立根(10,35350,20);
c、 添加(lCantidadPuntas);
喷丝头Cantidadpuntas=新的JSpinner(新喷丝头编号模型(7,//valor inicial
Estrella.MINIMO_PUNTAS,//valor MINIMO
Estrella.MAXIMO_PUNTAS,//valor MAXIMO
1//递增/递减(paso)
)
);
喷丝头斜接(10,55120,20);
/*
这是一条非常重要的路线,它是一条通往和平之路的路线
这是一个独奏曲,由波托涅音乐中心演奏,包括艾尔·普罗皮纳、埃斯托·洛哈戈
阿尔圭岛上的一个小村庄[埃斯特雷拉.小村庄,埃斯特雷拉.大村庄]
*/
((JSpinner.DefaultEditor)spinnerCantidadPuntas.getEditor()).getTextField().setEditable(false);
c、 添加(喷丝头cantidadpuntas);
//埃斯特雷拉之家酒店
lTamanioEstrella=新JLabel(“Tamaño de la estrella(像素):”;
根瘤菌(10,80350,20);
c、 添加(ltamaniostrella);
sliderTamanioEstrella=新的JSlider(
JSlider.HORIZONTAL,
TAMANIO_ESTRELLA_MINIMO区,
塔马尼奥·埃斯特雷拉·马克西莫地区,
(国际)(塔马尼奥·埃斯特雷拉·马克西莫地区/2)
);
Hashtable etiquetasDelSlider1=新的Hashtable();
int i、cantidadDeRayas、rango、paso、minimo、maximo、valorDeLaRaya;
最小值=最小值;
maximo=区域DIBUJOS.TAMANIO_ESTRELLA_maximo;
rango=最大最小值;
cantidadDeRayas=10;
paso=兰戈/坎蒂达德雷亚斯;
对于(i=0;i您的第一个问题(颜色更改事件)与以下事实有更多关系:尽管您已将侦听器添加到颜色选择器的选择模型中,但预期事件来自颜色选择器

public void stateChanged(ChangeEvent e) {

    System.out.println("!! Ping !!" + e.getSource());

    if (e.getSource() == ventana.sliderLargoPuntas) {
        dibujo.setLargoDePuntas(ventana.sliderLargoPuntas.getScaledValue());
    } else if (e.getSource() == ventana.sliderTamanioEstrella) {
        dibujo.setTamanio(ventana.sliderTamanioEstrella.getValue());

    } else if (e.getSource() == ventana.spinnerCantidadPuntas) {
        dibujo.setCantidadPuntas((int) ventana.spinnerCantidadPuntas.getValue());

    // >>--- Change this ------------------------------------------------------------<<
    //} else if (e.getSource() == ventana.ccColor) {
    } else if (e.getSource() == ventana.ccColor.getSelectionModel()) {

        System.out.println("!! Pong !!");
        dibujo.setColor(ventana.ccColor.getColor());
    }
    areaDeTrabajo.pintarEstrella(dibujo);
}
public void stateChanged(变更事件e){
System.out.println(!!Ping!!“+e.getSource());
if(e.getSource()==ventana.sliderArgopuntas){
setLargoDePuntas(ventana.sliderArgopuntas.getScaledValue());
}else if(e.getSource()==ventana.sliderTamanioEstrella){
setTamanio(ventana.sliderTamanioEstrella.getValue());
}else if(e.getSource()==ventana.spinnerCantidadPuntas){
setCantidadPuntas((int)ventana.spinnerCantidadPuntas.getValue());

//>>---改变这个---------------------------------------------再一次,你给了我一个完美的答案,我真的很感激,至少我今天帮助了一个人:Dty:)下周我们将在另一个问题中再次见面。相关问题:a)永远不要进行任何手动大小调整/定位,而是使用合适的布局管理器b)对于自定义绘制,不要覆盖绘制,而是覆盖绘制组件c)不要使用键侦听器,而是使用键绑定手动大小调整/定位,即组件的绝对定位,为您提供对组件位置的精确控制,加上swing组件的轻量级特性,使您的GUI在所有操作系统上看起来完全相同。“不要手动调整大小/定位”这句话背后的原因是什么?这样做的好处是什么?绝对定位的缺点是什么?为什么在这样的应用程序中,您会建议不要这样做?使您的GUI在所有错误的操作系统上看起来完全相同(想想硬件:f.i.dpi、OS/LAF特定字体、边框、边距……)好的,我会告诉你的,但我要求你给我一个合理的理由,在这个“永远不要做任何手动尺寸/定位”,因为我在一所大学学习,如果没有确凿的证据,我习惯于无视这样的陈述。请给我解释一下布局管理器的好处,绝对定位的缺点,以及为什么绝对定位不适合这种应用程序哇,一个大学生,多酷啊-那么你已经习惯于做一些研究了,不是吗:-)
public void stateChanged(ChangeEvent e) {

    System.out.println("!! Ping !!" + e.getSource());

    if (e.getSource() == ventana.sliderLargoPuntas) {
        dibujo.setLargoDePuntas(ventana.sliderLargoPuntas.getScaledValue());
    } else if (e.getSource() == ventana.sliderTamanioEstrella) {
        dibujo.setTamanio(ventana.sliderTamanioEstrella.getValue());

    } else if (e.getSource() == ventana.spinnerCantidadPuntas) {
        dibujo.setCantidadPuntas((int) ventana.spinnerCantidadPuntas.getValue());

    // >>--- Change this ------------------------------------------------------------<<
    //} else if (e.getSource() == ventana.ccColor) {
    } else if (e.getSource() == ventana.ccColor.getSelectionModel()) {

        System.out.println("!! Pong !!");
        dibujo.setColor(ventana.ccColor.getColor());
    }
    areaDeTrabajo.pintarEstrella(dibujo);
}