【问题标题】:Java GUI - displaying text in a new window from another methodJava GUI - 从另一个方法在新窗口中显示文本
【发布时间】: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


【解决方案1】:

至于你的第一个问题,让textArea 成为一个实例变量。至于您的第二个,我认为您显示的代码没有任何问题。

【讨论】:

    【解决方案2】:

    文本区域应该在 drawScenariocompare 方法中可见,以便您能够更新它。以下是您如何做到这一点(只需对您的代码进行最少的更改)

    public class MyClass {
         private static JTextArea textArea = new JTextArea("", 10, 40);
         public void static drawScenario(){
             // ...
         }
         public static void compare(String txt1, String txt2){
             textArea.setText("here is some text...");
         }    
    }
    

    现在,如果您想正确实现您的要求,您应该这样做:

    1. drawScenariocompare 以及所有 UI 元素都可以设为非静态
    2. 创建您自己的框架(扩展JFrame)类,它将封装所有 UI 元素的创建。在您的 drawScenario() 中,您可以创建框架并显示它。

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 2015-03-24
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多