英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论