我正在写一个 2x2 棋盘。我有代码在我按下它的地方工作,但说我在底部网格中我想禁用这个按钮我该怎么做,因为我想禁用某些网格的某些按钮,但不是相同的按钮所有的网格
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class assignment1 extends JFrame implements ActionListener {
JPanel board, buttons;
JLabel G1, G2, G3, G4;
JButton B1, B2, B3, B4, B5, B6, B7, B8, B9;
ImageIcon i1 = new ImageIcon("Images/pawn.jpg");
ImageIcon a1 = new ImageIcon("Images/left_up.png");
ImageIcon a2 = new ImageIcon("Images/up.png");
ImageIcon a3 = new ImageIcon("Images/right_up.png");
ImageIcon a4 = new ImageIcon("Images/left.png");
ImageIcon a5 = new ImageIcon("Images/reset.png");
ImageIcon a6 = new ImageIcon("Images/right.png");
ImageIcon a7 = new ImageIcon("Images/left_down.png");
ImageIcon a8 = new ImageIcon("Images/down.png");
ImageIcon a9 = new ImageIcon("Images/right_down.png");
public static void main(String[]args){
assignment1 frame = new assignment1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public assignment1() {
super("Chess Board");
Container c = getContentPane();
c.setLayout(new GridLayout (2,3));
board = new JPanel();
board.setLayout(new GridLayout(2,2,4,4));
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(3,3,2,2));
G1 = new JLabel(i1);
G1.setBackground(Color.white);
G1.setOpaque(true);
G1.setIcon(i1);
board.add(G1);
G2 = new JLabel(i1);
G2.setBackground(Color.black);
G2.setOpaque(true);
G2.setIcon(null);
board.add(G2);
G3 = new JLabel(i1);
G3.setBackground(Color.black);
G3.setOpaque(true);
G3.setIcon(null);
board.add(G3);
G4 = new JLabel(i1);
G4.setBackground(Color.white);
G4.setOpaque(true);
G4.setIcon(null);
board.add(G4);
c.add(board);
//c.add(board.BorderLayout.NORTH);
c.add(buttons);
B1 = new JButton(a1);
buttons.add(B1);
B2 = new JButton(a2);
buttons.add(B2);
B3 = new JButton(a3);
buttons.add(B3);
B4 = new JButton(a4);
buttons.add(B4);
B5 = new JButton(a5);
buttons.add(B5);
B6 = new JButton(a6);
buttons.add(B6);
B7 = new JButton(a7);
buttons.add(B7);
B8 = new JButton(a8);
buttons.add(B8);
B9 = new JButton(a9);
buttons.add(B9);
B1.addActionListener(this);
B2.addActionListener(this);
B3.addActionListener(this);
B4.addActionListener(this);
B5.addActionListener(this);
B6.addActionListener(this);
B7.addActionListener(this);
B8.addActionListener(this);
B9.addActionListener(this);
setVisible(true);
setSize(500,500);
}
public void actionPerformed(ActionEvent e) {
//if statement for Grid 1 Button 6 Right
if (G1.getIcon() == i1)
{
if(e.getSource() == B6)
{
G1.setIcon(null);
G2.setIcon(i1);
}
}
//if statement for Grid 1 Button 9 Right_down
if (G1.getIcon() == i1)
{
if(e.getSource() == B9)
{
G1.setIcon(null);
G4.setIcon(i1);
}
}
//if statement for Grid 1 Button 8 Down
if (G1.getIcon() == i1)
{
if(e.getSource() == B8)
{
G1.setIcon(null);
G3.setIcon(i1);
}
}
//if statement for Grid 2 Button 4 Left
if(G2.getIcon() == i1)
{
if(e.getSource() == B4)
{
G2.setIcon(null);
G1.setIcon(i1);
}
}
//if statement for Grid 2 Button 7 left Down
if(G2.getIcon() == i1)
{
if(e.getSource() == B7)
{
G2.setIcon(null);
G3.setIcon(i1);
}
}
//if statement for Grid 2 Button 8 Down
if(G2.getIcon() == i1)
{
if(e.getSource() == B8)
{
G2.setIcon(null);
G4.setIcon(i1);
}
}
//if statement for Grid 3 Button 2 Up
if(G3.getIcon() == i1)
{
if(e.getSource() == B2)
{
G3.setIcon(null);
G1.setIcon(i1);
}
}
//if statement for Grid 3 Button 6 right
if(G3.getIcon() == i1)
{
if(e.getSource() == B6)
{
G3.setIcon(null);
G4.setIcon(i1);
}
}
//if statement for Grid 3 Button 3 right Up
if(G3.getIcon() == i1)
{
if(e.getSource() == B3)
{
G3.setIcon(null);
G2.setIcon(i1);
}
}
//if statement for Grid 4 Button 2 Up
if(G4.getIcon() == i1)
{
if(e.getSource() == B2)
{
G4.setIcon(null);
G2.setIcon(i1);
}
}
//if statement for Grid 4 Button 4 Left
if(G4.getIcon() == i1)
{
if(e.getSource() == B4)
{
G4.setIcon(null);
G3.setIcon(i1);
}
}
//if statement for Grid 4 Button 1 Left Up
if(G4.getIcon() == i1)
{
if(e.getSource() == B1)
{
G4.setIcon(null);
G1.setIcon(i1);
}
}
//reset Button
if (e.getSource() == B5)
{
G1.setIcon(i1);
G2.setIcon(null);
G3.setIcon(null);
G4.setIcon(null);
}
}
}