如何使用actionPerformed方法的结果来影响主方法java
我有一个名为Screen的类,其中包含actionPerformed方法。 我希望不同的菜单项有不同的结果:随机、攻击性和人性化 这一结果影响了主要方法,但我不确定如何将两者联系起来如何使用actionPerformed方法的结果来影响主方法java,java,class,actionlistener,main,Java,Class,Actionlistener,Main,我有一个名为Screen的类,其中包含actionPerformed方法。 我希望不同的菜单项有不同的结果:随机、攻击性和人性化 这一结果影响了主要方法,但我不确定如何将两者联系起来 public class Screen extends JFrame implements ActionListener { ActionListener listener; JMenuItem random = new JMenuItem("Random"); JMenuIte
public class Screen extends JFrame
implements ActionListener {
ActionListener listener;
JMenuItem random = new JMenuItem("Random");
JMenuItem aggressive = new JMenuItem("Aggressive");
JMenuItem human = new JMenuItem("Human");
public Screen(Board board){
//menuBar items
menu.add(random);
random.addActionListener(this);
menu.add(aggressive);
aggressive.addActionListener(this);
menu.add(human);
human.addActionListener(this);
....
//sets up board of buttons and adds actionListener to each.
....
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == random){
}
if(e.getSource() == aggressive){
}
if(e.getSource() == human){
}
//code for the board buttons - nothing to do with the menu.
//But thought it might help
if (numClicks == 0){
JButton piece = (JButton) e.getSource();
String xy = piece.getName();
String x = xy.substring(0,1);
String y = xy.substring(2,3);
FromXInt = Integer.parseInt(x);
FromYInt = Integer.parseInt(y);
System.out.println("From" + " " +FromXInt + "," + FromYInt);
}
else{
JButton piece = (JButton) e.getSource();
String xy = piece.getName();
String x = xy.substring(0,1);
String y = xy.substring(2,3);
ToXInt = Integer.parseInt(x);
ToYInt = Integer.parseInt(y);
System.out.println("To" + " " + ToXInt + "," + ToYInt);
}
numClicks++;
if (numClicks >= 2){
numClicks = 0;
}
return;
}
}
包含主方法的My类:
public class Chess{
public static void main(String [ ] args){
Screen s = new Screen(board);
// my attempt but doesn't work
if (s.actionPerformed(e) == random){
.....
package chess;
import chess.Screen.PlayStyle;
public class Chess{
public static void main(String [ ] args){
Screen s = new Screen(board);
// this attempt will work
if (s.getStyle()==PlayStyle.Random) {
...
} else if (s.getStyle()==PlayStyle.Aggressive){
...
注意:我是Java新手,仍在努力了解多个类的链接。
--------------------如果单击按钮,ActionPerformed方法也会包含事件,但我没有添加代码,因为这会使事情变得过于复杂。-您正在调用一个方法,似乎希望使用该方法返回的内容,但该方法本身不返回任何内容,即“void”。我更改了Screen类,以便该方法现在返回一些内容
public class Screen extends JFrame
implements ActionListener {
public Source actionPerformed(ActionEvent e) {
....
if(e.getSource() == random){
}
if(e.getSource() == aggressive){
}
if(e.getSource() == human){
}
return e.getSource()
}
main方法现在可以从调用中接收结果并使用它。此方法使用公共枚举,并根据用户菜单选择设置样式变量:
package chess;
//...
public class Screen extends JFrame
implements ActionListener {
private JMenuItem random = new JMenuItem("Random");
private JMenuItem aggressive = new JMenuItem("Aggressive");
private JMenuItem human = new JMenuItem("Human");
public enum PlayStyle {Random, Aggressive, Human};
private PlayStyle style;
public Screen(Board board) {
//menuBar items
menu.add(random);
random.addActionListener(this);
menu.add(aggressive);
aggressive.addActionListener(this);
menu.add(human);
human.addActionListener(this);
//....
//sets up board of buttons and adds actionListener to each.
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == random) {
style=PlayStyle.Random;
}
if (e.getSource() == aggressive) {
style=PlayStyle.Aggressive;
}
if (e.getSource() == human) {
style=PlayStyle.Human;
}
//code for the board buttons - nothing to do with the menu.
//....
}
public PlayStyle getStyle(){
return style;
}
}
这是包含主方法的类:
public class Chess{
public static void main(String [ ] args){
Screen s = new Screen(board);
// my attempt but doesn't work
if (s.actionPerformed(e) == random){
.....
package chess;
import chess.Screen.PlayStyle;
public class Chess{
public static void main(String [ ] args){
Screen s = new Screen(board);
// this attempt will work
if (s.getStyle()==PlayStyle.Random) {
...
} else if (s.getStyle()==PlayStyle.Aggressive){
...
您没有从actionPerformed返回任何内容,因此无法将其与主方法中的任何内容进行比较。如果要在actionPerformed方法中添加return e.getSource(),则可以这样做。但是我不完全理解你的程序的功能。你是否向任何onject添加了ActionListener?是否检查了e.getSource()返回的内容?是否可以添加所有代码?为什么要影响主方法?你不想影响屏幕吗?空if语句用于什么?无论选择哪个项目,屏幕都是相同的。基本上,选择的项目影响棋子移动的方式。所以我认为这意味着主方法将受到影响?我得到了错误:返回类型与ActionListener不兼容。actionPerformed(ActionEvent)@Juru它是错误的,重写的方法签名必须是same@Suspended我不知道这是一条被覆盖的消息,缺少[at]然后重写注释。@Juru ActionListener接口具有所需的签名,而且注释只是可选的,不是必需的。我知道,但我不能用代码段嗅到这一点,除非它具有该注释。它不是一个要求的唯一原因是向后兼容性,无论如何,它应该是必需的。我不需要知道actionPerformed来自ActionListener,