gpt4 book ai didi

java - 为什么除非调整窗口大小,否则无法在 JTextArea 中键入内容?

转载 作者:行者123 更新时间:2023-12-01 10:02:22 24 4
gpt4 key购买 nike

我已将 JTextArea 放置在 JFrame 中,但遇到了一个问题:除非调整窗口大小,否则我只能在 JTextArea 中键入内容。如何让 JTextArea 让我在窗口运行后立即输入内容,而无需调整其大小?

public class Frame extends JFrame{

public Note() {
this.setSize(new Dimension(1000, 1000));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JScrollPane createContent(){
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
this.add(scrollPane, BorderLayout.CENTER);
return null;
}
public static void main(String[] args) {
new Note();
Note mainWindow = new Note();
}
}

最佳答案

公共(public)类 Note 扩展 JFrame{

private static final long serialVersionUID = 1L;

public Note() {
createContent(); // add this line into your code.
int x = 400;
int y = 300;
this.setSize(new Dimension(x, y));
this.setTitle("Post-It Note");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JScrollPane createContent(){
Color textAreaColor = new Color(248, 247, 235);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBorder(null);
textArea.setBackground(textAreaColor);
scrollPane.setBackground(textAreaColor);
textArea.setMargin(new Insets(10, 15, 20, 20));
this.add(scrollPane, BorderLayout.CENTER);
return null;
}

public static void main(String[] args) {
new Note();
// mainWindow.createContent(); comment this line...
}

}

注意:

进入您的旧代码,

new Note(); // this one create 1-frame..
...
// Note mainWindow = new Note(); this one also create another frame so need to comment it
// mainWindow.createContent(); does not required because already this method called by constructor...

关于java - 为什么除非调整窗口大小,否则无法在 JTextArea 中键入内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36698391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com