Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 电影院座椅选择和取消选择未在JButton中工作?_Java_Swing - Fatal编程技术网

Java 电影院座椅选择和取消选择未在JButton中工作?

Java 电影院座椅选择和取消选择未在JButton中工作?,java,swing,Java,Swing,当我执行这段代码时,它会显示一个按钮网格,其中只有位于网格下角的按钮可以工作,而其他按钮不能工作。当你点击一个按钮时,它将变为绿色,表示它已被选中;如果你再次点击它,它将变为白色,表示未被选中。我想做一个电影院座位预订系统,用户可以在其中选择自己的座位。我不明白为什么其他人不工作 有人能帮我吗 import javax.swing.*; import java.io.IOException; import java.awt.image.BufferedImage;

当我执行这段代码时,它会显示一个按钮网格,其中只有位于网格下角的按钮可以工作,而其他按钮不能工作。当你点击一个按钮时,它将变为绿色,表示它已被选中;如果你再次点击它,它将变为白色,表示未被选中。我想做一个电影院座位预订系统,用户可以在其中选择自己的座位。我不明白为什么其他人不工作

有人能帮我吗

    import javax.swing.*;
    import java.io.IOException;
    import java.awt.image.BufferedImage;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    import javax.swing.border.LineBorder;
    import javax.swing.border.Border;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import java.io.File;
    class cinemaSeats extends JFrame implements ActionListener { 
    private JButton[] bt = new JButton[1]; 

    static int c=4;
    static int k=5;
      public cinemaSeats() throws IOException{ 
      this.setSize(100, 100); 


      this.setLayout(null); 
     for(int s=0;s<=10;s++,k+=30)
      {
       c=4;
         for(int j=1;j<=10;j++,c+=30)
           {
              for (int i = 0; i < bt.length; i++) { 
              bt[i] = new JButton(""); 
              this.add(bt[i]); 

              bt[i].setBackground(Color.WHITE);
              bt[i].addActionListener(this); 
              bt[i].setBounds(c,k,30,30);
   } 
  }
 }
    this.setVisible(true); 
    this.pack();
    this.setSize(new Dimension(3400,735));
} 

    public void actionPerformed(ActionEvent e) { 
     for (int i = 0; i < bt.length; i++) { 
       if (e.getSource() == bt[i]) { 
       if(bt[i].getBackground() == Color.GREEN){ 
       bt[i].setBackground(Color.WHITE); 
       }else if(bt[i].getBackground() == Color.WHITE){ 
       bt[i].setBackground(Color.GREEN); 
       }else{ 
       bt[i].setBackground(Color.WHITE); 
      } 
     } 
    } 
   }
  }

    public class cinemaSeat1 { 
     public static void main()throws IOException { 
     cinemaSeats bcts = new cinemaSeats(); 
   } 
  } 
import javax.swing.*;
导入java.io.IOException;
导入java.awt.image.buffereImage;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.Color;
导入javax.swing.border.LineBorder;
导入javax.swing.border.border;
导入java.awt.image.buffereImage;
导入javax.imageio.imageio;
导入javax.swing.ImageIcon;
导入java.io.File;
类JFrame实现ActionListener{
私有JButton[]bt=新JButton[1];
静态int c=4;
静态int k=5;
公共影院座位()引发IOException{
此.setSize(100100);
此.setLayout(null);

对于(int s=0;s您的
bt
数组具有长度
1
。即使您正在创建多个
JButton
,您也只保留对其中一个(最后创建的一个)的引用。 因此,当您进入
actionPerformed
时,
if(e.getSource()==bt[i])
条件只有在按下创建的最后一个按钮时才会为
true

您必须保留对所有按钮的引用,或者您可以执行以下操作:

public void actionPerformed(ActionEvent e) {
    JButton pressedButton = (JButton)e.getSource();
    if(pressedButton.getBackground() == Color.GREEN){ 
       pressedButton.setBackground(Color.WHITE); 
    }else if(pressedButton.getBackground() == Color.WHITE){ 
       pressedButton.setBackground(Color.GREEN); 
    }else{ 
       pressedButton.setBackground(Color.WHITE); 
    } 
}