只有一个项目在JFrame窗口中可见。

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

Only single item is visible in the JFrame Window

问题

以下是翻译好的部分:

  1. package learningPackage;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5. import javax.swing.JOptionPane;
  6. import javax.swing.JFrame;
  7. import javax.swing.JTextField;
  8. import javax.swing.JPasswordField;
  9. class tuna extends JFrame {
  10. private JTextField textField1;
  11. private JTextField textField2;
  12. private JTextField textField3;
  13. private JPasswordField passwordField;
  14. public tuna() {
  15. super("Title Text");
  16. textField1 = new JTextField(10);
  17. add(textField1);
  18. textField2 = new JTextField("Enter a Text");
  19. add(textField2);
  20. textField3 = new JTextField("Uneditable", 20);
  21. textField3.setEditable(false);
  22. add(textField3);
  23. passwordField = new JPasswordField("myPassword");
  24. add(passwordField);
  25. thehandler Handler = new thehandler();
  26. textField1.addActionListener(Handler);
  27. textField2.addActionListener(Handler);
  28. textField3.addActionListener(Handler);
  29. passwordField.addActionListener(Handler);
  30. }
  31. class thehandler implements ActionListener {
  32. public void actionPerformed(ActionEvent event) {
  33. String string = "";
  34. if(event.getSource()==textField1)
  35. string = String.format("Text Field 1 : %s", event.getActionCommand());
  36. else if(event.getSource()==textField2)
  37. string = String.format("Text Field 2 : %s", event.getActionCommand());
  38. else if(event.getSource()==textField3)
  39. string = String.format("Text Field 3 : %s", event.getActionCommand());
  40. else if(event.getSource()==passwordField)
  41. string = String.format("Password Field : %s", event.getActionCommand());
  42. JOptionPane.showConfirmDialog(null, string);
  43. }
  44. }
  45. }
  46. public class Apples {
  47. public static void main(String[] args) {
  48. tuna Srivathsan = new tuna();
  49. Srivathsan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50. Srivathsan.setSize(500, 500);
  51. Srivathsan.setVisible(true);
  52. Srivathsan.setLocationRelativeTo(null);
  53. }
  54. }

请注意,我已按照您的要求,仅提供了代码的翻译部分,没有添加额外的内容。如果您需要进一步的帮助或解释,请随时提问。

英文:

I wrote this after seeing videos of thenewboston and then, when I ran, only the last item added was visible and all other textFields [textField1, textField2 and textField3] are not visible. It worked perfectly in his videos but when I tried, only the passwordField was visible. I'm a beginner and I was unable to find what's the problem. Please help me out what is the problem and what should I do to mitigate this error. One small request, please explain a bit detail because I a newbie to Java and GUI.

  1. package learningPackage;
  2. import java.awt.FlowLayout;
  3. //gives the layout
  4. import java.awt.event.ActionListener;
  5. //this makes the field wait for an event.
  6. import java.awt.event.ActionEvent;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JFrame;
  9. import javax.swing.JTextField;
  10. //creates a field where we can type text.
  11. import javax.swing.JPasswordField;
  12. //creates a text field where the text typed is hidden with asterics.
  13. class tuna extends JFrame
  14. {
  15. private JTextField textField1;
  16. private JTextField textField2;
  17. private JTextField textField3;
  18. private JPasswordField passwordField;
  19. public tuna()
  20. {
  21. super("Title Text");
  22. textField1 = new JTextField(10);
  23. //sets a the default value of 10.
  24. add(textField1);
  25. textField2 = new JTextField("Enter a Text");
  26. //sets default text of "Enter a Text" without the quotes
  27. add(textField2);
  28. textField3 = new JTextField("Uneditable", 20);
  29. //Displays Uneditable with a default value of 20.
  30. //To make this Uneditable, you must do this...
  31. textField3.setEditable(false);
  32. //this makes the textField uneditable.
  33. add(textField3);
  34. passwordField = new JPasswordField("myPassword");
  35. add(passwordField);
  36. thehandler Handler = new thehandler();
  37. textField1.addActionListener(Handler);
  38. textField2.addActionListener(Handler);
  39. textField3.addActionListener(Handler);
  40. passwordField.addActionListener(Handler);
  41. /*
  42. * The addActionListener method takes an object of Event Handler class.
  43. *
  44. * Therefore, we must create an object of Event Handler Class.
  45. *
  46. *
  47. * The addActionListener method takes an object because, sometimes, we might
  48. * have different classes with different code to execute. So, we pass the object to
  49. * identify which class code is to be executed.
  50. */
  51. }
  52. class thehandler implements ActionListener
  53. {
  54. /*
  55. * In order to handle events in Java, you need Event handler Class.
  56. * and, that Event Handler Class must implement ActionListener.
  57. *
  58. * What the ActionListener does is that
  59. *
  60. * It will wait for some Event to happen and after that particular event happens,
  61. * it will implement some piece of code.
  62. */
  63. public void actionPerformed(ActionEvent event)
  64. {
  65. String string = "";
  66. if(event.getSource()==textField1)
  67. string = String.format("Text Field 1 : %s", event.getActionCommand());
  68. else if(event.getSource()==textField2)
  69. string = String.format("Text Field 2 : %s", event.getActionCommand());
  70. else if(event.getSource()==textField3)
  71. string = String.format("Text Field 3 : %s", event.getActionCommand());
  72. else if(event.getSource()==passwordField)
  73. string = String.format("Password Field : %s", event.getActionCommand());
  74. //when the user presses enter key after entering text, this actually stores the
  75. //thing entered into the String.
  76. JOptionPane.showConfirmDialog(null, string);
  77. }
  78. }
  79. }
  80. public class Apples {
  81. public static void main(String[] args)
  82. {
  83. tuna Srivathsan = new tuna();
  84. Srivathsan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85. //this would close the window when X button is pressed.
  86. Srivathsan.setSize(500, 500);
  87. Srivathsan.setVisible(true);
  88. Srivathsan.setLocationRelativeTo(null);
  89. }
  90. }

