设置toggleButton不可点击

时间:2016-05-25 15:26:22

标签: java mysql loops for-loop togglebutton

所以这是我的电影GUI。有30 JToggleButton。我想通过检查MySQL中的值来找出已选择的座位。如果未选择座位,则可点击,否则不可点击。

public selectSeat(String title, String day, String time) throws Exception
{
    JPanel topPanel= new JPanel(new GridLayout(1, 15));
    RectDraw rect= new RectDraw();
    rect.setPreferredSize(new Dimension(30,25)); 
    topPanel.add(rect);

      JToggleButton[] ButtonList = new JToggleButton[30];

        JPanel ButtonPanel= new JPanel(new GridLayout(5,15,45,25)); // row,col,hgap,vgap
        for(int i = 0; i < 30; i++) {
            a=i+1;
            ButtonList[i]= new JToggleButton(""+a);
            ButtonPanel.add(ButtonList[i]); 
        }
         int no= findNo(day,title,time); // get hall number
         System.out.println(no); 
         List<String> seats= checkSeat(no);  // get selected seats value
         System.out.println(seats); // [22,23]
        for(String s : seats)
        {
            for(int j = 0; j<30;j++)
            {
                if(s.contains(ButtonList[j].getText()))  // if seats label with 22 and 23
                {
                    ButtonList[j].setEnabled(false); // non-clickable
                }
            }

        }

但是,标有2,3,22和23的切换按钮变为不可点击。

3 个答案:

答案 0 :(得分:0)

好的,我用这种方式解决了

for(String s : seats) // remove [] brackets (22,23)
            {

                String[] selected=s.split(","); // remove the comma
                for (String t: selected) 
                {
                    for(int j = 0; j<30;j++)
                    {
                        if(ButtonList[j].getText().equals(t))  // if seats label with 22 and 23
                        {
                            ButtonList[j].setEnabled(false); // non-clickable
                        }
                    }
                }

            }

答案 1 :(得分:0)

您正在检查&#34; 23&#34;包含&#34; 2&#34;或&#34; 3&#34;它确实如此。摆脱外部 for循环,而只是检查ArrayList 是否包含字符串,而不是List的字符串包含子字符串:

for(int j = 0; j < ButtonList.length; j++) {
    ButtonList[j].setEnabled(!seats.contains(ButtonList[j].getText()));
}

答案 2 :(得分:0)

错误是if语句if(s.contains(ButtonList[j].getText())) 你正在检查你的字符串s是否包含你应该比较的按钮数量s.equals()