我使用这个答案中的代码来提供日志记录功能,它运行得很好!谢谢古斯塔夫。
从不同的地方向摇摆JTextArea发送消息
我想使用这种方法,并添加函数:writeEntry(String,String)来写入一个ArrayList,我希望可以从swing访问它;即,单击"show"并让它将单词对列表写入JText元素。
所以在gustafc的代码中添加一点。
接口:
package com.example.logging;
public interface SimpleActivityLogger {
void logAction(String message);
void writeEntry(String s1); //*** I added this ***
}
按照gustafc的示例将其传递给另一个类:
public class SimpleComponentLogger implements SimpleActivityLogger{
private JTextComponent target;
private ArrayList data;
public SimpleComponentLogger(JTextComponent target, ArrayList data){
this.target = target;
this.data = data;
}
public void logAction(final String message){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
target.setText(String.format("%s%s%n", new Object[]{target.getText(), message}));
}
});
}
public void writeEntry(String s1){
data.add(s1);
System.out.println("data array length: " + data.size());
}
}
然后进行一个实现:
// this originally extended NotesThread,
// but I assume you won't have that on your system
public class LookupIterator3 extends Thread {
private SimpleActivityLogger logger;
// Constructor that passes logger instance
public LookupIterator3(SimpleActivityLogger logger){
this.logger = logger;
}
public void doLookup(){
this.start();
}
public void run() {
String[] words = {"the", "quick", "smart", "fox", "jumps", "over", "the", "lazy", "dog"};
for(int i=0; i<words.length; i++){
synchronized(words){
try{
logger.logAction(words[i]);
words.wait(500);
}
catch(InterruptedException ie){ie.printStackTrace();}
}
logger.writeEntry(words[i]);
}
}
}
然而,当我尝试从swing访问ArrayList时,它返回size()=0。这里有一大块摆动,但你应该可以复制并粘贴它:
public class MySwingTest extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField filename = new JTextField();
private JTextField dir = new JTextField();
private JTextPane output, answersPane;
private JScrollPane scroller;
private SimpleComponentLogger logger;
private ArrayList answer;
public MySwingTest() {
JMenu fileMenu;
JMenuBar menuBar;
JMenuItem menuOpen, menuExit;
JButton answerButton = new JButton("show answers");
answerButton.addActionListener(new MyListener());
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
menuOpen = new JMenuItem("open file");
menuExit = new JMenuItem("exit");
answer = new ArrayList();
output = new JTextPane();
logger = new SimpleComponentLogger(output, answer);
scroller = new JScrollPane(output, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
output.setEditable(false);
answer = new ArrayList();
answersPane = new JTextPane();
answersPane.setSize(100, 200);
answersPane.setEditable(false);
fileMenu.add(menuOpen);
fileMenu.add(menuExit);
menuOpen.addActionListener(new MyListener());
menuExit.addActionListener(new MyListener());
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 1));
p.add(filename);
p.add(dir);
Container cp = getContentPane();
cp.add(menuBar,BorderLayout.NORTH);
cp.add(p);
cp.add(scroller, BorderLayout.CENTER);
cp.add(answersPane, BorderLayout.WEST);
cp.add(answerButton, BorderLayout.SOUTH);
dir.setEditable(false);
filename.setEditable(false);
}
// Inner class listener
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String eventCommand = e.getActionCommand();
if(eventCommand.equals("exit")){
System.exit(0);
}
if(eventCommand.equals("show answers")){
String entry = (String)answer.get(0);
answersPane.setText(entry);
}
else {
LookupIterator3 lu3 = new LookupIterator3(logger);
lu3.doLookup();
}
}
}
public static void main(String[] args) {
run(new MySwingTest(), 450, 600);
}
public static void run(JFrame frame, int width, int height) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
}
很抱歉问了这么长的问题,但这让我头疼!有什么建议吗?(我知道我会因为写一个长qn而疲惫不堪,但我不知道我能遗漏什么)
好的。长编辑完成。打开婴儿并从菜单中选择"打开文件"。它将在没有文件选择器的情况下运行。
问题实际上很简单:在MySwingTest
构造函数中,您有两行:
answer = new ArrayList();
在创建记录器之前和之后各一次。只需拨打第二个电话,它就会正常工作。
注意:在实例之间共享ArrayList确实是一种糟糕的做法(可响应性分布在几个类中)。如果你只让你的记录器直接创建自己的ArrayList,并用getter提供对它的访问,这会简单得多。更好的是,它可以直接提供对其包含的对象的访问(防止其他实例修改ArrayList的内容)。
注意2:考虑使用接口声明而不是类声明:使用List
而不是ArrayList
以减少耦合。
注3:我强烈建议您键入您的收藏:List<String> answer = new ArrayList<String>();