所有你需要做的是子類的JTextArea和使用一個接口。該界面可用於讓子文本區域知道主文本區域已更新。
您將需要兩個子類。一個將是主要文本區域,另一個將用於子面板。讓子面板實現一個接口,這樣當父母更新時,他們就會收到數據。然後他們可以處理它們如何選擇。
Main.java 這將運行演示
package Text;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
/**
*
* @author dvargo
*/
public class Main
{
public static void main(String [] args)
{
//build the main text area
JFrame mainFrame = new JFrame();
mainFrame.setSize(700,700);
mainFrame.setTitle("Main Frame");
mainFrame.setLayout(new GridLayout());
//build a sub text area
JFrame subFrameA = new JFrame();
subFrameA.setTitle("Sub Frame A");
subFrameA.setSize(300,300);
subFrameA.setLayout(new GridLayout());
subFrameA.setLocation(mainFrame.getX() + mainFrame.getWidth() + 25, mainFrame.getY());
//build another sub text area
JFrame subFrameB = new JFrame();
subFrameB.setTitle("Sub Frame b");
subFrameB.setSize(300,300);
subFrameB.setLayout(new GridLayout());
subFrameB.setLocation(subFrameA.getX() + subFrameA.getWidth() + 50, subFrameA.getY());
//this is the main text area. Anything typed into here will be sent to the sub text areas
TextField mainTextField = new TextField("Type here and text will appear in the sub frames!!!");
//this sub text area will just mirror the main text area
SubTextField subTextFieldA = new SubTextField();
//this sub text area will add a "-" to the begining of every line
SubTextField subTextFieldB = new SubTextField()
{
@Override
public void update(String text, char lastPressedChar)
{
super.update("- " + text.replace("\n", "\n- "),lastPressedChar);
}
};
//register the sub text areas with the main text areas
mainTextField.register(subTextFieldA);
mainTextField.register(subTextFieldB);
//add them to their frames
mainFrame.add(new JScrollPane(mainTextField));
subFrameA.add(new JScrollPane(subTextFieldA));
subFrameB.add(new JScrollPane(subTextFieldB));
//make everything visible
mainFrame.setVisible(true);
subFrameA.setVisible(true);
subFrameB.setVisible(true);
}
}
I_SubTextField.java 接口:
子文本區域與主文本區域
這裏是一個工作示例註冊所有要實施的子文本區域
package Text;
/**
* Interface to implement to be notified when the text has changed
* @author dvargo
*/
public interface I_SubTextField
{
public void update(String text, char lastChar);
}
TextField.java 以此作爲你的主要文本區域
package Text;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTextArea;
/**
* Text area
* @author dvargo
*/
public class TextField extends JTextArea
{
//holds all registered sub text areas that are registered for updates
List < I_SubTextField > registeredSubTextAreas = new ArrayList < I_SubTextField >();
/**
* Default constructor
*/
public TextField()
{
this("");
}
/**
* Constructor
* @param text Sets this text area to display this text
*/
public TextField(String text)
{
super(text);
addListener();
}
/**
* Registers a sub text area to get updates when this text area is updated
* @param subTextArea
*/
public void register(I_SubTextField subTextArea)
{
registeredSubTextAreas.add(subTextArea);
}
/**
* Unregisters a sub text area to stop receiving updates
* @param subTextField
*/
public void unRegister(I_SubTextField subTextField)
{
registeredSubTextAreas.remove(subTextField);
}
/**
* Notifies all registered classes when the data in the main window has changed
*/
private void addListener()
{
addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyReleased(java.awt.event.KeyEvent evt)
{
for (I_SubTextField registeredField : registeredSubTextAreas)
{
registeredField.update(TextField.this.getText(), evt.getKeyChar());
}
}
});
}
}
SubTextField.java 使用此爲所有子文本區域
package Text;
/**
* Represents a sub text area. This can be registered with a TextField to be notified
* when the data has been updated
* @author dvargo
*/
public class SubTextField extends TextField implements I_SubTextField
{
/**
* Default constructor
*/
public SubTextField()
{
super();
}
/**
* Constructor
* @param text Text to display in the text area
*/
public SubTextField(String text)
{
super(text);
}
/**
* Called when the parent TextField is updated. Handle the text as you want
* @param text The text for the main parent
* @param lastPressedChar The last char the user pressed
*/
public void update(String text, char lastPressedChar)
{
setText(text);
}
}
注意,SubTextField是一個子類文本字段,所以你可以將附加的SubTextFields註冊到SubTextField。
您可以將它們設置爲相互註冊並將未處理的文本發送給對方。然後,每個SubTextField都可以按照需要的方式處理文本。所有你需要做的是覆蓋更新()
我很好奇......你爲什麼要這麼做? – MirroredFate
這個想法就像是把你的課堂筆記和片段放到貼子上,然後貼在你的牆上。如果偶然發現郵件上有錯誤,您可以對其進行編輯並讓筆記做好(反之亦然)。這是一個新穎的項目。 – matthieu