IntelliJ IDEA在我尝试运行代码时创建了大量新窗口。

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

IntelliJ IDEA Creating a ton of new windows when I try to run my code

问题

  1. import javax.swing.*;
  2. import javax.swing.border.Border;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. public class GUI implements ActionListener {
  7. JFrame frame;
  8. JPanel panel;
  9. JLabel label;
  10. public String output;
  11. public String input;
  12. public GUI() {
  13. panel = new JPanel();
  14. frame = new JFrame();
  15. frame.setSize(500,400);
  16. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. frame.setVisible(true);
  18. frame.add(panel);
  19. panel.setLayout(null);
  20. label = new JLabel("Input");
  21. label.setBounds(10, 20, 80, 25);
  22. panel.add(label);
  23. JTextField inputText = new JTextField(20);
  24. inputText.setBounds(100, 20, 165, 25);
  25. panel.add(inputText);
  26. JLabel outputLabel = new JLabel("Password");
  27. outputLabel.setBounds(10, 50, 80, 25);
  28. panel.add(outputLabel);
  29. JTextField outputText = new JTextField();
  30. outputText.setBounds(100, 50, 165, 25);
  31. panel.add(outputText);
  32. JButton button = new JButton("Convert");
  33. button.setBounds(10, 80, 80, 25);
  34. button.addActionListener(new GUI());
  35. panel.add(button);
  36. JLabel successLabel = new JLabel("");
  37. successLabel.setBounds(10, 110, 300, 25);
  38. panel.add(successLabel);
  39. frame.setVisible(true);
  40. input = inputText.getText();
  41. output = outputLabel.getText();
  42. }
  43. public static void main(String[] args) {
  44. new GUI();
  45. }
  46. @Override
  47. public void actionPerformed(ActionEvent e) {
  48. }
  49. }
英文:

I'm making a very simple program in IntelliJ. It's going smoothly, and I would still be making progress if it ran properly. I click the run button and it gives me no errors when compiling, but it opens a ton of windows very quickly, and when I stop the program it closes them all. This has effectively halted my progress and I'm starting to run out of time to complete this. Anyone have a fix?

Code:

  1. import javax.swing.*;
  2. import javax.swing.border.Border;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. public class GUI implements ActionListener {
  7. JFrame frame;
  8. JPanel panel;
  9. JLabel label;
  10. public String output;
  11. public String input;
  12. public GUI() {
  13. panel = new JPanel();
  14. frame = new JFrame();
  15. frame.setSize(500,400);
  16. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. frame.setVisible(true);
  18. frame.add(panel);
  19. panel.setLayout(null);
  20. label = new JLabel("Input");
  21. label.setBounds(10, 20, 80, 25);
  22. panel.add(label);
  23. JTextField inputText = new JTextField(20);
  24. inputText.setBounds(100, 20, 165, 25);
  25. panel.add(inputText);
  26. JLabel outputLabel = new JLabel("Password");
  27. outputLabel.setBounds(10, 50, 80, 25);
  28. panel.add(outputLabel);
  29. JTextField outputText = new JTextField();
  30. outputText.setBounds(100, 50, 165, 25);
  31. panel.add(outputText);
  32. JButton button = new JButton("Convert");
  33. button.setBounds(10, 80, 80, 25);
  34. button.addActionListener(new GUI());
  35. panel.add(button);
  36. JLabel successLabel = new JLabel("");
  37. successLabel.setBounds(10, 110, 300, 25);
  38. panel.add(successLabel);
  39. frame.setVisible(true);
  40. input = inputText.getText();
  41. output = outputLabel.getText();
  42. }
  43. public static void main(String[] args) {
  44. new GUI();
  45. }
  46. @Override
  47. public void actionPerformed(ActionEvent e) {
  48. }
  49. }

答案1

得分: 2

谢谢您更新帖子并附上代码。事实上,您已经创建了一个无限循环!

问题出在:

  1. button.addActionListener(new GUI());

每次实例化GUI类时,您都在创建一个新的GUI类的实例。明白我的意思吗?

我想您想要使用GUI类的当前实例作为您的动作监听器,所以正确的做法是:

  1. button.addActionListener(this);

希望有所帮助。

英文:

Thank you for updating your post with the code. You have, in fact, created an endless loop!

The problem lies with:

  1. button.addActionListener(new GUI());

You are creating a new instance of the class GUI every time the class GUI is instantiated. See what I mean?

I think you want to use the current instance of class GUI as your action listener, so the correct way to do this would be:

  1. button.addActionListener(this);

Hope that helped.

huangapple
  • 本文由 发表于 2020年10月13日 09:09:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64327116.html
匿名

发表评论

匿名网友

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

确定