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

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

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

问题

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUI implements ActionListener {

    JFrame frame;
    JPanel panel;
    JLabel label;
    public String output;
    public String input;

    public GUI() {
        panel = new JPanel();

        frame = new JFrame();
        frame.setSize(500,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.add(panel);

        panel.setLayout(null);

        label = new JLabel("Input");
        label.setBounds(10, 20, 80, 25);
        panel.add(label);

        JTextField inputText = new JTextField(20);
        inputText.setBounds(100, 20, 165, 25);
        panel.add(inputText);

        JLabel outputLabel = new JLabel("Password");
        outputLabel.setBounds(10, 50, 80, 25);
        panel.add(outputLabel);

        JTextField outputText = new JTextField();
        outputText.setBounds(100, 50, 165, 25);
        panel.add(outputText);

        JButton button = new JButton("Convert");
        button.setBounds(10, 80, 80, 25);
        button.addActionListener(new GUI());
        panel.add(button);

        JLabel successLabel = new JLabel("");
        successLabel.setBounds(10, 110, 300, 25);
        panel.add(successLabel);

        frame.setVisible(true);

        input = inputText.getText();
        output = outputLabel.getText();
    }

    public static void main(String[] args) {
        new GUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
英文:

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:

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI implements ActionListener {
JFrame frame;
JPanel panel;
JLabel label;
public String output;
public String input;
public GUI() {
panel = new JPanel();
frame = new JFrame();
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
panel.setLayout(null);
label = new JLabel("Input");
label.setBounds(10, 20, 80, 25);
panel.add(label);
JTextField inputText = new JTextField(20);
inputText.setBounds(100, 20, 165, 25);
panel.add(inputText);
JLabel outputLabel = new JLabel("Password");
outputLabel.setBounds(10, 50, 80, 25);
panel.add(outputLabel);
JTextField outputText = new JTextField();
outputText.setBounds(100, 50, 165, 25);
panel.add(outputText);
JButton button = new JButton("Convert");
button.setBounds(10, 80, 80, 25);
button.addActionListener(new GUI());
panel.add(button);
JLabel successLabel = new JLabel("");
successLabel.setBounds(10, 110, 300, 25);
panel.add(successLabel);
frame.setVisible(true);
input = inputText.getText();
output = outputLabel.getText();
}
public static void main(String[] args) {
new GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
}
}

答案1

得分: 2

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

问题出在:

button.addActionListener(new GUI());

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

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

button.addActionListener(this);

希望有所帮助。

英文:

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

The problem lies with:

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:

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:

确定