禁用 JAVA 上的按钮,但保留唯一按钮

我需要在单击它们时出现的唯一按钮会消失问题是当单击“唯一”按钮(见图)时,它们也会消失。我的代码:


private String namesArr[] = {"Yakir","Yarden","Igor","Maoz","Moshe","Israel","Tal","Haim","Nati","Mor","Daniel","Idan"};

private Button buttonArr[] = new Button[namesArr.length];

private Font font;


public StudentsGUI(String caption) {

    super(caption);

    addWindowListener(new WindowAdapter(){

            public void windowClosing(WindowEvent e){

                    dispose();

                        System.exit(0);

                        }

                    }); 

    this.setLayout(new GridLayout(3,3));

    font = new Font("Ariel",Font.BOLD,35);


    for(int i=0;i<namesArr.length;i++) {

        buttonArr[i] = new Button(" "+namesArr[(int)(Math.random()*namesArr.length)]);

        buttonArr[i].setFont(font);

        buttonArr[i].addActionListener(this);

        this.add(buttonArr[i]);

    }

    setLocation(800,500);

    setVisible(true);

    pack();

}


public void actionPerformed(ActionEvent e) {

    if (e.getSource() instanceof Button) {

        String btnText = ((Button)e.getSource()).getLabel();

        for(int i=0; i<buttonArr.length; i++) {

            if (buttonArr[i].getLabel().equals(btnText)) {

                this.remove(buttonArr[i]);

                pack();

            }

        }

    }

}

这张图帮助你理解:

http://img.mukewang.com/62c5648c0001581804440147.jpg

因此,如果单击“Idan”,witch 是一个唯一名称,则不会发生任何事情,因为它只有一个实例,但是如果单击“Maoz”,则所有带有“Maoz”标题的按钮都会消失(这已经发生了)



30秒到达战场
浏览 118回答 2
2回答

HUWWW

按照@Freddy的回答使用集合应该会更好。但是,如果您要坚持使用数组,则应该执行以下操作(尽管尚未测试)public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; if (e.getSource() instanceof Button) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String btnText = ((Button)e.getSource()).getLabel();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int counter = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<buttonArr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (buttonArr[i].getLabel().equals(btnText)) counter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count > 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int j=0; j<buttonArr.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (buttonArr[j].getLabel().equals(btnText))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.remove(buttonArr[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pack();&nbsp; &nbsp; &nbsp; &nbsp; }}

月关宝盒

你的意思是这样的(代码可能有语法错误)?public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; if (e.getSource() instanceof Button) {&nbsp; &nbsp; &nbsp; &nbsp; String btnText = ((Button)e.getSource()).getLabel();&nbsp; &nbsp; &nbsp; &nbsp; List<Button> btnList = new ArrayList<Button>();&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<buttonArr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (buttonArr[i].getLabel().equals(btnText)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btnList.add(buttonArr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //this.remove(buttonArr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //pack();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (btnList.size() > 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Iterator<Button> it = btnList.iterator(); it.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.remove(it.next());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pack();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java