所以这是我的电影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 的切换按钮变得不可点击。
最佳答案
好的,我用这个方法解决了
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
}
}
}
}
关于java - 设置toggleButton不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37441546/