有源代码不允许编辑,我需要添加一个稍微修改它的方面类。
以下是我所做的课程:
package ext;
import java.awt.event.ActionEvent;
import javax.swing.*;
import battleship.*;
public privileged aspect AddStrategy {
JButton playButton1 = new JButton("Play");
after(BattleshipDialog dialog): this(dialog) && execution(JPanel BattleshipDialog.makeControlPane()){
dialog.playButton.setText("Practice");
JPanel buttons = (JPanel) dialog.playButton.getParent();
buttons.add(playButton1);
playButton1.setFocusPainted(false);
playButton1.addActionListener(this::playButton1Clicked);
}
BattleshipDialog bd;
public void playButton1Clicked(ActionEvent event) {
bd.startNewGame();
}
}
我能够成功地将原始按钮“Play”重命名为“练习”。每当我点击“练习”按钮时,它都具有重新启动游戏的原始功能,并且工作正常。
但是,当我尝试使用新创建的按钮“Play”模仿该功能时,控制台会显示一堆错误,所有错误都表示blahblahblah(UnknownSource)。
我想我得到了这个问题,因为我需要扩充相同的“对话框”,我正在尝试使用“对话框”和“bd”。但是,我尝试了各种方法使它们使用相同的对话框,但没有一个工作。
TL;博士 试图让2个按钮做同样的事情,但只有1个按钮执行它,而另一个按钮向控制台抛出一堆“未知来源”错误。
我不确定它是否会有所帮助,但这是来自BattleshipDialog.java类的相关源代码:
public void playButtonClicked(ActionEvent event) {
if (isGameOver()) {
startNewGame();
} else {
if (JOptionPane.showConfirmDialog(BattleshipDialog.this,
"Play a new game?", "Battleship", JOptionPane.YES_NO_OPTION)
== JOptionPane.YES_OPTION) {
startNewGame();
}
}
}
/** Start a new game. This will terminate the current play and
* start a new play. */
private void startNewGame() {
msgBar.setText("Shots: 0");
board.reset();
placeShips();
repaint();
}
编辑:以下是根据请求的错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ext.AddStrategy.playButton1Clicked(AddStrategy.aj:30)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)