java小白求助 eclipse JButton 数组,事件监听 buttons【num】.addActionListener(this); 报错
用数组添加事件监听buttons【num】.addActionListener(this);出错应该怎么对数组添加事件啊?importjavax.swing.*;impo...
用数组添加事件监听 buttons【num】.addActionListener(this); 出错
应该怎么对数组添加事件啊?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DeckOfCards extends JFrame {
private Card deck[];
private int currentCard;
private JButton dealButton, shuffleButton;
private JLabel status;
private JPanel panel1, panel2;
private JButton[] buttons;
int num;
public DeckOfCards() {
super("Card Grid Game");
this.setSize(525, 450); // set the window size
this.setVisible(true); // show the window
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six"};
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
deck = new Card[24];
currentCard = -1;
for (int i = 0; i < deck.length; i++)
deck[i] = new Card(faces[i % 6], suits[i / 6]);
JPanel panel1 = new JPanel();
shuffleButton = new JButton("Shuffle cards");
panel1.add(shuffleButton);
shuffleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(num = 0; num <9 ;num ++)
{
buttons[num].setEnabled(true);
buttons[num].setText(""+num);
}
status.setText("SHUFFLING ...");
shuffle();
Card dealt = dealCard();
if (dealt != null)
{
status.setText(dealt.toString());
shuffleButton.setEnabled(false);
}
else
{
status.setText("SHUFFLE CARDS TO BEGIN");
}
}
});
status = new JLabel("SHUFFLE CARDS TO BEGIN");
panel1.add(status);
dealButton = new JButton("Deal card");
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(3,3));
buttons = new JButton[9];
for(num = 0; num <9 ;num ++)
{
buttons[num] = new JButton("");
panel2.add(buttons[num]);
buttons[num].setEnabled(false);
}
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North",panel1);
getContentPane().add("Center",panel2);
}
public static void main(String args[]) {
DeckOfCards app = new DeckOfCards();
}
} 展开
应该怎么对数组添加事件啊?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DeckOfCards extends JFrame {
private Card deck[];
private int currentCard;
private JButton dealButton, shuffleButton;
private JLabel status;
private JPanel panel1, panel2;
private JButton[] buttons;
int num;
public DeckOfCards() {
super("Card Grid Game");
this.setSize(525, 450); // set the window size
this.setVisible(true); // show the window
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six"};
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
deck = new Card[24];
currentCard = -1;
for (int i = 0; i < deck.length; i++)
deck[i] = new Card(faces[i % 6], suits[i / 6]);
JPanel panel1 = new JPanel();
shuffleButton = new JButton("Shuffle cards");
panel1.add(shuffleButton);
shuffleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(num = 0; num <9 ;num ++)
{
buttons[num].setEnabled(true);
buttons[num].setText(""+num);
}
status.setText("SHUFFLING ...");
shuffle();
Card dealt = dealCard();
if (dealt != null)
{
status.setText(dealt.toString());
shuffleButton.setEnabled(false);
}
else
{
status.setText("SHUFFLE CARDS TO BEGIN");
}
}
});
status = new JLabel("SHUFFLE CARDS TO BEGIN");
panel1.add(status);
dealButton = new JButton("Deal card");
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(3,3));
buttons = new JButton[9];
for(num = 0; num <9 ;num ++)
{
buttons[num] = new JButton("");
panel2.add(buttons[num]);
buttons[num].setEnabled(false);
}
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North",panel1);
getContentPane().add("Center",panel2);
}
public static void main(String args[]) {
DeckOfCards app = new DeckOfCards();
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询