Java 如何在实现动作侦听器时设置不同的动作命令?
退出似乎不起作用首先,不要使用“==”来比较字符串。使用equals(…)方法 退出似乎不起作用 为什么要检查“退出” 动作命令默认为按钮的文本,除非您明确设置了该命令。首先,切勿使用“==”来比较字符串。使用equals(…)方法 退出似乎不起作用 为什么要检查“退出” 动作命令默认为按钮的文本,除非您明确地设置了该命令。我从不喜欢“开关板”动作侦听器,在这种侦听器中,侦听器尝试执行所有操作,但由于难以修复的错误而可能什么也不做。我认为最好使用匿名内部类来保存简单的代码本身,或者如果更复杂,则将代码路由到其他方法,或者如果更复杂,则调用控制器的方法 例如:Java 如何在实现动作侦听器时设置不同的动作命令?,java,Java,退出似乎不起作用首先,不要使用“==”来比较字符串。使用equals(…)方法 退出似乎不起作用 为什么要检查“退出” 动作命令默认为按钮的文本,除非您明确设置了该命令。首先,切勿使用“==”来比较字符串。使用equals(…)方法 退出似乎不起作用 为什么要检查“退出” 动作命令默认为按钮的文本,除非您明确地设置了该命令。我从不喜欢“开关板”动作侦听器,在这种侦听器中,侦听器尝试执行所有操作,但由于难以修复的错误而可能什么也不做。我认为最好使用匿名内部类来保存简单的代码本身,或者如果更复杂,则
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Menu extends JFrame implements ActionListener{
// Create the components and global variables
JButton newGameButton = new JButton("New Game");
JButton instructionGameButton = new JButton("Instructions");
JButton exitButton = new JButton("Exit");
JLabel mylabel = new JLabel("Welcome to Blackjack");
public Menu()
{
// Create the window
super("ThreeButtons");
setSize(300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//Creating the container for the components and set the layout
Container content = getContentPane();
FlowLayout layout = new FlowLayout();
content.setLayout(layout);
//Adding the event listener
newGameButton.addActionListener(this);
instructionGameButton.addActionListener(this);
exitButton.addActionListener(this);
//Adding of components
content.add(mylabel);
content.add(newGameButton);
content.add(instructionGameButton);
content.add(exitButton);
setContentPane(content);
}
//Add the event handler
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand()=="New Game")
new lol4();
if (event.getActionCommand()=="Instructions")
//new Instructions();
if (event.getActionCommand()=="Quit ?")
System.exit(0);
}
public static void main (String[] args)
{
//Create an instance of my class
new Menu();
}
}
在课堂上的其他地方:
newGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newGameActionPerformed(); // delegate this to a class method
}
});
instructionGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// call a controller object's methods
if (myController != null) {
myController.instructionGameAction();
}
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Menu.this.dispose(); // simple code can be called in-line
}
});
我从来都不喜欢“开关板”动作监听器,监听器尝试做所有事情,但由于难以修复的bug而可能什么都不做。我认为最好使用匿名内部类来保存简单的代码本身,或者如果更复杂,则将代码路由到其他方法,或者如果更复杂,则调用控制器的方法
例如:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Menu extends JFrame implements ActionListener{
// Create the components and global variables
JButton newGameButton = new JButton("New Game");
JButton instructionGameButton = new JButton("Instructions");
JButton exitButton = new JButton("Exit");
JLabel mylabel = new JLabel("Welcome to Blackjack");
public Menu()
{
// Create the window
super("ThreeButtons");
setSize(300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//Creating the container for the components and set the layout
Container content = getContentPane();
FlowLayout layout = new FlowLayout();
content.setLayout(layout);
//Adding the event listener
newGameButton.addActionListener(this);
instructionGameButton.addActionListener(this);
exitButton.addActionListener(this);
//Adding of components
content.add(mylabel);
content.add(newGameButton);
content.add(instructionGameButton);
content.add(exitButton);
setContentPane(content);
}
//Add the event handler
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand()=="New Game")
new lol4();
if (event.getActionCommand()=="Instructions")
//new Instructions();
if (event.getActionCommand()=="Quit ?")
System.exit(0);
}
public static void main (String[] args)
{
//Create an instance of my class
new Menu();
}
}
在课堂上的其他地方:
newGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newGameActionPerformed(); // delegate this to a class method
}
});
instructionGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// call a controller object's methods
if (myController != null) {
myController.instructionGameAction();
}
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Menu.this.dispose(); // simple code can be called in-line
}
});
请将您的代码粘贴在这里,而不是其他网站上。StackOverflow是一个高质量问题和答案的存储库;当pastie关上门或终止旧帖子时会发生什么?这将变得(更)无用,对将来的其他人毫无帮助。谢谢我明白你的意思了,很抱歉,现在好多了??请把你的代码贴在这里,不要贴在其他网站上。StackOverflow是一个高质量问题和答案的存储库;当pastie关上门或终止旧帖子时会发生什么?这将变得(更)无用,对将来的其他人毫无帮助。谢谢我明白你的意思了我很抱歉现在好点了吗??