Java 我如何实现一个ActionListener?
我有这个代码,我正试图修改,以便添加一个菜单栏与'文件'等。 添加它们没有问题,添加它们的侦听器被证明是一个问题。每当我尝试使用语法Java 我如何实现一个ActionListener?,java,oop,Java,Oop,我有这个代码,我正试图修改,以便添加一个菜单栏与'文件'等。 添加它们没有问题,添加它们的侦听器被证明是一个问题。每当我尝试使用语法fileMenu1.addActionListener时(这个)我得到一个错误“不能在静态上下文中使用它”。有什么建议吗?我相信我即将完成这项工作。 这是一个多类课程。如果需要的话,我们会安排其他人 import java.awt.Container; import java.awt.GridLayout; import javax.swing.*; import
fileMenu1.addActionListener时(这个)代码>我得到一个错误“不能在静态上下文中使用它”。有什么建议吗?我相信我即将完成这项工作。
这是一个多类课程。如果需要的话,我们会安排其他人
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
}
导入java.awt.Container;
导入java.awt.GridLayout;
导入javax.swing.*;
导入java.awt.event.*;
公共类BingoMain扩展JFrame实现ActionListener{//我在这里添加了监听器
私有静态最终整数行=5;
专用静态最终int COLS=5;
private static final int MAX_BINGO=15*COLS;//15*5=75.最大宾果数
私有静态JMenuItem fileMenu1=新JMenuItem(“播放”);
私有静态JMenuItem fileMenu2=新JMenuItem(“退出”);
/**
*@param args
*
*/
公共静态void main(字符串[]args){
//询问玩家的数量,获取输入,解析它,创建那么多的宾果卡
stringplayers=JOptionPane.showInputDialog(null,“有多少玩家?(1到5名玩家)”);
int playerNums=Integer.parseInt(玩家);
JFrame myBingoGUI=newjframe();//frame
myBingoGUI.setSize(900400);
myBingoGUI.setLocation(100100);
myBingoGUI.setTitle(“宾果”);
myBingoGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
容器myContentPane=myBingoGUI.getContentPane();
JMenuBar bar=新建JMenuBar();//创建菜单栏
JMenu fileMenu=newjmenu(“文件”);//在工具栏中创建文件项
添加(文件菜单);
fileMenu.add(fileMenu1);
fileMenu.add(fileMenu2);
myBingoGUI.setJMenuBar(bar);
fileMenu1.addActionListener(此);//错误!
fileMenu2.addActionListener(this);//相同错误
设置布局(新的网格布局(0,playerNums));
BingoCard[]卡=新的BingoCard[playerNums];
对于(int i=0;i最大值宾果){
error=“Value not legal”+调用的值+”(1代码应该创建一个BingoMain
的实例。main
方法是static
,这意味着它与类的任何实例都没有关联。关键字static
表示方法、字段等与类本身关联,而不是类的实例。代码错误地假定static
main方法可以引用类本身的实例,这是不可能的,因为它是静态的
public static void main (String[] args) {
/* Omitted*/
BingoMain main = new BingoMain();
fileMenu1.addActionListener(main); //NO ERROR!
/* Omitted*/
}
代码应该创建一个BingoMain
的实例。main
方法是static
,这意味着它不与类的任何实例相关联。关键字static
表示方法、字段等与类本身相关联,而不是类的实例。代码错误地假定static
main方法可以引用类本身的实例,这是不可能的,因为它是静态的
public static void main (String[] args) {
/* Omitted*/
BingoMain main = new BingoMain();
fileMenu1.addActionListener(main); //NO ERROR!
/* Omitted*/
}
代码应该创建一个BingoMain
的实例。main
方法是static
,这意味着它不与类的任何实例相关联。关键字static
表示方法、字段等与类本身相关联,而不是类的实例。代码错误地假定static
main方法可以引用类本身的实例,这是不可能的,因为它是静态的
public static void main (String[] args) {
/* Omitted*/
BingoMain main = new BingoMain();
fileMenu1.addActionListener(main); //NO ERROR!
/* Omitted*/
}
代码应该创建一个BingoMain
的实例。main
方法是static
,这意味着它不是一个
JFrame myBingoGUI=new JFrame(); //frame
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
}
@Override
public void actionPerformed(ActionEvent evt) {
}
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();
}
}