Here is the image of the window :

只有一个项目在JFrame窗口中可见。

答案1

得分: 2

  1. import java.awt.FlowLayout;
  2. // 导入布局
  3. 那个语句**导入**了布局并使其在代码中可用但不会在任何容器上设置布局
  4. 虽然`JPanel`**默认**布局是`FlowLayout``JFrame`的内容窗格的默认布局是`BorderLayout``BorderLayout`在每个5个约束中最多接受5个组件或容器`JPanel`)。如果不给出约束它默认为`CENTER`如果将多个组件添加到`CENTER``BorderLayout`的任何其他区域),除了一个组件外其他所有组件都将无法显示我记不清是第一个还是最后一个出现了但这是一个无关紧要的问题因为代码不应该这样做
  5. 我的建议如下
  6. 1. **不要**扩展`JFrame``JPanel`)。
  7. 2. 将一个`JPanel`添加到`JFrame`
  8. 3. 将组件/或容器添加到该`JPanel`
  9. 这里有一个实现第2和第3点的示例1点没有被实现不包括电池)。
  10. ```java
  11. import javax.swing.*;
  12. public class SingleComponentLayoutProblem extends JFrame {
  13. private final JTextField textField1;
  14. private final JTextField textField2;
  15. private final JTextField textField3;
  16. private final JPasswordField passwordField;
  17. public SingleComponentLayoutProblem() {
  18. super("Title Text");
  19. JPanel panel = new JPanel(); // 默认使用FlowLayout
  20. add(panel);
  21. textField1 = new JTextField(10);
  22. // 设置默认值为10。
  23. panel.add(textField1);
  24. textField2 = new JTextField("Enter a Text");
  25. // 设置默认文本为"Enter a Text"(不带引号)
  26. panel.add(textField2);
  27. textField3 = new JTextField("Uneditable", 20);
  28. // 显示Uneditable,默认值为20。
  29. // 要使此字段不可编辑,您必须执行以下操作...
  30. textField3.setEditable(false);
  31. // 这将使textField不可编辑。
  32. panel.add(textField3);
  33. passwordField = new JPasswordField("myPassword");
  34. panel.add(passwordField);
  35. }
  36. public static void main(String[] args) {
  37. Runnable r = () -> {
  38. SingleComponentLayoutProblem sclp =
  39. new SingleComponentLayoutProblem();
  40. sclp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. // 当按下X按钮时,将关闭窗口。
  42. // 不要猜测大小!..
  43. // sclp.setSize(500, 500);
  44. // .. 取而代之 ..
  45. sclp.pack();
  46. sclp.setVisible(true);
  47. sclp.setLocationRelativeTo(null);
  48. };
  49. SwingUtilities.invokeLater(r);
  50. }
  51. }
英文:
  1. import java.awt.FlowLayout;
  2. //gives the layout

That statement imports the layout & makes it available to the code, but does not set the layout on any container.

While a JPanel has a default layout of FlowLayout, the default layout of the (content pane of the) JFrame is BorderLayout. A BorderLayout accepts up to 5 components or containers (like JPanel) in each of 5 constraints. If no constraint is given, it defaults to the CENTER. If more than one component is added to the CENTER (or any other area of a BorderLayout), all but one of those components will fail to appear. I cannot recall if it is the first or last that appears, but it's a moot point, because the code should not do that.

My advice would be as follows:

  1. Don't extend JFrame (or JPanel).
  2. Add one JPanel to the JFrame.
  3. Add the components (and/or containers) to that JPanel.

Here is an example implementing points 2 & 3. Point 1 is not implemented (batteries not included).

只有一个项目在JFrame窗口中可见。

  1. import javax.swing.*;
  2. public class SingleComponentLayoutProblem extends JFrame {
  3. private final JTextField textField1;
  4. private final JTextField textField2;
  5. private final JTextField textField3;
  6. private final JPasswordField passwordField;
  7. public SingleComponentLayoutProblem() {
  8. super("Title Text");
  9. JPanel panel = new JPanel(); // uses FlowLayout be DEFAULT
  10. add(panel);
  11. textField1 = new JTextField(10);
  12. //sets a the default value of 10.
  13. panel.add(textField1);
  14. textField2 = new JTextField("Enter a Text");
  15. //sets default text of "Enter a Text" without the quotes
  16. panel.add(textField2);
  17. textField3 = new JTextField("Uneditable", 20);
  18. //Displays Uneditable with a default value of 20.
  19. //To make this Uneditable, you must do this...
  20. textField3.setEditable(false);
  21. //this makes the textField uneditable.
  22. panel.add(textField3);
  23. passwordField = new JPasswordField("myPassword");
  24. panel.add(passwordField);
  25. }
  26. public static void main(String[] args) {
  27. Runnable r = () -> {
  28. SingleComponentLayoutProblem sclp =
  29. new SingleComponentLayoutProblem();
  30. sclp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31. //this would close the window when X button is pressed.
  32. // don't guess sizes! ..
  33. //sclp.setSize(500, 500);
  34. // .. instead ..
  35. sclp.pack();
  36. sclp.setVisible(true);
  37. sclp.setLocationRelativeTo(null);
  38. };
  39. SwingUtilities.invokeLater(r);
  40. }
  41. }

huangapple
  • 本文由 发表于 2020年7月27日 16:48:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63111755.html
匿名

发表评论

匿名网友

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

确定