现在在光标当前位置插入字符已经知道了。如下:
=================================================================
String str="要显示的字符。
int i=EditArea.getCaret().getDot();
EditArea.insert("我在这插入一串字符了:"+str,i); //多个窗口中的EditArea名字变化
=================================================================
现在地问题就是上面的EditArea因在多个窗口名字变化。比如我现在有两个内置窗口,他们的名字分别是InterFrm1,InterFrm2。两个窗口中分别有一个JtextArea组件,怎么样才能在我按下按钮的时候又能判断他是否是JtextAread类组件,又能把字符插入到所在窗口的光标当前位置处。
谢谢!
9 个解决方案
#1
add FocusListener to the JTextArea components
focusGained(focusEvent e){
//a JtextArea gained focus, insert str
}
focusGained(focusEvent e){
//a JtextArea gained focus, insert str
}
#2
友情UP!
#3
友情UP!
#4
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test {
JFrame frame ;
JPanel pane ;
JScrollPane sPane1,sPane2,sPane3 ;
JTextArea textArea1,textArea2,textArea3,textTemp ;
JButton button ;
String temp ;
public Test() {
temp = "我们都有一个家,名字叫中国。" ;
jbInit();
}
public void jbInit(){
frame = new JFrame ("获取焦点插入") ;
pane = new JPanel() ;
button = new JButton ("插入") ;
textArea1 = new JTextArea(3,25) ;
textArea2 = new JTextArea(3,25) ;
textArea3 = new JTextArea(3,25) ;
sPane1 = new JScrollPane(textArea1);
sPane2 = new JScrollPane(textArea2);
sPane3 = new JScrollPane(textArea3);
frame.getContentPane().add(pane, BorderLayout.CENTER);
pane.add(sPane1, null) ;
pane.add(sPane2, null) ;
pane.add(sPane3, null) ;
pane.add(button, null) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
textArea1.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea2.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea3.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textTemp==textArea1){
textArea1.insert(temp,textArea1.getText().length());
}
if(textTemp==textArea2){
textArea2.insert(temp,textArea2.getText().length());
}
if(textTemp==textArea3){
textArea3.insert(temp,textArea3.getText().length());
}
}
});
frame.setSize(300,250);
frame.setVisible(true);
}
public void textArea(FocusEvent focusEvent){
textTemp = (JTextArea) focusEvent.getComponent() ;
}
public static void main(String[] args) {
Test test = new Test();
}
}
<------ 树欲静而风不止 ------>
import java.awt.*;
import java.awt.event.*;
public class Test {
JFrame frame ;
JPanel pane ;
JScrollPane sPane1,sPane2,sPane3 ;
JTextArea textArea1,textArea2,textArea3,textTemp ;
JButton button ;
String temp ;
public Test() {
temp = "我们都有一个家,名字叫中国。" ;
jbInit();
}
public void jbInit(){
frame = new JFrame ("获取焦点插入") ;
pane = new JPanel() ;
button = new JButton ("插入") ;
textArea1 = new JTextArea(3,25) ;
textArea2 = new JTextArea(3,25) ;
textArea3 = new JTextArea(3,25) ;
sPane1 = new JScrollPane(textArea1);
sPane2 = new JScrollPane(textArea2);
sPane3 = new JScrollPane(textArea3);
frame.getContentPane().add(pane, BorderLayout.CENTER);
pane.add(sPane1, null) ;
pane.add(sPane2, null) ;
pane.add(sPane3, null) ;
pane.add(button, null) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
textArea1.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea2.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea3.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textTemp==textArea1){
textArea1.insert(temp,textArea1.getText().length());
}
if(textTemp==textArea2){
textArea2.insert(temp,textArea2.getText().length());
}
if(textTemp==textArea3){
textArea3.insert(temp,textArea3.getText().length());
}
}
});
frame.setSize(300,250);
frame.setVisible(true);
}
public void textArea(FocusEvent focusEvent){
textTemp = (JTextArea) focusEvent.getComponent() ;
}
public static void main(String[] args) {
Test test = new Test();
}
}
<------ 树欲静而风不止 ------>
#5
不好意思,上面的Button事件内写成
textTemp.insert(temp,textArea1.getText().length());
<------ 树欲静而风不止 ------>
textTemp.insert(temp,textArea1.getText().length());
<------ 树欲静而风不止 ------>
#6
又写错了,哎~~~~~~
textTemp.insert(temp,textTemp.getText().length());
<------ 树欲静而风不止 ------>
textTemp.insert(temp,textTemp.getText().length());
<------ 树欲静而风不止 ------>
#7
>>>>>mq612(理想)
非常感谢你的帮助,我得问题在你的帮助下已经完成了,主要就是动态得到组件名字的问题。我发现在这儿交流着对提高是有很大帮助的,你学习java多长时间了。
非常感谢你的帮助,我得问题在你的帮助下已经完成了,主要就是动态得到组件名字的问题。我发现在这儿交流着对提高是有很大帮助的,你学习java多长时间了。
#8
〉〉〉〉wobelisk() 也谢谢你的帮助,你们的帮助就是我学习的动力。同时也谢谢参与地同志们。
#9
呵呵~~~~别客气,一起学习嘛,Java如海呀。
<------ 树欲静而风不止 ------>
<------ 树欲静而风不止 ------>
#1
add FocusListener to the JTextArea components
focusGained(focusEvent e){
//a JtextArea gained focus, insert str
}
focusGained(focusEvent e){
//a JtextArea gained focus, insert str
}
#2
友情UP!
#3
友情UP!
#4
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test {
JFrame frame ;
JPanel pane ;
JScrollPane sPane1,sPane2,sPane3 ;
JTextArea textArea1,textArea2,textArea3,textTemp ;
JButton button ;
String temp ;
public Test() {
temp = "我们都有一个家,名字叫中国。" ;
jbInit();
}
public void jbInit(){
frame = new JFrame ("获取焦点插入") ;
pane = new JPanel() ;
button = new JButton ("插入") ;
textArea1 = new JTextArea(3,25) ;
textArea2 = new JTextArea(3,25) ;
textArea3 = new JTextArea(3,25) ;
sPane1 = new JScrollPane(textArea1);
sPane2 = new JScrollPane(textArea2);
sPane3 = new JScrollPane(textArea3);
frame.getContentPane().add(pane, BorderLayout.CENTER);
pane.add(sPane1, null) ;
pane.add(sPane2, null) ;
pane.add(sPane3, null) ;
pane.add(button, null) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
textArea1.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea2.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea3.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textTemp==textArea1){
textArea1.insert(temp,textArea1.getText().length());
}
if(textTemp==textArea2){
textArea2.insert(temp,textArea2.getText().length());
}
if(textTemp==textArea3){
textArea3.insert(temp,textArea3.getText().length());
}
}
});
frame.setSize(300,250);
frame.setVisible(true);
}
public void textArea(FocusEvent focusEvent){
textTemp = (JTextArea) focusEvent.getComponent() ;
}
public static void main(String[] args) {
Test test = new Test();
}
}
<------ 树欲静而风不止 ------>
import java.awt.*;
import java.awt.event.*;
public class Test {
JFrame frame ;
JPanel pane ;
JScrollPane sPane1,sPane2,sPane3 ;
JTextArea textArea1,textArea2,textArea3,textTemp ;
JButton button ;
String temp ;
public Test() {
temp = "我们都有一个家,名字叫中国。" ;
jbInit();
}
public void jbInit(){
frame = new JFrame ("获取焦点插入") ;
pane = new JPanel() ;
button = new JButton ("插入") ;
textArea1 = new JTextArea(3,25) ;
textArea2 = new JTextArea(3,25) ;
textArea3 = new JTextArea(3,25) ;
sPane1 = new JScrollPane(textArea1);
sPane2 = new JScrollPane(textArea2);
sPane3 = new JScrollPane(textArea3);
frame.getContentPane().add(pane, BorderLayout.CENTER);
pane.add(sPane1, null) ;
pane.add(sPane2, null) ;
pane.add(sPane3, null) ;
pane.add(button, null) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
textArea1.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea2.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea3.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textTemp==textArea1){
textArea1.insert(temp,textArea1.getText().length());
}
if(textTemp==textArea2){
textArea2.insert(temp,textArea2.getText().length());
}
if(textTemp==textArea3){
textArea3.insert(temp,textArea3.getText().length());
}
}
});
frame.setSize(300,250);
frame.setVisible(true);
}
public void textArea(FocusEvent focusEvent){
textTemp = (JTextArea) focusEvent.getComponent() ;
}
public static void main(String[] args) {
Test test = new Test();
}
}
<------ 树欲静而风不止 ------>
#5
不好意思,上面的Button事件内写成
textTemp.insert(temp,textArea1.getText().length());
<------ 树欲静而风不止 ------>
textTemp.insert(temp,textArea1.getText().length());
<------ 树欲静而风不止 ------>
#6
又写错了,哎~~~~~~
textTemp.insert(temp,textTemp.getText().length());
<------ 树欲静而风不止 ------>
textTemp.insert(temp,textTemp.getText().length());
<------ 树欲静而风不止 ------>
#7
>>>>>mq612(理想)
非常感谢你的帮助,我得问题在你的帮助下已经完成了,主要就是动态得到组件名字的问题。我发现在这儿交流着对提高是有很大帮助的,你学习java多长时间了。
非常感谢你的帮助,我得问题在你的帮助下已经完成了,主要就是动态得到组件名字的问题。我发现在这儿交流着对提高是有很大帮助的,你学习java多长时间了。
#8
〉〉〉〉wobelisk() 也谢谢你的帮助,你们的帮助就是我学习的动力。同时也谢谢参与地同志们。
#9
呵呵~~~~别客气,一起学习嘛,Java如海呀。
<------ 树欲静而风不止 ------>
<------ 树欲静而风不止 ------>