从数组循环中的另一个类访问 JTextArea 的实例。最好的方法是什么?

发布于 2024-12-15 13:58:00 字数 1827 浏览 0 评论 0原文

我在概念上无法理解如何使我的应用程序正常工作。

我有 15 个 JTextField 组件、15 个 JButton 组件和 1 个 JTextArea。我希望每个按钮都将文本插入每个相应文本字段的文本区域内。

如果您希望查看解释此设置的图形 - [https://i.sstatic.net/bDN7l.png]此处1

我已成功将frame.finaltext 获取到课程主题类,但在actionPerformed 方法中看不到它。

获取frame.finaltext.append(textfields[i])

TIA 的最佳方法是什么 亚当

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import net.miginfocom.swing.MigLayout;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.Dimension;

    public class CurriculumSubject extends JPanel implements ActionListener{
    /**
     * This is an class that will hold  details for each subject. 
     */

    // First we want some variables 
    String subjectName;

    public CurriculumSubject (final JReport frame){
        MigLayout layout = new MigLayout("wrap 2");//, debug 4000");
        this.setLayout(layout);
        final JTextField [] textfields = new JTextField[15];
        JButton [] buttons = new JButton[15];

        for (int i=0;i<15;i++){
            String content = "This is a test. " + i; 
            textfields[i] = new JTextField(content);
            this.add(textfields[i], "width 500:700:1000");
            buttons[i] = new JButton(">>>");
            buttons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                    //Execute when button is pressed
                    String text = textfields[i].getText();
                    frame.finaltext.append(text);
                }});
            buttons[i].setPreferredSize(new Dimension(10,10));  
            this.add(buttons[i]);
        }
    }
}

I'm having problems conceptually getting my head around how to make my application work.

I have 15 JTextField components and 15 JButton components and 1 JTextArea. I want each button to insert the text inside the textarea from each corresponding textfield.

If you wish to see a graphic to explain this setup - [https://i.sstatic.net/bDN7l.png]here1

I've managed to get frame.finaltext to the Curriculum subject class, but it is not seen inside the actionPerformed method.

What is the best way of getting a hold of frame.finaltext.append(textfields[i])

TIA
Adam

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import net.miginfocom.swing.MigLayout;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.Dimension;

    public class CurriculumSubject extends JPanel implements ActionListener{
    /**
     * This is an class that will hold  details for each subject. 
     */

    // First we want some variables 
    String subjectName;

    public CurriculumSubject (final JReport frame){
        MigLayout layout = new MigLayout("wrap 2");//, debug 4000");
        this.setLayout(layout);
        final JTextField [] textfields = new JTextField[15];
        JButton [] buttons = new JButton[15];

        for (int i=0;i<15;i++){
            String content = "This is a test. " + i; 
            textfields[i] = new JTextField(content);
            this.add(textfields[i], "width 500:700:1000");
            buttons[i] = new JButton(">>>");
            buttons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                    //Execute when button is pressed
                    String text = textfields[i].getText();
                    frame.finaltext.append(text);
                }});
            buttons[i].setPreferredSize(new Dimension(10,10));  
            this.add(buttons[i]);
        }
    }
}

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

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

发布评论

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

评论(3

沐歌 2024-12-22 13:58:00

好吧,我想我明白你想要什么。您应该将 JTextArea 定义为类变量,这样您就可以在一个表单对象中拥有它的一个实例,然后只需将文本附加到该对象即可。方法示例:

public class CurriculumSubject extends JPanel implements ActionListener {

    private JTextArea mainArea = new JTextArea();

    public CurriculumSubject(final JReport frame) {
        MigLayout layout = new MigLayout("wrap 2");//, debug 4000");
        this.setLayout(layout);
        final JTextField[] textfields = new JTextField[15];
        JButton[] buttons = new JButton[15];

        for (int i = 0; i < 15; i++) {
            String content = "This is a test. " + i;
            textfields[i] = new JTextField(content);
            this.add(textfields[i], "width 500:700:1000");
            buttons[i] = new JButton(">>>");
            buttons[i].addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    //Execute when button is pressed
                    String text = textfields[i].getText();
                    addTextToFinalArea(text);
                }
            });
            buttons[i].setPreferredSize(new Dimension(10, 10));
            this.add(buttons[i]);
        }
    }

    public void addTextToFinalArea(String text) {
        mainArea.append(text);
    }
}

Ok, I think I understood what you wanted. You should define your JTextArea as class variable, so you have one instance of it in one form object, and then you just append text to that object. Example with method:

public class CurriculumSubject extends JPanel implements ActionListener {

    private JTextArea mainArea = new JTextArea();

    public CurriculumSubject(final JReport frame) {
        MigLayout layout = new MigLayout("wrap 2");//, debug 4000");
        this.setLayout(layout);
        final JTextField[] textfields = new JTextField[15];
        JButton[] buttons = new JButton[15];

        for (int i = 0; i < 15; i++) {
            String content = "This is a test. " + i;
            textfields[i] = new JTextField(content);
            this.add(textfields[i], "width 500:700:1000");
            buttons[i] = new JButton(">>>");
            buttons[i].addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    //Execute when button is pressed
                    String text = textfields[i].getText();
                    addTextToFinalArea(text);
                }
            });
            buttons[i].setPreferredSize(new Dimension(10, 10));
            this.add(buttons[i]);
        }
    }

    public void addTextToFinalArea(String text) {
        mainArea.append(text);
    }
}
掩于岁月 2024-12-22 13:58:00

我不知道 JReport 是什么,但为什么不在其中提供一个方法来接受字符串(或创建一个提供此方法的 JReport 子类):

class ReportThingy extends JReport {
   public void addText(String text) {
        finaltext.append(text);
   }
 }

更新:

您似乎有很多小部件需要跟踪。我建议引入 模型-视图-控制器 模式使您的应用程序更易于管理。在短期内,您可以尝试通过 setter 或作为构造函数参数使 Finaltext 小部件可供 CurriculumSubject 类使用。

I have no idea what JReport is, but why don't you provide a method in it to accept a string (or make a sub class of JReport that provides this method):

class ReportThingy extends JReport {
   public void addText(String text) {
        finaltext.append(text);
   }
 }

UPDATE:

You seem to have many widgets to keep track of. I would recommend introducing a Model-View-Controller pattern to make your application more manageable. In the short term, you can try making the finaltext widget available to the CurriculumSubject class through a setter or as a constructor argument.

池木 2024-12-22 13:58:00

我想知道编译器不会抱怨您的代码:您在匿名内部 ActionListener 实现中使用了非最终局部变量 i 。尝试将 i 存储在最终变量中,并在内部类中使用该变量:

for (int i = 0; i < 15; i++) {
...
  final int index = i;
  buttons[i].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
      //Execute when button is pressed
      String text = textfields[index].getText();
      frame.finaltext.append(text + "\n");
  }});
...
}

I'm wondering that the compiler isn't complaining about your code: You are using a non-final local variable i within your anonymous inner ActionListener implementation. Try storing i in a final variable and use that one in the inner class:

for (int i = 0; i < 15; i++) {
...
  final int index = i;
  buttons[i].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
      //Execute when button is pressed
      String text = textfields[index].getText();
      frame.finaltext.append(text + "\n");
  }});
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文