我想要修改此代码,以添加带有“文件”等的菜单栏。
添加他们已经不是问题,事实证明添加他们的听众是一个问题。每当我尝试使用语法fileMenu1.addActionListener(this);
时,都会出现错误“无法在静态上下文中使用”。有什么建议?我相信我即将完成这项工作。
这是一个多类程序。如果需要的话会放别人的
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.*;
import java.awt.event.*;
public class BingoMain extends JFrame implements ActionListener { //I ADDED THE LISTENER HERE
private static final int ROWS = 5;
private static final int COLS = 5;
private static final int MAX_BINGO = 15 * COLS; //15*5 = 75. Max number of bingo numbers
private static JMenuItem fileMenu1 = new JMenuItem("Play");
private static JMenuItem fileMenu2 = new JMenuItem("Quit");
/**
* @param args
*
*/
public static void main (String[] args) {
//Ask for how number of players, take the input, parse it, create that many bingo cards
String players = JOptionPane.showInputDialog(null, "How many players? (1 to 5 players)");
int playerNums= Integer.parseInt(players);
JFrame myBingoGUI=new JFrame(); //frame
myBingoGUI.setSize(900, 400);
myBingoGUI.setLocation(100, 100);
myBingoGUI.setTitle("BINGO");
myBingoGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container myContentPane = myBingoGUI.getContentPane();
JMenuBar bar = new JMenuBar(); //create menu bar
JMenu fileMenu = new JMenu("File"); //create the file item in the bar
bar.add(fileMenu);
fileMenu.add(fileMenu1);
fileMenu.add(fileMenu2);
myBingoGUI.setJMenuBar(bar);
fileMenu1.addActionListener(this); //ERROR!
fileMenu2.addActionListener(this); //Same error
myContentPane.setLayout(new GridLayout(0, playerNums));
BingoCard[] cards = new BingoCard[playerNums];
for (int i = 0; i < cards.length; i++) {
cards[i] = new BingoCard("Card " + (i + 1), COLS, ROWS, MAX_BINGO / COLS);
BingoGUI bingoCard = new BingoGUI();
cards[i].addListener(bingoCard);
myContentPane.add(bingoCard);
}
myBingoGUI.setVisible(true);
System.out.println(cards[0]); //print the cards on the console
System.out.println();
/*
* Play the game:
*/
boolean winner = false; //default false value for every player
while (!winner) {
String error = "";
int calledValue = -1;
int calledColumn=-1;
do {
String calledNumber = JOptionPane.showInputDialog(null, error + " Enter a BINGO call:");
error = "";
calledColumn = -1;
calledValue = -1;
/*
* The first character of the input string is converted to a column number between 0 and 4
*/
if (Character.toUpperCase(calledNumber.charAt(0))=='B') calledColumn=0;
if (Character.toUpperCase(calledNumber.charAt(0))=='I') calledColumn=1;
if (Character.toUpperCase(calledNumber.charAt(0))=='N') calledColumn=2;
if (Character.toUpperCase(calledNumber.charAt(0))=='G') calledColumn=3;
if (Character.toUpperCase(calledNumber.charAt(0))=='O') calledColumn=4;
if (calledColumn < 0) {
error = "Called Column '" + Character.toUpperCase(calledNumber.charAt(0)) + "' must be on the BINGO card"; //if first character is not a B, I, N, G, O show message
} else { //error catching
/*
* The remainder of the input string is converted to an integer
*/
//try catch block to catch any illegal numerical values (A legal column with an illegal value within the range will still be accepted)
try {
calledValue = Integer.parseInt(calledNumber.substring(1,calledNumber.length()));
if (calledValue < 1 || calledValue > MAX_BINGO) {
error = "Value not legal " + calledValue + " (1 <= value <= " + MAX_BINGO + ")"; //error if <0 or >75 is input, values dont exist in Bingo
}
} catch (NumberFormatException nfe) {
error = "Illegal number " + calledNumber.substring(1,calledNumber.length()); //error if format is wrong (i.e B9g or N5t) cant mix letters with numbers
}
}
} while (error.length() != 0);
/*
* The array of called numbers is updated to show the number has been called.
*/
for (BingoCard card : cards) {
if (card.called(calledColumn, calledValue)) {
winner = true;
}
}
if (winner) {
for (BingoCard card : cards) {
JOptionPane.showInputDialog(null, "BINGO");
card.gameOver();
}
}
System.out.println(cards[0]);
System.out.println();
} // while
} // main
}
最佳答案
基本上this
在static
方法内没有上下文,因为没有可用的“ this
”。
相反,您需要实现ActionListener
的类的实例,奇怪的是,您从未创建过该实例。
您使用...创建一个类
public class BingoMain extends JFrame implements ActionListener {...
但是您使用...创建主框架
JFrame myBingoGUI=new JFrame(); //frame
这是一种更好的方法,但无法达到从
JFrame
扩展的目的。相反,我建议删除
extends JFrame
部分并创建一个构造函数以初始化主程序,然后将其添加到框架中。例如...
public class BingoMain implements ActionListener { //I ADDED THE LISTENER HERE
private static final int ROWS = 5;
private static final int COLS = 5;
private static final int MAX_BINGO = 15 * COLS; //15*5 = 75. Max number of bingo numbers
private JMenuItem fileMenu1 = new JMenuItem("Play");
private JMenuItem fileMenu2 = new JMenuItem("Quit");
public BingoMain() {
//Ask for how number of players, take the input, parse it, create that many bingo cards
String players = JOptionPane.showInputDialog(null, "How many players? (1 to 5 players)");
int playerNums= Integer.parseInt(players);
JFrame myBingoGUI=new JFrame(); //frame
myBingoGUI.setSize(900, 400);
myBingoGUI.setLocation(100, 100);
myBingoGUI.setTitle("BINGO");
myBingoGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container myContentPane = myBingoGUI.getContentPane();
JMenuBar bar = new JMenuBar(); //create menu bar
JMenu fileMenu = new JMenu("File"); //create the file item in the bar
bar.add(fileMenu);
fileMenu.add(fileMenu1);
fileMenu.add(fileMenu2);
myBingoGUI.setJMenuBar(bar);
fileMenu1.addActionListener(this); //ERROR!
fileMenu2.addActionListener(this); //Same error
//...
}
/**
* @param args
*
*/
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BingoMain main = new BingoMain();
}
});
} // main
}
在可能要使用多个变量实例的地方,尝试避免使用
static
变量更新
正如@peeskillet指出的那样,您的代码示例中似乎没有实现
actionPerformed
。这可能只是您的疏忽,否则将引发下一个问题。确保您添加
@Override
public void actionPerformed(ActionEvent evt) {
}
对于您的班级,也对您的进口商品
import java.event.ActionEvent
...