按钮没有放在我告诉它们的位置。

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

Buttons are not being place where I tell them

问题

以下是您要翻译的内容:

"So I was trying to place 10 buttons in a grid at the bottom of my frame (without using a layout manager) and for some reason I cannot figure out why this isn't working, I was wondering if anyone could help.

I should probably explain the code:

Basically I have just created a loop that stores a 'while' in it, this while is repeated 3 times before ending and going back to the loop. After the while is finished, the Y changes by 85 to start a new layer for the next 3 buttons. The loop repeats 4 times to create 4 different layers of 10 buttons.

Thanks!

Main

  1. public class Main {
  2. private static JFrame frame = new JFrame();
  3. private static JPanel panel = new JPanel();
  4. public static void main(String[] args) {
  5. Buttons.setButtons(frame, panel);
  6. Window.setFrame(frame, panel);
  7. }
  8. }

Buttons

  1. public class Buttons {
  2. private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
  3. private static JButton equals;
  4. private static JButton minus;
  5. private static JButton plus;
  6. private static int i = 0;
  7. private static int x = 10;
  8. private static int y = 140;
  9. private static JButton buttons[] = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};
  10. public static void setButtons(JFrame frame, JPanel panel) {
  11. for (int b = 0; b < 4; b++) {
  12. while (i < 3) {
  13. buttons[i] = new JButton(Integer.toString(i));
  14. buttons[i].setBounds(x, y, 80, 80);
  15. x = x + 85;
  16. buttons[i].setFocusable(false);
  17. buttons[i].setBackground(Color.GRAY);
  18. panel.add(buttons[i]);
  19. i++;
  20. }
  21. y = y + 85;
  22. x = 10;
  23. }
  24. System.out.print(y);
  25. frame.setVisible(true);
  26. }
  27. }

Window

  1. public class Window {
  2. public static void setFrame(JFrame frame, JPanel panel) {
  3. frame.setSize(370, 525);
  4. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. frame.setResizable(false);
  6. panel.setBounds(10, 140, 200, 200);
  7. panel.setBackground(Color.DARK_GRAY);
  8. frame.add(panel);
  9. }
  10. }
英文:

So I was trying to place 10 buttons in a grid at the bottom of my frame (without using a layout manager) and for some reason I cannot figure out why this isn't working, I was wondering if anyone could help.

I should probably explain the code:

Basically I have just created a loop that stores a "while" in it, this while is repeated 3 times before ending and going back to the loop. After the while is finished, the Y changes by 85 to start a new layer for the next 3 buttons. The loop repeats 4 times to create 4 different layers of 10 buttons.

Thanks!

Main

  1. public class Main {
  2. private static JFrame frame = new JFrame();
  3. private static JPanel panel = new JPanel();
  4. public static void main(String[] args) {
  5. Buttons.setButtons(frame, panel);
  6. Window.setFrame(frame, panel);
  7. }
  8. }

Buttons

  1. public class Buttons {
  2. private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
  3. private static JButton equals;
  4. private static JButton minus;
  5. private static JButton plus;
  6. private static int i = 0;
  7. private static int x = 10;
  8. private static int y = 140;
  9. private static JButton buttons[] = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};
  10. public static void setButtons(JFrame frame, JPanel panel) {
  11. for (int b = 0; b < 4; b++) {
  12. while (i < 3) {
  13. buttons[i] = new JButton(Integer.toString(i));
  14. buttons[i].setBounds(x, y, 80, 80);
  15. x = x + 85;
  16. buttons[i].setFocusable(false);
  17. buttons[i].setBackground(Color.GRAY);
  18. panel.add(buttons[i]);
  19. i++;
  20. }
  21. y = y + 85;
  22. x = 10;
  23. }
  24. System.out.print(y);
  25. frame.setVisible(true);
  26. }
  27. }

Window

  1. public class Window {
  2. public static void setFrame(JFrame frame, JPanel panel) {
  3. frame.setSize(370, 525);
  4. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. frame.setResizable(false);
  6. panel.setBounds(10, 140, 200, 200);
  7. panel.setBackground(Color.DARK_GRAY);
  8. frame.add(panel);
  9. }
  10. }

答案1

得分: -2

Here's the translated code:

  1. 感谢这个代码有效并解决了我的一个问题另一个问题是在第二次循环后while不起作用所以我不得不重写循环系统这是解决方案以防你想使用它不要使用这个我是新手也不要像我一样使用布局管理器。):
  2. public class Buttons {
  3. private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
  4. private static JButton dot;
  5. private static JButton equals;
  6. private static JButton minus;
  7. private static JButton plus;
  8. private static int count = 9;
  9. private static int x = 10;
  10. private static int y = 145;
  11. private static JButton buttons[] = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};
  12. public static void setButtons(JFrame frame, JPanel panel) {
  13. for (int b = 3; b > 0; b--) {
  14. for (int i = 3; i > 0 ; i--) {
  15. buttons[count] = new JButton(Integer.toString(count));
  16. buttons[count].setBounds(x, y, 80, 80);
  17. x = x + 85;
  18. buttons[count].setFocusable(false);
  19. buttons[count].setBackground(Color.GRAY);
  20. panel.add(buttons[count]);
  21. count--;
  22. }
  23. y = y + 85;
  24. x = 10;
  25. }
  26. dot = new JButton(".");
  27. dot.setBounds(x, y, 80, 80);
  28. dot.setFocusable(false);
  29. dot.setBackground(new Color(0, 200, 150));
  30. panel.add(dot);
  31. buttons[0] = new JButton("0");
  32. x = x + 85;
  33. buttons[0].setBounds(x, y, 80, 80);
  34. buttons[0].setFocusable(false);
  35. buttons[0].setBackground(Color.GRAY);
  36. panel.add(buttons[0]);
  37. plus = new JButton("+");
  38. x = x + 85;
  39. plus.setBounds(x, y, 80, 80);
  40. plus.setFocusable(false);
  41. plus.setBackground(new Color(0, 200, 150));
  42. panel.add(plus);
  43. minus = new JButton("-");
  44. x = x + 85;
  45. minus.setBounds(x, y, 80, 80);
  46. minus.setFocusable(false);
  47. minus.setBackground(new Color(0, 200, 150));
  48. panel.add(minus);
  49. equals = new JButton("=");
  50. y = 145;
  51. equals.setBounds(x, y, 80, 250);
  52. equals.setFocusable(false);
  53. equals.setBackground(new Color(240, 150, 50));
  54. panel.add(equals);
  55. frame.setVisible(true);
  56. }
  57. }

I've translated the code as requested.

英文:

Thanks this works, and solved one of my problems. The other problem was that the "while" did not work after the second loop so I had to rewrite the loop system, here is the solution just in case you want to use it (don't use this, I'm a noob. Also don't be like me, use a layout manager.):

  1. public class Buttons {
  2. private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
  3. private static JButton dot;
  4. private static JButton equals;
  5. private static JButton minus;
  6. private static JButton plus;
  7. private static int count = 9;
  8. private static int x = 10;
  9. private static int y = 145;
  10. private static JButton buttons[] = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};
  11. public static void setButtons(JFrame frame, JPanel panel) {
  12. for (int b = 3; b > 0; b--) {
  13. for (int i = 3; i > 0 ; i--) {
  14. buttons[count] = new JButton(Integer.toString(count));
  15. buttons[count].setBounds(x, y, 80, 80);
  16. x = x + 85;
  17. buttons[count].setFocusable(false);
  18. buttons[count].setBackground(Color.GRAY);
  19. panel.add(buttons[count]);
  20. count--;
  21. }
  22. y = y + 85;
  23. x = 10;
  24. }
  25. dot = new JButton(".");
  26. dot.setBounds(x, y, 80, 80);
  27. dot.setFocusable(false);
  28. dot.setBackground(new Color(0, 200, 150));
  29. panel.add(dot);
  30. buttons[0] = new JButton("0");
  31. x = x + 85;
  32. buttons[0].setBounds(x, y, 80, 80);
  33. buttons[0].setFocusable(false);
  34. buttons[0].setBackground(Color.GRAY);
  35. panel.add(buttons[0]);
  36. plus = new JButton("+");
  37. x = x + 85;
  38. plus.setBounds(x, y, 80, 80);
  39. plus.setFocusable(false);
  40. plus.setBackground(new Color(0, 200, 150));
  41. panel.add(plus);
  42. minus = new JButton("-");
  43. x = x + 85;
  44. minus.setBounds(x, y, 80, 80);
  45. minus.setFocusable(false);
  46. minus.setBackground(new Color(0, 200, 150));
  47. panel.add(minus);
  48. equals = new JButton("=");
  49. y = 145;
  50. equals.setBounds(x, y, 80, 250);
  51. equals.setFocusable(false);
  52. equals.setBackground(new Color(240, 150, 50));
  53. panel.add(equals);
  54. frame.setVisible(true);
  55. }

}

答案2

得分: -3

也许你认为你没有使用布局管理器,因为你没有在你的 JFrame 上调用 setLayout,但事实上,默认情况下,JFrame 会自动使用 BorderLayout 布局。

如果你确实想要不使用布局管理器,请在你的设置代码中添加以下行:

  1. frame.setLayout(null);
英文:

Maybe you think you're working without a layout manager because you haven't called setLayout on your JFrame, but in fact the JFrame gave itself a BorderLayout by default.

If you truly want to work without a layout manager, add the line

  1. frame.setLayout(null);

in your setup code.

huangapple
  • 本文由 发表于 2020年8月1日 11:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63201609.html
匿名

发表评论

匿名网友

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

确定