Action Listener 从未在本地使用
Action Listener never used locally
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() 方法创建一个简单的图形
您有两个 ActionListener,MyActionListener
和 myActionListener
,并且您只使用 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("");
}
}
}
}
编辑:
通过 naming convenction 在 java
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
所以我建议你使用MyActionListener
instaead myActionListener
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() 方法创建一个简单的图形
您有两个 ActionListener,MyActionListener
和 myActionListener
,并且您只使用 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("");
}
}
}
}
编辑: 通过 naming convenction 在 java
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
所以我建议你使用MyActionListener
instaead myActionListener