JTextArea/Field 中未设置文本

发布于 2024-12-14 08:54:39 字数 2689 浏览 5 评论 0原文

我在 JTextArea 和 JTextField 中设置一些文本时遇到问题...我有点困惑,因为我之前已经在其中设置了文本,事实上我已经将它们设置在同一个程序的不同类中,没有任何问题...我已经打印出字符串进行检查,以确保它不为空或只是空“”,并且我的字符串确实打印了它们应该打印的内容,但它们没有在 JTextField/Area 中设置..

任何人都可以告诉我为什么会搞砸?

(我已经给了您完整的代码,这样您就不会被方法调用所困惑,但其中大部分可能是不必要的。您真正需要关注的是 run() 方法和 populateInfo() 方法。 populateInfo( )是应该将测试设置为 textArea 和 textField 的地方。)

下面是代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import java.io.*;

public class EditDiary extends JApplet implements ActionListener {

  private JFrame frame = new JFrame();
  private String[] date;
  private String[] rest;
  private int index = 0;
  private int count = 0;

  private JLabel lblDate = new JLabel("Date");
  private JTextField txtDate = new JTextField();
  private JLabel lblDiary = new JLabel("Diary Entry");
  private JTextArea txtDiary = new JTextArea(20,30);

  private JButton btnDone = new JButton("Done");
  private JButton btnCancel = new JButton("Cancel");

  public EditDiary(){

    JPanel panel1 = new JPanel();

    panel1.add(lblDate);
    panel1.add(txtDate);
    panel1.add(lblDiary);
    panel1.add(txtDiary);
    panel1.add(btnDone);
    panel1.add(btnCancel);

    add(panel1);

    btnDone.addActionListener(this);
    btnCancel.addActionListener(this);

  }

  public void actionPerformed(ActionEvent e) {

    if (e.getSource() == btnDone) {

      try {
        editInfo();
      }
      catch (Exception ex) {

      }
      finally {

      }
    }
    else if (e.getSource() == btnCancel) {
      // Haven't done anything here yet
    }
  }

  public void run(String[] sDate, String[] sRest, int iIndex, int iCount) {

    date = sDate;
    rest = sRest;
    index = iIndex;
    count = iCount;

    JApplet applet = new EditDiary();
    frame.add(applet);
    frame.setTitle("Edit Diary Entry");
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(1280, 800);
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setVisible(true);

    populateInfo();
  }

  public static void main(String[] args) {

  }

  public void populateInfo() {

    // System.out.println(date[index]);
    // System.out.println(rest[index]);

    txtDate.setText(date[index]);
    txtDiary.setText(rest[index]);

  }

  public void editInfo() throws Exception {

    BufferedWriter out = new BufferedWriter(new FileWriter("Diary.txt"));

    date[index] = txtDate.getText();
    rest[index] = txtDiary.getText();

    for(int i = 0; i < count; ++i) {
      out.write(date[i]);
      out.write(rest[i]);
    }
  }    
}

I am having problems setting some text in a JTextArea and a JTextField...I am a little confused because I have set text in them before, as a matter of fact I have set them in a different class of this same program without any problems... I have printed out the String to check to make sure that it isn't null or just empty "" and my strings do print what they are supposed to but they don't set in the JTextField/Area..

Can anyone tell me why it is messing up?

(I have given you the whole code so that you are not confused by a method call but most of it is probably not necessary. All you really should need to focus on is the run() method and the populateInfo() method. populateInfo() is where it should be setting the test to the textArea and textField.)

Below is the code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import java.io.*;

public class EditDiary extends JApplet implements ActionListener {

  private JFrame frame = new JFrame();
  private String[] date;
  private String[] rest;
  private int index = 0;
  private int count = 0;

  private JLabel lblDate = new JLabel("Date");
  private JTextField txtDate = new JTextField();
  private JLabel lblDiary = new JLabel("Diary Entry");
  private JTextArea txtDiary = new JTextArea(20,30);

  private JButton btnDone = new JButton("Done");
  private JButton btnCancel = new JButton("Cancel");

  public EditDiary(){

    JPanel panel1 = new JPanel();

    panel1.add(lblDate);
    panel1.add(txtDate);
    panel1.add(lblDiary);
    panel1.add(txtDiary);
    panel1.add(btnDone);
    panel1.add(btnCancel);

    add(panel1);

    btnDone.addActionListener(this);
    btnCancel.addActionListener(this);

  }

  public void actionPerformed(ActionEvent e) {

    if (e.getSource() == btnDone) {

      try {
        editInfo();
      }
      catch (Exception ex) {

      }
      finally {

      }
    }
    else if (e.getSource() == btnCancel) {
      // Haven't done anything here yet
    }
  }

  public void run(String[] sDate, String[] sRest, int iIndex, int iCount) {

    date = sDate;
    rest = sRest;
    index = iIndex;
    count = iCount;

    JApplet applet = new EditDiary();
    frame.add(applet);
    frame.setTitle("Edit Diary Entry");
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(1280, 800);
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setVisible(true);

    populateInfo();
  }

  public static void main(String[] args) {

  }

  public void populateInfo() {

    // System.out.println(date[index]);
    // System.out.println(rest[index]);

    txtDate.setText(date[index]);
    txtDiary.setText(rest[index]);

  }

  public void editInfo() throws Exception {

    BufferedWriter out = new BufferedWriter(new FileWriter("Diary.txt"));

    date[index] = txtDate.getText();
    rest[index] = txtDiary.getText();

    for(int i = 0; i < count; ++i) {
      out.write(date[i]);
      out.write(rest[i]);
    }
  }    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2024-12-21 08:54:39

调用 run 的代码无关紧要...

当您遇到问题时,您不知道哪些代码相关或不相关。代码如何调用的上下文是相关的。是什么让你认为我们总是可以通过查看代码来解决问题。有时我们实际上需要执行代码,以便我们可以看到程序的实际逻辑流程等。

这不是创建 Applet 的方式。组件应该在小程序的 init() 方法中添加到小程序中。我建议您阅读如何制作Applet

您的代码的问题是您有 2 个 EditDiary 类的实例。一个是 Applet,另一个是您尝试添加到框架中的。然而,ActionListener 代码仅引用 Applet 文本组件,而不引用添加到框架的组件。

您需要重新设计您的程序。也许创建框架的代码应该是一个内部类,以便您可以创建属于框架而不是小程序的变量和侦听器。

The code that calls run is of no concern...

When you have a problem, you don't know what code is or isn't relevant. The context of how code is invoked is relevant. What makes you think we can always solve problems just by looking at the code. Sometimes we actually need to execute the code so we can see the actual logic flow of the program etc.

This is not how you create an Applet. Components should be added to the applet in the init() method of the applet. I suggest you read How to Make Applets.

The problem with your code is that you have 2 instances of the EditDiary class. One that is the Applet and one that you attempt to add to the frame. However the ActionListener code only references the Applet text components, not the components added to the frame.

You need a redesign of your program. Maybe the code to create the frame should be an inner class so that you can create variables and listeners that belong to the frame and not the applet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文