禁用JAVA上的按钮,但保留唯一按钮
根据我前面的问题: 我需要的是,唯一的按钮,出现一个以上将消失,而点击他们 问题是当点击“独一无二”的按钮(见图)时,它们也会消失。 我的代码:禁用JAVA上的按钮,但保留唯一按钮,java,awt,Java,Awt,根据我前面的问题: 我需要的是,唯一的按钮,出现一个以上将消失,而点击他们 问题是当点击“独一无二”的按钮(见图)时,它们也会消失。 我的代码: 私有字符串namesArr[]={“Yakir”、“Yarden”、“Igor”、“Maoz”、“Moshe”、“Israel”、“Tal”、“Haim”、“Nati”、“Mor”、“Daniel”、“Idan”}; 私有按钮ButtonAr[]=新按钮[NameArr.length]; 专用字体; 公共学生GUI(字符串标题){ 超级(标题); a
私有字符串namesArr[]={“Yakir”、“Yarden”、“Igor”、“Maoz”、“Moshe”、“Israel”、“Tal”、“Haim”、“Nati”、“Mor”、“Daniel”、“Idan”};
私有按钮ButtonAr[]=新按钮[NameArr.length];
专用字体;
公共学生GUI(字符串标题){
超级(标题);
addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(WindowEvent e){
处置();
系统出口(0);
}
});
这个.setLayout(新的GridLayout(3,3));
字体=新字体(“Ariel”,字体粗体,35);
对于(inti=0;i您的意思是这样的(代码可能有语法错误)
public void actionPerformed(ActionEvent e){
if(如getSource()instanceof按钮){
字符串btnText=((按钮)e.getSource()).getLabel();
List btnList=new ArrayList();
对于(int i=0;i 1){
for(Iterator it=btnList.Iterator();it.hasNext()){
this.remove(it.next());
}
包装();
}
}
}
您的意思是这样的(代码可能有语法错误)
public void actionPerformed(ActionEvent e){
if(如getSource()instanceof按钮){
字符串btnText=((按钮)e.getSource()).getLabel();
List btnList=new ArrayList();
对于(int i=0;i 1){
for(Iterator it=btnList.Iterator();it.hasNext()){
this.remove(it.next());
}
包装();
}
}
}
按照@Freddy的答案使用集合应该更好。但是,如果你想坚持使用数组,下面的方法应该可以做到(不过还没有测试过)
public void actionPerformed(ActionEvent e){
if(如getSource()instanceof按钮){
字符串btnText=((按钮)e.getSource()).getLabel();
int计数器=0;
对于(int i=0;i 1){
对于(int j=0;j,按照@Freddy的答案使用集合应该更好。但是,如果您要坚持使用数组,下面的方法应该可以做到(不过还没有测试过)
public void actionPerformed(ActionEvent e){
if(如getSource()instanceof按钮){
字符串btnText=((按钮)e.getSource()).getLabel();
int计数器=0;
对于(int i=0;i 1){
对于(int j=0;jso)你的问题和你的问题或错误到底是什么?如何只让那些重复自己的人消失?独特的人需要留下来,在图片中,那些需要留下来的人不管点击的是“以色列”和“丹尼尔”和“伊丹”那么,你的问题和你的问题或错误到底是什么?如何让那些重复自己的人消失?那些独特的人需要留下来,在图片中,那些需要留下来的人,不管点击的是“以色列”、“但以理”和“伊丹”谢谢!现在测试过了,效果很好!快速提问,你说的集合是什么意思?例如ArrayList
,因为它们提供了更简单的添加/删除方法等:谢谢@funkyjelly谢谢!现在测试过了,效果很好!快速提问,你说的集合是什么意思?例如ArrayList
,因为他们提供了更简单的添加/删除方法等:谢谢@funkyjelly感谢您的回答,但是@funkyjelly发布了所需内容:)谢谢!感谢您的回答,但是@funkyjelly发布了所需内容:)谢谢!
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();
}
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Button) {
String btnText = ((Button)e.getSource()).getLabel();
List<Button> btnList = new ArrayList<Button>();
for(int i=0; i<buttonArr.length; i++) {
if (buttonArr[i].getLabel().equals(btnText)) {
btnList.add(buttonArr[i]);
//this.remove(buttonArr[i]);
//pack();
}
}
if (btnList.size() > 1) {
for (Iterator<Button> it = btnList.iterator(); it.hasNext()) {
this.remove(it.next());
}
pack();
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Button) {
String btnText = ((Button)e.getSource()).getLabel();
int counter = 0;
for(int i=0; i<buttonArr.length; i++) {
if (buttonArr[i].getLabel().equals(btnText)) counter++;
if (count > 1) {
for(int j=0; j<buttonArr.length; j++) {
if (buttonArr[j].getLabel().equals(btnText))
this.remove(buttonArr[j]);
}
}
}
pack();
}
}