ChangeListener未检测到更新的变量

时间:2013-01-25 14:03:29

标签: java swing changelistener

创建了2组4个按钮。

第一组按钮的ImageIcon将被拖到第二组按钮上。

布尔enab变量用于确定是否启用了第2组4个按钮。 我最初将布尔值设置为false,仅当第一组按钮中的ImageIcon被拖过时,enab值才会更改为true

然而,当我将图像拖到它上面时,第二组按钮仍处于禁用状态。

我插入了System.out.println语句以查看它出错的地方,结果是enab变量已更新,但stateChanged方法根本没有运行。

如何让stateChanged方法检测到它所侦听的按钮已经启用?

public class Test extends JFrame {
    private boolean enab = false;
    private JButton[] button = new JButton[4];
    private JButton[] rg = new JButton[4];
    private JPanel leftg;
    private String id,id2;
    private int cc,cc2;
    private String name1;
    private BufferedImage bi;

    public Test(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // creates a new panel for the splitted puzzle pieces
        leftg = new JPanel();
        leftg.setLayout(new GridLayout(2,2));
        leftg.setPreferredSize(new Dimension(600,400));

        bi = new BufferedImage(200,133,BufferedImage.TYPE_INT_RGB);

    // creates 4 buttons   
    for(int a=0; a<4; a++){

        button[a] = new JButton(new ImageIcon(bi));   
        TransferHandler transfer = new TransferHandler("icon");
        button[a].setTransferHandler(transfer);
         //MouseListener ml = new MouseListener(){ 
        //button[a].addMouseListener(ml);
        button[a].addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
            JButton button = (JButton)e.getSource();
            TransferHandler handle = button.getTransferHandler();
            handle.exportAsDrag(button, e, TransferHandler.COPY);
            name1 = e.getComponent().getName();
            System.out.println(name1);
            enab = true;
            System.out.println("enabled0:" + enab); // prints true
            }       

        });
            // adds the 4 buttons to panel
            leftg.add(button[a]);        
    }

// adds leftg panel into the frame      
this.add(leftg,BorderLayout.WEST);

// creates a new panel for the completed puzzle pieces
JPanel cp = new JPanel();
cp.setLayout(new GridLayout(2,2));
cp.setPreferredSize(new Dimension(600,400));

// creates another 4 buttons for stuff to be dragged to
for(int b=0; b<4; b++){

     rg[b] = new JButton();
     id2 += Integer.toString(++cc2);
     // unique name for each button as an ID
     rg[b].setName(id2); 
     TransferHandler transfer1 = new TransferHandler("icon");
     rg[b].setTransferHandler(transfer1);
     rg[b].setEnabled(enab);
     System.out.println("enabled00:" + enab); // prints false 


     ChangeListener clistener = new ChangeListener(){                 
         public void stateChanged(ChangeEvent ce) {

             JButton source = (JButton)ce.getSource();
             ButtonModel mod = source.getModel();
             System.out.println("enabled1:" + enab); // no output

               if (mod.isEnabled()){
                System.out.println("enabled2:" + enab); // no output
                    if(name1 == source.getName()){

                    }
                    else{
                        source.setIcon(null);
                    }
               }  
               else{        
               }
     };
     };
rg[b].addChangeListener(clistener);
// adds the 4 buttons to panel
cp.add(rg[b]);
}  
// adds rg panel to frame
this.add(cp,BorderLayout.EAST);

this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null);
}
public static void main(String[] args){
    new Test();  
 }

}    

0 个答案:

没有答案