gpt4 book ai didi

java - JTextArea 未添加到框架中

转载 作者:行者123 更新时间:2023-12-01 18:07:37 25 4
gpt4 key购买 nike

我无法理解为什么 JTextArea 没有随我的代码一起显示。这是我第一次在 swing 程序中使用 FocusAdapter 类。

这是代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Focus extends FocusAdapter
{
JFrame f;
JTextArea jt;
Focus()
{
f=new JFrame("focus");
jt=new JTextArea(50,50);
jt.addFocusListener(this);
jt.setFont(new Font("varinda",Font.PLAIN,15));

f.setSize(550,550);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void focusGained(FocusEvent fe)
{
jt.setText("focusgained");
}
public static void main(String s[])
{
new Focus();
}

}

最佳答案

am unable to understand why JTextArea is not getting displayed with my code. This is the first time i am using FocusAdapter class in a swing program.

这是因为您将 JFrame 的布局设置为 null,并且我没有看到您将 JTextField 添加到 JFrame 中。

你可以这样做:

f.add(jt);
jt.setBounds(x, y, width, height); //Give int values for x, y, width, height

最后但并非最不重要的一点是,尝试为您的 JFrame 使用布局,您可以考虑向 JFrame 添加 JPanel,而不是直接向 JFrame 添加组件。

关于java - JTextArea 未添加到框架中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110810/

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