【发布时间】:2012-07-07 09:34:28
【问题描述】:
我有以下界面结构:一个框架,我可以在其中浏览和选择一些文件(在类文件中),当我按下按钮时,它会读取文件(在单独的类文件中)并将它们发送给一些处理(在另一个类文件中,第 3 个)。处理本身与这个问题无关。
当我按下前面提到的处理按钮时,会启动一个新窗口(框架)。在那个窗口中,我有一个文本区域,我想在处理过程中显示一些控制台输出,然后显示另一个文本。
第二帧的绘制方法位于第三类文件中,处理方法如下:
public static void drawScenario(){
final JPanel mainPanel2 = new JPanel();
JPanel firstLine = new JPanel();
JPanel secLine = new JPanel();
mainPanel2.setLayout(new BoxLayout(mainPanel2, BoxLayout.Y_AXIS));
mainPanel2.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
mainPanel2.add(Box.createVerticalGlue());
firstLine.setLayout(new BoxLayout(firstLine, BoxLayout.X_AXIS));
firstLine.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
firstLine.add(Box.createVerticalGlue());
secLine.setLayout(new BoxLayout(secLine, BoxLayout.X_AXIS));
secLine.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
secLine.add(Box.createVerticalGlue());
JTextArea textArea = new JTextArea("", 10, 40);
textArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
textArea.setEditable(false);
JLabel label1 = new JLabel("Processing results:");
firstLine.add(Box.createRigidArea(new Dimension(5,0)));
firstLine.add(label1);
firstLine.add(Box.createRigidArea(new Dimension(5,0)));
secLine.add(textArea);
secLine.add(Box.createRigidArea(new Dimension(5,0)));
mainPanel2.add(firstLine);
mainPanel2.add(Box.createRigidArea(new Dimension(0, 30)));
mainPanel2.add(secLine);
mainPanel2.add(Box.createRigidArea(new Dimension(0, 20)));
JFrame frame = new JFrame("Test results");
frame.setSize(400, 300);
frame.setLocation(50,50);
frame.setVisible( true );
frame.add(mainPanel2);
frame.pack();
}
处理方法(public static void compare(String txt1, String txt2))也位于同一个文件中,在drawScenario()方法的下方。我的问题是,如何将文本从 compare() 打印到 drawScenario() 方法的 TextArea?
此外,虽然我在 compare() 之前调用了 drawScenario(),但在处理过程中,窗口并没有完全绘制自己(它显示了一个黑色的列,并且没有在其中绘制 TextArea)。有什么办法可以解决吗?
谢谢!
【问题讨论】:
-
为了获得更好的帮助,请尽快发布SSCCE,您将只使用一个 JComponent,而不是整个 GUI
-
不要阻塞 EDT(事件调度线程)——当这种情况发生时,GUI 将“冻结”。为长时间运行的任务实现
SwingWorker。有关详细信息,请参阅Concurrency in Swing。 -
好的:我如何在 TextArea(类似于 textArea.setText("bla"))中使用另一种方法而不是在其中绘制 TextArea 的方法写入?
标签: java swing user-interface jtextcomponent