Java GUI:我的面板都没有显示在框架上,但背景颜色和标题是可见的。

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

Java GUI: None of my panels are showing up on my Frame, but its background color and title are visible?

问题

我正在创建一个基于GUI的计算器程序。我已经将区域分成了几个面板,每个面板有以下用途:

  • inputPanel:用户将输入数字的地方;
  • numericPanel:包含数字 0-9 的面板;
  • operatorPanel:包含运算符号(+、-、*、/)的面板;
  • formatPanel:包含CLEAR、ENTER和EXIT按钮的面板。

在使窗口可见之前,我将所有这些面板添加到了框架中,但当我点击运行按钮时,除了背景颜色外什么都没有显示出来。我是否应该以不同的方式进行格式化?

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.concurrent.Flow;
  6. public class calculator extends Frame implements ActionListener{
  7. private static JTextField firstField= new JTextField(25);
  8. private static JTextField num2 = new JTextField(25);
  9. //------------------------------------------------------------------------------------
  10. private static JButton b0 = new JButton("0");
  11. private static JButton b1 = new JButton("1");
  12. private static JButton b2 = new JButton("2");
  13. private static JButton b3 = new JButton("3");
  14. private static JButton b4 = new JButton("4");
  15. private static JButton b5 = new JButton("5");
  16. private static JButton b6 = new JButton("6");
  17. private static JButton b7 = new JButton("7");
  18. private static JButton b8 = new JButton("8");
  19. private static JButton b9 = new JButton("9");
  20. //-----------------------------------------------------------------------------------
  21. private static JButton enterButton = new JButton("Enter");
  22. private static JButton clearButton = new JButton("Clear");
  23. private static JButton exitButton = new JButton("Close");
  24. private static JButton addB = new JButton("+");
  25. private static JButton subB = new JButton("-");
  26. private static JButton multiB = new JButton("x");
  27. private static JButton divideB = new JButton("÷");
  28. private static JPanel inputPanel;
  29. private static JPanel numericPanel;
  30. private static JPanel operatorPanel;
  31. private static JPanel formatPanel;
  32. private static JLabel resultLabel;
  33. private static JLabel nameLabel = new JLabel("Created by: Yuri Videz");
  34. //-----------------------------------------------------------------------------------
  35. public void setInputPanel(JPanel panel) {
  36. panel.setLayout(new FlowLayout());
  37. JLabel firstNum = new JLabel("Enter your number:");
  38. panel.add(firstNum);
  39. panel.add(firstField);
  40. }
  41. public void setNumericPanel(JPanel panel) {
  42. panel.setLayout(new GridLayout(6, 3));
  43. panel.add(b0);
  44. panel.add(b1);
  45. panel.add(b2);
  46. panel.add(b3);
  47. panel.add(b4);
  48. panel.add(b5);
  49. panel.add(b6);
  50. panel.add(b7);
  51. panel.add(b8);
  52. panel.add(b9);
  53. }
  54. public void setOperatorPanel(JPanel panel) {
  55. JPanel operatorPanel = new JPanel();
  56. //operatorPanel.setLayout(new FlowLayout());
  57. panel.add(addB);
  58. panel.add(subB);
  59. panel.add(multiB);
  60. panel.add(divideB);
  61. }
  62. public void setFormatPanel(JPanel panel) {
  63. JPanel formatPanel = new JPanel();
  64. panel.setLayout(new FlowLayout());
  65. panel.add(enterButton);
  66. panel.add(clearButton);
  67. }
  68. public calculator() {
  69. inputPanel = new JPanel();
  70. setInputPanel(inputPanel);
  71. numericPanel = new JPanel();
  72. setNumericPanel(numericPanel);
  73. operatorPanel = new JPanel();
  74. setOperatorPanel(operatorPanel);
  75. formatPanel = new JPanel();
  76. setFormatPanel(formatPanel);
  77. setTitle("Calculator Program");
  78. JFrame frame = new JFrame();
  79. frame.getContentPane().setLayout(new GridLayout(3, 3));
  80. frame.add(nameLabel);
  81. frame.add(inputPanel);
  82. frame.add(numericPanel);
  83. frame.add(operatorPanel);
  84. frame.add(formatPanel);
  85. setBackground(Color.gray);
  86. setSize(350, 500);
  87. frame.pack();
  88. setVisible(true);
  89. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  90. }
  91. public static void main(String[] args){
  92. calculator calcuObject = new calculator();
  93. }
  94. @Override
  95. public void actionPerformed(ActionEvent ac) {
  96. }
  97. }
英文:

I am creating a GUI-based Calculator program. I've separated the areas into panels, which have the ff purposes:

  • inputPanel: Where users will input numbers;
  • numericPanel: Panel that holds the digits 0-9
  • operatorPanel: Panel that holds operator symbols(+, -, *, /)
  • formatPanel: Panel that will hold CLEAR, ENTER, and EXIT buttons

