给按钮添加了鼠标事件,单击在界面上添加当前按钮的左子按钮,双击添加右字按钮。运行时,有的按钮可以正常的显示,有的不可以,求各位助攻!!!(用了eclipse的swt)
import java.awt.Color;
import java.awt.Event;
import java.awt.Point;
import java.util.Scanner;import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JButton;import org.eclipse.swt.widgets.Display;import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;public class shArea extends JPanel {
JRootPane jframe=null;Point point =null;
Drawing draw;
int index=0;
JButton bus[]=new JButton[50];
BinaryTree bt[]=new BinaryTree [50];
public shArea(JRootPane jr) {
super();
setBackground(Color.CYAN);
setSize(782, 526);
setLayout(null);

final JButton btRoot = new JButton("");
btRoot.setForeground(Color.CYAN);
btRoot.setBackground(Color.CYAN);
btRoot.setIcon(new ImageIcon(shArea.class.getResource("/icon/Root.png")));
btRoot.setBounds(321, 10, 46,52);//  46,52
bt[0]=new BinaryTree();
bt[0].x=btRoot.getX();
bt[0].y=btRoot.getY();
bus[0]=btRoot;
bt[0].d=0;bt[0].D=0;bt[0].in=1;
btRoot.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
switch(e.getClickCount()){
case 1:{
if(bt[0].ifadd)
btRoot.disable();
bt[0].L=new BinaryTree();
bt[0].L.in=bt[0].in<<1;
add(addbt(bt[0].x-150,bt[0].y+100,bt[0].L.in)); 
bt[0].ifadd=true;
break;
 }
case 2:{
if(bt[0].ifadd)
btRoot.disable();
bt[0].R=new BinaryTree();
bt[0].R.in=bt[0].in<<1+1;
add(addbt(bt[0].x+150,bt[0].y+100,bt[0].R.in)); 
bt[0].ifadd=true;
break;
}
}
}
});
add(btRoot);
validate();
}

public JButton addbt(int x,int y,final int i){
bus[i]= new JButton("");
bus[i].setForeground(Color.CYAN);
bus[i].setBackground(Color.CYAN);
bus[i].setIcon(new ImageIcon(getClass().getResource("/icon/li.png")));
bus[i].setBounds(x , y , 31, 32);
bt[i]=new BinaryTree();
bt[i].x=x;
bt[i].y=y;
bus[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
switch(e.getClickCount()){
case 1:{
bt[i].L=new BinaryTree();
bt[i].L.in=bt[i].in<<1;
add(addbt(bt[i].x-30,bt[i].y+100,bt[i].L.in)); 
bt[i].ifadd=true;
break;
 }
case 2:{
bt[i].R=new BinaryTree();
bt[i].R.in=bt[i].in<<1+1;
add(addbt(bt[i].x+30,bt[i].y+100,bt[i].R.in)); 
bt[i].ifadd=true;
break;
}
}
}
});
return bus[i];
}
public void setIndex(int x){//设置index的接口
     index = x;
    }
    public int getIndex(){//设置index的接口
     return index ;
    }
}显示如下:java

解决方案 »

  1.   

    我知道问题出在 final i,但是不知道怎么改~
      

  2.   

    你可以直接将那些隐藏的按钮设置为不可以见啊,当点击button1的时候 通过传递一个boolean参数将按钮设置为可见,再次单击又可设置为不可见啊!    只是你这里处理的是单击和双击事件,但都差不多吧,我也新手你源码我就没看了,新手看代码 都比较痛苦的!