我有这个代码,我试图修改,以添加一个菜单栏与'文件'等。
添加它们一直没有问题,添加它们的听众证明是一个问题。每当我尝试使用语法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
}
答案 0 :(得分:2)
代码应该是BingoMain
的实例。 main
方法是static
,这意味着它不与类的任何实例相关联。关键字static
表示方法,字段等与类本身相关联,而不是类的实例。代码错误地假定static
main方法可以引用类本身的实例,这是静态的。
public static void main (String[] args) {
/* Omitted*/
BingoMain main = new BingoMain();
fileMenu1.addActionListener(main); //NO ERROR!
/* Omitted*/
}
答案 1 :(得分:1)
基本上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
您的导入......
答案 2 :(得分:0)
fileMenu1.addActionListener(this); //ERROR!
fileMenu2.addActionListener(this); //Same error
main
方法是静态方法,静态上下文中没有this
。您必须创建BingoMain
类的实例,并在其中传递该类的实例。
答案 3 :(得分:0)
你应该在类的构造函数中构建你的接口,如:
public class BingoMain extends JFrame implements ActionListener {
public BingoMain() {
// code to build your UI
fileMenu1.addActionListener(this);
// some more code
}
public static void main (String[]args){
new BingoMain();
}
}