[英]Opening a JTextArea when a button is clicked
我正在编写一个包含许多按钮的程序,当我单击其中一个按钮时,我希望它打开一个 JTextArea,其中包含一些 txt 文件信息。 我试图将 JTextArea 添加到按钮,但它没有打开任何东西。 我是 Java 的新手,所以我不知道这是否是正确的方法。
我在 GUI 中定义了按钮和 JTextArea
showListButton.add(jTextArea);
这是我在 public void actionPerformed(ActionEvent e) 中编写的代码
if(e.getSource() == showListButton){
try {
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
String word;
while((word= in.readLine()) != null)
{
jTextArea.read( in, null );
jTextTextArea.requestFocus();
}
in.close();
}catch(IOException e1){
e1.printStackTrace();
}
}
要显示某些内容,您需要一个根容器,例如Window
或Jframe
。 要显示您的文本区域,您已将其添加到另一个框架中,您将在单击时显示或将其添加到已显示的面板中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.