从数组循环中的另一个类访问 JTextArea 的实例。最好的方法是什么?
我在概念上无法理解如何使我的应用程序正常工作。
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我想我明白你想要什么。您应该将 JTextArea 定义为类变量,这样您就可以在一个表单对象中拥有它的一个实例,然后只需将文本附加到该对象即可。方法示例:
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:
我不知道 JReport 是什么,但为什么不在其中提供一个方法来接受字符串(或创建一个提供此方法的 JReport 子类):
更新:
您似乎有很多小部件需要跟踪。我建议引入 模型-视图-控制器 模式使您的应用程序更易于管理。在短期内,您可以尝试通过 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):
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.
我想知道编译器不会抱怨您的代码:您在匿名内部
ActionListener
实现中使用了非最终局部变量i
。尝试将i
存储在最终变量中,并在内部类中使用该变量:I'm wondering that the compiler isn't complaining about your code: You are using a non-final local variable
i
within your anonymous innerActionListener
implementation. Try storingi
in a final variable and use that one in the inner class: