![](/img/trans.png)
[英]To append into jtextarea from another jtextarea using jbutton
[英]Getting text from a JTextArea using a JButton and a JTabbedPane
新手在这里尝试使用JTabbedPane制作简单的GUI。 我浏览了许多示例,但找不到解决方案。 基本上,我正在尝试将字符串打印到JTextArea。 尽管看起来很简单,但我无法使所有内容协同工作。 我了解局部变量和全局变量之间的区别,但是我认为这就是我的问题所在。 任何指导将不胜感激。 *请注意,我们无法为此项目使用布局管理器。
下面的代码代表具有JButton和JTextArea的选项卡的一部分。
//Text area that shows details. Scrolls.
JTextArea areaDeets = new JTextArea();
areaDeets.setBounds(65, 300, 250, 300 );
areaDeets.setText("");
JScrollPane scroll = new JScrollPane (areaDeets);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
panel2.add(areaDeets);
panel2.add(scroll);
areaDeets.addActionListener(new StopTest());
//Stop button--stops tests when pressed.
JButton stop = new JButton("Stop");
stop.setBounds(215, 650, 100, 40);
panel2.add(stop);
stop.addActionListener(new StopTest());
下面的代码是调用ActionListener的方法。
//Panel 1 - Stop, shows that the test has been stopped
static class StopTest implements ActionListener{
public void actionPerformed(ActionEvent e){
String stop = "The test has been stopped";
areaDeets.setText(stop);
panel2.repaint();
}
}
编辑:代码无法编译。 Eclipse说我不能在JTextField上调用addActionListener。
其他不相关的建议:
pack()
。 setBounds()
, setSize()
, setPreferredSize()
或任何类似的调用,因为它会因设置限制而阻止JScrollPane在JScrollPane中正常工作。 当添加更多行时,这将防止JTextArea扩展,有时会阻止滚动条的显示,或者如果滚动条显示了,则会阻止滚动条正常工作。 最好通过具有int参数的构造函数之一设置JTextArea的可见列和行。 repaint()
,因为textarea的模型将通知其视图(在GUI上呈现的部分)更改,然后该视图将自动调用自身的repaint 。 您可以将JTextArea
的引用传递给ActionListener
的构造函数:
public class StopTest implements ActionListener {
private JTextArea area;
public StopTest(JTextArea area) {
this.area = area;
}
public void actionPerformed(ActionEvent e) {
String stop = "The test has been stopped";
area.setText(stop);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.