I added all these panels to the frame before making it visible, but when I press play, nothing shows up except for the background color. Should I be formatting them a different way?

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.concurrent.Flow;
  6. /**
  7. * @author Graeven Yuri Videz
  8. * This is a program that simulates a simple calculator with arithmetic functions.
  9. */
  10. public class calculator extends Frame implements ActionListener{
  11. private static JTextField firstField= new JTextField(25);
  12. private static JTextField num2 = new JTextField(25);
  13. //------------------------------------------------------------------------------------
  14. private static JButton b0 = new JButton("0");
  15. private static JButton b1 = new JButton("1");
  16. private static JButton b2 = new JButton("2");
  17. private static JButton b3 = new JButton("3");
  18. private static JButton b4 = new JButton("4");
  19. private static JButton b5 = new JButton("5");
  20. private static JButton b6 = new JButton("6");
  21. private static JButton b7 = new JButton("7");
  22. private static JButton b8 = new JButton("8");
  23. private static JButton b9 = new JButton("9");
  24. //-----------------------------------------------------------------------------------
  25. private static JButton enterButton = new JButton("Enter");
  26. private static JButton clearButton = new JButton("Clear");
  27. private static JButton exitButton = new JButton("Close");
  28. private static JButton addB = new JButton("+");
  29. private static JButton subB = new JButton("-");
  30. private static JButton multiB = new JButton("x");
  31. private static JButton divideB = new JButton("÷");
  32. private static JPanel inputPanel;
  33. private static JPanel numericPanel;
  34. private static JPanel operatorPanel;
  35. private static JPanel formatPanel;
  36. private static JLabel resultLabel;
  37. private static JLabel nameLabel = new JLabel("Created by: Yuri Videz");
  38. //-----------------------------------------------------------------------------------
  39. public void setInputPanel(JPanel panel) {
  40. panel.setLayout(new FlowLayout());
  41. JLabel firstNum = new JLabel("Enter your number:");
  42. panel.add(firstNum);
  43. panel.add(firstField);
  44. }
  45. public void setNumericPanel(JPanel panel) {
  46. panel.setLayout(new GridLayout(6, 3));
  47. panel.add(b0);
  48. panel.add(b1);
  49. panel.add(b2);
  50. panel.add(b3);
  51. panel.add(b4);
  52. panel.add(b5);
  53. panel.add(b6);
  54. panel.add(b7);
  55. panel.add(b8);
  56. panel.add(b9);
  57. }
  58. public void setOperatorPanel(JPanel panel) {
  59. JPanel operatorPanel = new JPanel();
  60. //operatorPanel.setLayout(new FlowLayout());
  61. panel.add(addB);
  62. panel.add(subB);
  63. panel.add(multiB);
  64. panel.add(divideB);
  65. }
  66. public void setFormatPanel(JPanel panel) {
  67. JPanel formatPanel = new JPanel();
  68. panel.setLayout(new FlowLayout());
  69. panel.add(enterButton);
  70. panel.add(clearButton);
  71. }
  72. public calculator() {
  73. inputPanel = new JPanel();
  74. setInputPanel(inputPanel);
  75. numericPanel = new JPanel();
  76. setNumericPanel(numericPanel);
  77. operatorPanel = new JPanel();
  78. setOperatorPanel(operatorPanel);
  79. formatPanel = new JPanel();
  80. setFormatPanel(formatPanel);
  81. setTitle("Calculator Program");
  82. JFrame frame = new JFrame();
  83. frame.getContentPane().setLayout(new GridLayout(3, 3));
  84. frame.add(nameLabel);
  85. frame.add(inputPanel);
  86. frame.add(numericPanel);
  87. frame.add(operatorPanel);
  88. frame.add(formatPanel);
  89. setBackground(Color.gray);
  90. setSize(350, 500);
  91. frame.pack();
  92. setVisible(true);
  93. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  94. }
  95. public static void main(String[] args){
  96. calculator calcuObject = new calculator();
  97. }
  98. @Override
  99. public void actionPerformed(ActionEvent ac) {
  100. }
  101. }

答案1

得分: 1

你的代码中有两个不同的 JFrame 实例。
你将所有组件添加到 JFrame frame = new JFrame();,但你通过 setVisible(true); 使另一个实例可见。
解决方案:
setVisible(true); 更改为 frame.setVisible(true);
计算器没有必要扩展 JFrame


附注:
1. 请查看[Java命名规范](https://www.geeksforgeeks.org/java-naming-conventions/)。
2. 点击[此链接](https://ide.geeksforgeeks.org/nwtWItC9s5)查看代码的重构版本。

英文:

You have two different JFrame instances in your code.<br/>
You add all components to JFrame frame = new JFrame(); but you make the other one visible by setVisible(true);<br/>
Solution:
change setVisible(true); to frame.setVisible(true);<br/>
There is no need for calculator to extend JFrame.
<hr>
Side notes: <br/>

  1. see Java naming conventions<br/>
  2. Follow this link for a refactored version of your code

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

发表评论

匿名网友

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

确定