如果条件同时适用于9个JButton?

huangapple go评论107阅读模式
英文:

If condition for 9 Jbuttons simultaneously?

问题

  1. [if condition working for only one button at a time]
  2. 我已经编写了代码来使得9JButton在鼠标点击时随机显示其中一种8种颜色之一我添加了一个JLabel来显示结果如果所有的JButton都是相同的颜色我希望结果显示"Winner!"
  3. 以下是完整的代码
  4. ```java
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.Random;
  10. import static java.awt.Color.*;
  11. public class buttonTest {
  12. public static void main(String[] args) {
  13. new buttonTest();
  14. }
  15. public buttonTest() { //构造函数。
  16. EventQueue.invokeLater(new Runnable() {
  17. @Override
  18. public void run() {
  19. JFrame frame = new JFrame("Colour Button 4.0");
  20. frame.add(new colourButton());
  21. frame.pack();
  22. frame.setLocationRelativeTo(null);
  23. frame.setVisible(true);
  24. }
  25. });
  26. }
  27. public class colourButton extends JPanel implements ActionListener {
  28. Random rand = new Random();
  29. JButton buttons[]; // 创建一个按钮数组。
  30. JLabel gameRules = new JLabel("Match the colour buttons.");
  31. JLabel timer = new JLabel("00:00 (placeholder)");
  32. JLabel result = new JLabel("Result: (placeholder)");
  33. byte value = 0;
  34. public colourButton() {
  35. add(gameRules);
  36. buttons = new JButton[9];
  37. setLayout(new GridLayout(3, 3));
  38. for (int i = 0; i < buttons.length; i++) {
  39. buttons[i] = new JButton("colour button");
  40. buttons[i].setBorderPainted(false);
  41. buttons[i].setContentAreaFilled(false);
  42. buttons[i].setOpaque(true);
  43. buttons[i].addActionListener(this);
  44. add(buttons[i]);
  45. add(timer);
  46. add(result);
  47. setVisible(true);
  48. }
  49. }
  50. @Override
  51. public void actionPerformed(ActionEvent e) {
  52. if (!(e.getSource() instanceof JButton)) {
  53. return;
  54. }
  55. String clickedbutton = e.getActionCommand();
  56. System.out.println(clickedbutton + " button clicked.");
  57. JButton button = (JButton) e.getSource();
  58. value++;
  59. value %= 9;
  60. switch (rand.nextInt(9)) {
  61. case 0:
  62. button.setBackground(null);
  63. case 1:
  64. button.setBackground(red);
  65. break;
  66. case 2:
  67. button.setBackground(orange);
  68. break;
  69. case 3:
  70. button.setBackground(yellow);
  71. break;
  72. case 4:
  73. button.setBackground(green);
  74. break;
  75. case 5:
  76. button.setBackground(cyan);
  77. break;
  78. case 6:
  79. button.setBackground(blue);
  80. break;
  81. case 7:
  82. button.setBackground(MAGENTA);
  83. break;
  84. case 8:
  85. button.setBackground(pink);
  86. break;
  87. }
  88. if (button.getBackground() == magenta) {
  89. result.setText("magenta");
  90. } else {
  91. result.setText("placeholder result");
  92. }
  93. }
  94. }
  95. }

我认为逻辑上应该是这样的:

如果按钮数量为9,并且它们都是"这种"颜色,那么就返回这个字符串。

  1. <details>
  2. <summary>英文:</summary>
  3. [if condition working for only one button at a time][1]I have programmed 9 Jbuttons to return one of 8 colors randomly on mouse-click. I have added a Jlabel that returns the result - and I want the result to return a &quot;Winner!&quot; string if all Jbuttons are the same color.
  4. HERE&#39;S THE FULL CODE!

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import static java.awt.Color.*;

public class buttonTest {

  1. public static void main(String[] args) {
  2. new buttonTest();
  3. }
  4. public buttonTest() { //CONSTRUCTOR.
  5. EventQueue.invokeLater(new Runnable() {
  6. @Override
  7. public void run() {
  8. JFrame frame = new JFrame(&quot;Colour Button 4.0&quot;);
  9. frame.add(new colourButton());
  10. frame.pack();
  11. frame.setLocationRelativeTo(null);
  12. frame.setVisible(true);
  13. }
  14. });
  15. }
  16. public class colourButton extends JPanel implements ActionListener {
  17. Random rand = new Random();
  18. JButton buttons[]; // created a button array.
  19. JLabel gameRules = new JLabel(&quot;Match the colour buttons.&quot;);
  20. JLabel timer = new JLabel(&quot;00:00 (placeholder)&quot;);
  21. JLabel result = new JLabel(&quot;Result: (placeholder)&quot;);
  22. byte value = 0;
  23. public colourButton() {
  24. add(gameRules);
  25. buttons = new JButton[9];
  26. setLayout(new GridLayout(3, 3));
  27. for (int i = 0; i &lt; buttons.length; i++) {
  28. buttons[i] = new JButton(&quot;colour button&quot;);
  29. buttons[i].setBorderPainted(false);
  30. buttons[i].setContentAreaFilled(false);
  31. buttons[i].setOpaque(true);
  32. buttons[i].addActionListener(this);
  33. add(buttons[i]);
  34. add(timer);
  35. add(result);
  36. setVisible(true);
  37. }
  38. }
  39. @Override
  40. public void actionPerformed(ActionEvent e) {
  41. if (!(e.getSource() instanceof JButton)) {
  42. return;
  43. }
  44. String clickedbutton = e.getActionCommand();
  45. System.out.println(clickedbutton + &quot; button clicked.&quot;);
  46. JButton button = (JButton) e.getSource();
  47. value++;
  48. value %= 9;
  49. switch (rand.nextInt(9)) {
  50. case 0:
  51. button.setBackground(null);
  52. case 1:
  53. button.setBackground(red);
  54. break;
  55. case 2:
  56. button.setBackground(orange);
  57. break;
  58. case 3:
  59. button.setBackground(yellow);
  60. break;
  61. case 4:
  62. button.setBackground(green);
  63. break;
  64. case 5:
  65. button.setBackground(cyan);
  66. break;
  67. case 6:
  68. button.setBackground(blue);
  69. break;
  70. case 7:
  71. button.setBackground(MAGENTA);
  72. break;
  73. case 8:
  74. button.setBackground(pink);
  75. break;
  76. }
  77. if (button.getBackground() == magenta) {
  78. result.setText(&quot;magenta&quot;);
  79. } else {
  80. result.setText(&quot;placeholder result&quot;);
  81. }
  82. }
  83. }

}

  1. I&#39;d imagine it logically be something like:
  2. if button count is 9 and if they&#39;re all &#39;this&#39; colour, then return this string.
  3. [1]: https://i.stack.imgur.com/HzwVS.png
  4. </details>
  5. # 答案1
  6. **得分**: 0
  7. 以下是翻译好的内容:
  8. 当您创建一个Swing应用程序时,您还应该创建一个应用程序的逻辑模型。这是[model / view / controller][1]模式的一个示例。
  9. 通过将Swing应用程序的逻辑部分分开,您可以一次专注于应用程序的一个部分。
  10. 这是我测试过的GUI。正如您所见,结果文本显示为“Winner!”。
  11. [![Colour Button GUI][2]][2]
  12. 我创建了一个`GameModel`类来保存游戏模型,以及一个`ButtonModel`类来保存`JButton`的值和颜色。
  13. 测试所有九个ButtonModels的方法是`GameModel`类的`isMatch`方法。您可以看到使用模型大大简化了`ColourButton`类的`actionListener`方法。
  14. 以下是代码:
  15. ```java
  16. import java.awt.Color;
  17. import java.awt.EventQueue;
  18. import java.awt.GridLayout;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.util.Random;
  22. import javax.swing.BoxLayout;
  23. import javax.swing.JButton;
  24. import javax.swing.JFrame;
  25. import javax.swing.JLabel;
  26. import javax.swing.JPanel;
  27. public class JButtonTesting {
  28. public static void main(String[] args) {
  29. new JButtonTesting();
  30. }
  31. private GameModel model;
  32. public JButtonTesting() { // 构造函数。
  33. this.model = new GameModel();
  34. EventQueue.invokeLater(new Runnable() {
  35. @Override
  36. public void run() {
  37. JFrame frame = new JFrame("Colour Button 4.0");
  38. frame.add(new ColourButton());
  39. frame.pack();
  40. frame.setLocationRelativeTo(null);
  41. frame.setVisible(true);
  42. }
  43. });
  44. }
  45. public class ColourButton extends JPanel implements ActionListener {
  46. private static final long serialVersionUID = 1L;
  47. Random rand = new Random();
  48. JButton buttons[]; // 创建一个按钮数组。
  49. JLabel gameRules = new JLabel("匹配颜色按钮。");
  50. JLabel timer = new JLabel("00:00 (占位符)");
  51. JLabel result = new JLabel("结果:(占位符)");
  52. int value = 0;
  53. public ColourButton() {
  54. setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  55. gameRules.setAlignmentX(JLabel.CENTER_ALIGNMENT);
  56. add(gameRules);
  57. JPanel buttonPanel = createButtonPanel();
  58. add(buttonPanel);
  59. timer.setAlignmentX(JLabel.CENTER_ALIGNMENT);
  60. add(timer);
  61. result.setAlignmentX(JLabel.CENTER_ALIGNMENT);
  62. add(result);
  63. }
  64. private JPanel createButtonPanel() {
  65. JPanel buttonPanel = new JPanel();
  66. buttons = new JButton[9];
  67. buttonPanel.setLayout(new GridLayout(0, 3));
  68. for (int i = 0; i < buttons.length; i++) {
  69. buttons[i] = new JButton("颜色按钮");
  70. buttons[i].setActionCommand(Integer.toString(i));
  71. buttons[i].setBorderPainted(false);
  72. buttons[i].setContentAreaFilled(false);
  73. buttons[i].setOpaque(true);
  74. buttons[i].addActionListener(this);
  75. buttonPanel.add(buttons[i]);
  76. }
  77. return buttonPanel;
  78. }
  79. public void setButtonColour(int index, Color colour) {
  80. buttons[index].setBackground(colour);
  81. }
  82. @Override
  83. public void actionPerformed(ActionEvent event) {
  84. if (!(event.getSource() instanceof JButton)) {
  85. return;
  86. }
  87. int buttonIndex = Integer.valueOf(event.getActionCommand());
  88. System.out.println(buttonIndex + "按钮已点击。");
  89. value++;
  90. value %= 9;
  91. model.setColour(buttonIndex, value);
  92. setButtonColour(buttonIndex, model.getColour(buttonIndex));
  93. if (model.isMatch()) {
  94. result.setText("赢家!");
  95. }
  96. }
  97. }
  98. public class GameModel {
  99. private Color[] colours = { null, Color.RED, Color.ORANGE, Color.YELLOW,
  100. Color.GREEN, Color.CYAN, Color.BLUE,
  101. Color.MAGENTA, Color.PINK };
  102. private ButtonModel[] buttonValues;
  103. public GameModel() {
  104. buttonValues = new ButtonModel[9];
  105. for (int i = 0; i < buttonValues.length; i++) {
  106. buttonValues[i] = new ButtonModel();
  107. setColour(i, 0);
  108. }
  109. }
  110. public Color getColour(int index) {
  111. return buttonValues[index].getColour();
  112. }
  113. public void setColour(int index, int value) {
  114. buttonValues[index].setValue(value);
  115. buttonValues[index].setColour(colours[value]);
  116. }
  117. public boolean isMatch() {
  118. int value = buttonValues[0].getValue();
  119. for (int i = 1; i < buttonValues.length; i++) {
  120. if (buttonValues[i].getValue() != value) {
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. }
  127. public class ButtonModel {
  128. private int value;
  129. private Color colour;
  130. public int getValue() {
  131. return value;
  132. }
  133. public void setValue(int value) {
  134. this.value = value;
  135. }
  136. public Color getColour() {
  137. return colour;
  138. }
  139. public void setColour(Color colour) {
  140. this.colour = colour;
  141. }
  142. }
  143. }
英文:

When you create a Swing application, you should also create a logical model of the application. This is an example of the model / view / controller pattern.

By separating the logical portions of your Swing application, you can focus on one part of the application at a time.

Here's the GUI I tested with. As you can see, the result text says "Winner!".

如果条件同时适用于9个JButton?

I created a GameModel class to hold the game model and a ButtonModel class to hold the value and colour for a JButton.

The method to test all nine ButtonModels is the isMatch method of the GameModel class. You can see how using a model greatly simplified the actionListener method of the ColourButton class.

Here's the code.

  1. import java.awt.Color;
  2. import java.awt.EventQueue;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.util.Random;
  7. import javax.swing.BoxLayout;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. public class JButtonTesting {
  13. public static void main(String[] args) {
  14. new JButtonTesting();
  15. }
  16. private GameModel model;
  17. public JButtonTesting() { // CONSTRUCTOR.
  18. this.model = new GameModel();
  19. EventQueue.invokeLater(new Runnable() {
  20. @Override
  21. public void run() {
  22. JFrame frame = new JFrame(&quot;Colour Button 4.0&quot;);
  23. frame.add(new ColourButton());
  24. frame.pack();
  25. frame.setLocationRelativeTo(null);
  26. frame.setVisible(true);
  27. }
  28. });
  29. }
  30. public class ColourButton extends JPanel implements ActionListener {
  31. private static final long serialVersionUID = 1L;
  32. Random rand = new Random();
  33. JButton buttons[]; // created a button array.
  34. JLabel gameRules = new JLabel(&quot;Match the colour buttons.&quot;);
  35. JLabel timer = new JLabel(&quot;00:00 (placeholder)&quot;);
  36. JLabel result = new JLabel(&quot;Result: (placeholder)&quot;);
  37. int value = 0;
  38. public ColourButton() {
  39. setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  40. gameRules.setAlignmentX(JLabel.CENTER_ALIGNMENT);
  41. add(gameRules);
  42. JPanel buttonPanel = createButtonPanel();
  43. add(buttonPanel);
  44. timer.setAlignmentX(JLabel.CENTER_ALIGNMENT);
  45. add(timer);
  46. result.setAlignmentX(JLabel.CENTER_ALIGNMENT);
  47. add(result);
  48. }
  49. private JPanel createButtonPanel() {
  50. JPanel buttonPanel = new JPanel();
  51. buttons = new JButton[9];
  52. buttonPanel.setLayout(new GridLayout(0, 3));
  53. for (int i = 0; i &lt; buttons.length; i++) {
  54. buttons[i] = new JButton(&quot;colour button&quot;);
  55. buttons[i].setActionCommand(Integer.toString(i));
  56. buttons[i].setBorderPainted(false);
  57. buttons[i].setContentAreaFilled(false);
  58. buttons[i].setOpaque(true);
  59. buttons[i].addActionListener(this);
  60. buttonPanel.add(buttons[i]);
  61. }
  62. return buttonPanel;
  63. }
  64. public void setButtonColour(int index, Color colour) {
  65. buttons[index].setBackground(colour);
  66. }
  67. @Override
  68. public void actionPerformed(ActionEvent event) {
  69. if (!(event.getSource() instanceof JButton)) {
  70. return;
  71. }
  72. int buttonIndex = Integer.valueOf(event.getActionCommand());
  73. System.out.println(buttonIndex + &quot; button clicked.&quot;);
  74. value++;
  75. value %= 9;
  76. model.setColour(buttonIndex, value);
  77. setButtonColour(buttonIndex, model.getColour(buttonIndex));
  78. if (model.isMatch()) {
  79. result.setText(&quot;Winner!&quot;);
  80. }
  81. }
  82. }
  83. public class GameModel {
  84. private Color[] colours = { null, Color.RED, Color.ORANGE, Color.YELLOW,
  85. Color.GREEN, Color.CYAN, Color.BLUE,
  86. Color.MAGENTA, Color.PINK };
  87. private ButtonModel[] buttonValues;
  88. public GameModel() {
  89. buttonValues = new ButtonModel[9];
  90. for (int i = 0; i &lt; buttonValues.length; i++) {
  91. buttonValues[i] = new ButtonModel();
  92. setColour(i, 0);
  93. }
  94. }
  95. public Color getColour(int index) {
  96. return buttonValues[index].getColour();
  97. }
  98. public void setColour(int index, int value) {
  99. buttonValues[index].setValue(value);
  100. buttonValues[index].setColour(colours[value]);
  101. }
  102. public boolean isMatch() {
  103. int value = buttonValues[0].getValue();
  104. for (int i = 1; i &lt; buttonValues.length; i++) {
  105. if (buttonValues[i].getValue() != value) {
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. }
  112. public class ButtonModel {
  113. private int value;
  114. private Color colour;
  115. public int getValue() {
  116. return value;
  117. }
  118. public void setValue(int value) {
  119. this.value = value;
  120. }
  121. public Color getColour() {
  122. return colour;
  123. }
  124. public void setColour(Color colour) {
  125. this.colour = colour;
  126. }
  127. }
  128. }

huangapple
  • 本文由 发表于 2020年9月11日 16:20:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63843367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定