ActionListener从未在本地使用,我知道我缺少一些东西,但我无法弄清楚。我不明白为什么将其添加到Actionlisteners列表中后就不再使用
public class GUIinterface extends JFrame{
JMenuBar MenuBar;
JMenu file;
JMenu help;
JMenuItem load;
JMenuItem save;
JMenuItem about;
JTextField commandLine;
GraphicsPanel gp;
public GUIinterface() {
setTitle("Menu");
setSize(640,480);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
gp = new GraphicsPanel();
add(gp);
MenuBar = new JMenuBar();
file = new JMenu("File");
help = new JMenu("Help");
load = new JMenuItem("Load");
save = new JMenuItem("Save");
about = new JMenuItem("About");
commandLine = new JTextField();
file.add(load);
file.add(save);
help.add(about);
MenuBar.add(file);
MenuBar.add(help);
add(MenuBar);
add(commandLine, BorderLayout.PAGE_END);
file.addMenuListener(new myMenuListener());
load.addActionListener(new myActionListener());
save.addActionListener(new myActionListener());
about.addActionListener(new myActionListener());
setJMenuBar(MenuBar);
setVisible(true);
commandLine.addActionListener(new myActionListener());
}
private class myActionListener implements ActionListener{
private class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == about) {
System.out.println("A");
gp.about();
}
}
}
public void actionPerformed(ActionEvent e) {
System.out.println(commandLine.getText());
if (e.getSource() == commandLine) {
if (commandLine.getText().contains("penup")) {
gp.penUp();
commandLine.setText("");
} else if (commandLine.getText().contains("pendown")) {
gp.penDown();
commandLine.setText("");
} else if (commandLine.getText().contains("turnleft")) {
gp.turnLeft();
commandLine.setText("");
} else if (commandLine.getText().contains("turnright")) {
gp.turnRight();
commandLine.setText("");
} else if (commandLine.getText().contains("forward")) {
gp.forward(50);
commandLine.setText("");
} else if (commandLine.getText().contains("backward")) {
gp.forward(-50);
commandLine.setText("");
} else if (commandLine.getText().contains("black")) {
gp.setPenColour(Color.black);
commandLine.setText("");
} else if (commandLine.getText().contains("red")) {
gp.setPenColour(Color.red);
commandLine.setText("");
} else if (commandLine.getText().contains("green")) {
gp.setPenColour(Color.green);
commandLine.setText("");
}
}
}
}
private class myMenuListener implements MenuListener{
public void menuCanceled(MenuEvent arg0) {
System.out.println("cancel");
}
public void menuDeselected(MenuEvent arg0) {
System.out.println("deselect");
}
public void menuSelected(MenuEvent arg0) {
System.out.println("select");
}
}
}
按下时,侦听器应调用gp.about()方法,以创建简单的图形
答案 0 :(得分:1)
您有两个MyActionListener
和myActionListener
这两个ActionListener,而您仅使用myActionListener
尝试将两者统一为一个,就会出现类似的情况:
private class myActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println(commandLine.getText());
if (e.getSource() == about) {
System.out.println("A");
gp.about();
}
else if (e.getSource() == commandLine)
{
if (commandLine.getText().contains("penup"))
{
gp.penUp();
commandLine.setText("");
}
else if (commandLine.getText().contains("pendown"))
{
gp.penDown();
commandLine.setText("");
}
else if (commandLine.getText().contains("turnleft"))
{
gp.turnLeft();
commandLine.setText("");
}
else if (commandLine.getText().contains("turnright"))
{
gp.turnRight();
commandLine.setText("");
}
else if (commandLine.getText().contains("forward"))
{
gp.forward(50);
commandLine.setText("");
}
else if (commandLine.getText().contains("backward"))
{
gp.forward(-50);
commandLine.setText("");
}
else if (commandLine.getText().contains("black"))
{
gp.setPenColour(Color.black);
commandLine.setText("");
}
else if (commandLine.getText().contains("red"))
{
gp.setPenColour(Color.red);
commandLine.setText("");
}
else if (commandLine.getText().contains("green"))
{
gp.setPenColour(Color.green);
commandLine.setText("");
}
}
}
}
编辑: 通过Java中的naming convenction
类名应为名词,每个内部单词的首字母应大写。尝试使您的类名称保持简单和描述性。使用整个单词,避免使用首字母缩写词和缩写词(除非缩写词比长格式(如URL或HTML)使用得更广泛)。
所以我建议您使用MyActionListener
instaead myActionListener