Java 如何区分actionPerformed中的两个不同JButton?
所以我有我的钮扣Java 如何区分actionPerformed中的两个不同JButton?,java,swing,user-interface,jbutton,actionlistener,Java,Swing,User Interface,Jbutton,Actionlistener,所以我有我的钮扣 public static JButton textYes = new JButton("Yes"); public static JButton textNo = new JButton("No"); 我的菜单 public static String choiceReroll = ""; public static String menu = ""; 还有我的main()方法 然后我有我的actionListeners textYes.addActionListener
public static JButton textYes = new JButton("Yes");
public static JButton textNo = new JButton("No");
我的菜单
public static String choiceReroll = "";
public static String menu = "";
还有我的main()方法
然后我有我的actionListeners
textYes.addActionListener(this);
textNo.addActionListener(this);
如何使它在我单击一个按钮或另一个按钮时,在menu=is.next()处输入请求用户输入的文本代码>
我还希望它只输入文本,这取决于它单击的按钮。并让它输入文本,无论哪个变量要求输入
例如:
menu=is.next()代码>
vs
choiceroll=is.next()代码>
编辑:更多信息
我还有performedAction()方法
编辑:我添加了为按钮执行的操作,但现在只显示一个
if (area == "menu") {
if(e.getSource() == textYes){
menu = "yes";
}
if(e.getSource() == textNo){
menu = "no";
}
}
但是当我点击按钮时,它不会更新,为什么?
我该如何解决这个问题
编辑:在if语句中添加了一个“test”打印输出
if ("menu".equals(area)) {
if(e.getSource() == textYes){
menu = "yes";
System.out.println("test");
}
if(e.getSource() == textNo){
menu = "no";
}
每次单击“是”按钮时打印“测试”
编辑:这是方法的其余部分:
public static void main(String[] args) throws IOException {
Greed gui = new Greed();
gui.launchFrame();
redirectSystemStreams();
Container contentPane = f.getContentPane();
contentPane.add(new Greed());
Scanner is = new Scanner(System.in);
System.out.println("Welcome to Greed...");
do {
System.out.println("Would you like to play? (yes/no)");
area = "menu";
menu = is.next();
switch (menu) {
case "yes":
jTextArea1.setText(null);
diceOne = 0;
diceTwo = 0;
diceThree = 0;
diceFour = 0;
diceFive = 0;
System.out.println("Rolling...");
Game();
break;
case "no":
System.out.println("Goodbye...");
System.exit(0);
break;
default:
invalidInput();
break;
}
} while (!"yes".equals(menu) || !"no".equals(menu));
area = "";
}
public static void Game() throws IOException {
rollDiceOne();
rollDiceTwo();
rollDiceThree();
rollDiceFour();
rollDiceFive();
//displayDice();
displayDiceValues();
f.validate();
f.repaint();
choiceRerollDice();
}
public static void choiceRerollDice() {
Scanner is = new Scanner(System.in);
do {
if (!canRerollDiceOne && !canRerollDiceTwo && !canRerollDiceThree && !canRerollDiceFour && !canRerollDiceFive) {
System.out.println("Sorry, but you may not reroll any more dice...");
displayDiceValues();
System.exit(0);
}
else {
System.out.println("Would you like to reroll any (more) dice? (yes/no)");
choiceReroll = is.next();
switch (choiceReroll) {
case "yes":
rerollDice();
break;
case "no":
//endTurn();
displayDiceValues();
f.repaint();
//calculatePlayer1Score();
//System.out.println("Thank you for playing!");
//System.out.println("Goodbye!");
System.exit(0);
default:
invalidInput();
}
}
} while (!"yes".equals(choiceReroll) || !"no".equals(choiceReroll));
}
public static void rerollDice() {
Scanner is = new Scanner(System.in);
System.out.println("Which dice would you like to reroll? (Click the box under the dice!)");
rollSel = is.next();
switch (rollSel) {
case "roll":
if (willRerollDiceOne) {
if (canRerollDiceOne) {
diceOne = 0;
rollDiceOne();
canRerollDiceOne = false;
box1.setEnabled(false);
}
else {
System.out.println("error");
}
}
else {
}
if (willRerollDiceTwo) {
if (canRerollDiceTwo) {
diceTwo = 0;
rollDiceTwo();
canRerollDiceTwo = false;
box2.setEnabled(false);
}
else {
System.out.println("error");
}
}
else {
}
if (willRerollDiceThree) {
if (canRerollDiceThree) {
diceThree = 0;
rollDiceThree();
canRerollDiceThree = false;
box3.setEnabled(false);
}
}
else {
}
if (willRerollDiceFour) {
if (canRerollDiceFour) {
diceFour = 0;
rollDiceFour();
canRerollDiceFour = false;
box4.setEnabled(false);
}
}
else {
}
if (willRerollDiceFive) {
if (canRerollDiceFive) {
diceFive = 0;
rollDiceFive();
canRerollDiceFive = false;
box5.setEnabled(false);
}
}
else {
}
box1.setSelected(false);
box2.setSelected(false);
box3.setSelected(false);
box4.setSelected(false);
box5.setSelected(false);
f.validate();
f.repaint();
break;
default:
System.out.println("Error...");
}
在您的actionPerformed
方法中,只需执行以下操作:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == textYes){
//perform action when textYes clicked
}
if(e.getSource() == textNo){
//perform action when textNo clicked
}
}
if(e.getSource() == textYes){
start("yes");
}
if(e.getSource() == textNo){
start("no");
}
与此相反:
Scanner is = new Scanner(System.in);
System.out.println("Welcome to Greed...");
do {
System.out.println("Would you like to play? (yes/no)");
area = "menu";
menu = is.next();
switch (menu) {
case "yes":
jTextArea1.setText(null);
diceOne = 0;
diceTwo = 0;
diceThree = 0;
diceFour = 0;
diceFive = 0;
System.out.println("Rolling...");
Game();
break;
case "no":
System.out.println("Goodbye...");
System.exit(0);
break;
default:
invalidInput();
break;
}
} while (!"yes".equals(menu) || !"no".equals(menu));
area = "";
您可以创建一个将输入作为参数的方法
public void start(String menu){
switch (menu) {
case "yes":
jTextArea1.setText(null);
diceOne = 0;
diceTwo = 0;
diceThree = 0;
diceFour = 0;
diceFive = 0;
System.out.println("Rolling...");
Game();
break;
case "no":
System.out.println("Goodbye...");
System.exit(0);
break;
default:
invalidInput();
break;
}
}
然后在执行的操作中,只需执行以下操作:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == textYes){
//perform action when textYes clicked
}
if(e.getSource() == textNo){
//perform action when textNo clicked
}
}
if(e.getSource() == textYes){
start("yes");
}
if(e.getSource() == textNo){
start("no");
}
在您的actionPerformed
方法中,只需执行以下操作:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == textYes){
//perform action when textYes clicked
}
if(e.getSource() == textNo){
//perform action when textNo clicked
}
}
if(e.getSource() == textYes){
start("yes");
}
if(e.getSource() == textNo){
start("no");
}
与此相反:
Scanner is = new Scanner(System.in);
System.out.println("Welcome to Greed...");
do {
System.out.println("Would you like to play? (yes/no)");
area = "menu";
menu = is.next();
switch (menu) {
case "yes":
jTextArea1.setText(null);
diceOne = 0;
diceTwo = 0;
diceThree = 0;
diceFour = 0;
diceFive = 0;
System.out.println("Rolling...");
Game();
break;
case "no":
System.out.println("Goodbye...");
System.exit(0);
break;
default:
invalidInput();
break;
}
} while (!"yes".equals(menu) || !"no".equals(menu));
area = "";
您可以创建一个将输入作为参数的方法
public void start(String menu){
switch (menu) {
case "yes":
jTextArea1.setText(null);
diceOne = 0;
diceTwo = 0;
diceThree = 0;
diceFour = 0;
diceFive = 0;
System.out.println("Rolling...");
Game();
break;
case "no":
System.out.println("Goodbye...");
System.exit(0);
break;
default:
invalidInput();
break;
}
}
然后在执行的操作中,只需执行以下操作:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == textYes){
//perform action when textYes clicked
}
if(e.getSource() == textNo){
//perform action when textNo clicked
}
}
if(e.getSource() == textYes){
start("yes");
}
if(e.getSource() == textNo){
start("no");
}
从中,您可以获得。因此,只需单击每个按钮即可识别它们。从中,您可以获得。所以,只需按每个按钮来识别它们
如何区分actionPerformed中的两个不同JButton?
我有我的行动听众
textYes.addActionListener(this);
textNo.addActionListener(this);
最干净的方法是使用单独的侦听器。您可以使用匿名内部类来实现这一点,如
textYes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// here you can do any necessary actions for the "Yes" button,
// like calling a specific method of the outer class which handles the event
}
});
textNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// here you can do any necessary actions for the "No" button,
// like calling a specific method of the outer class which handles the event
}
});
如果您仍然只想使用一个操作侦听器,那么ActionEvent
中的方法允许您访问事件源,正如@ZouZou已经提到的那样
另见
如何区分actionPerformed中的两个不同JButton?
我有我的行动听众
textYes.addActionListener(this);
textNo.addActionListener(this);
最干净的方法是使用单独的侦听器。您可以使用匿名内部类来实现这一点,如
textYes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// here you can do any necessary actions for the "Yes" button,
// like calling a specific method of the outer class which handles the event
}
});
textNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// here you can do any necessary actions for the "No" button,
// like calling a specific method of the outer class which handles the event
}
});
如果您仍然只想使用一个操作侦听器,那么ActionEvent
中的方法允许您访问事件源,正如@ZouZou已经提到的那样
另请参见。由EventObject类指定,ActionEvent是的子对象(通过java.awt.awteEvent)。
这提供了对事件来源对象的引用
或者使用setActionComman(字符串命令)
为每个JButton设置命令,然后使用getActionCommand
获取相关的操作命令
String cmd = event.getActionCommand();
由EventObject类指定,ActionEvent是的子对象(通过java.awt.awteEvent)。
这提供了对事件来源对象的引用
或者使用setActionComman(字符串命令)
为每个JButton设置命令,然后使用getActionCommand
获取相关的操作命令
String cmd = event.getActionCommand();
为每个字段分配不同的ActionListener
。我强烈建议您这样做,不要使用ActionListener
扩展包含这些字段的容器,并将其作为this
传递。它保留了封装,强制分离了关注点,并且是一个干净、可理解的代码问题
因此,每个按钮都应该添加ActionListener
,如下所示:
textYes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(Action event e) {
//logic
}
});
为每个字段分配不同的ActionListener
。我强烈建议您这样做,不要使用ActionListener
扩展包含这些字段的容器,并将其作为this
传递。它保留了封装,强制分离了关注点,并且是一个干净、可理解的代码问题
因此,每个按钮都应该添加ActionListener
,如下所示:
textYes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(Action event e) {
//logic
}
});
那有帮助!我还在底部添加了另一个编辑,如果你能检查一下,那将是惊人的!谢谢你,伙计!不要将字符串与=
进行比较。使用if(area.equals(“menu”))
ok,我修复了它,但它仍然没有更新?我做错了什么?当你点击按钮时,面积等于菜单吗?试着打印if语句之前的区域。是的,确实如此。知道为什么没有更新吗?这很有帮助!我还在底部添加了另一个编辑,如果你能检查一下,那将是惊人的!谢谢你,伙计!不要将字符串与=
进行比较。使用if(area.equals(“menu”))
ok,我修复了它,但它仍然没有更新?我做错了什么?当你点击按钮时,面积等于菜单吗?试着打印if语句之前的区域。是的,确实如此。知道为什么没有更新吗?