Java 如何在UI窗口中按来自主类的消息显示消息?

Java 如何在UI窗口中按来自主类的消息显示消息?,java,swing,swt,awt,Java,Swing,Swt,Awt,不幸的是,我不能在主程序窗口(对象g)中进行分步输出 主要类别的一部分: GUI类: 因此,我只收到GUI主屏幕上的最后一条消息“Hello,Java Log.”。 据我所知,这意味着上一条消息会被下一条消息重写,但我需要一个串行消息输出(一条消息一条消息),如下所示: logCurDateTime-文件夹存在。(或文件夹不存在,将创建。) logCurDateTime-“你好,Java日志。” 我还尝试将printmessage方法重新编码(使用eclipse.swtlibrary)为 voi

不幸的是,我不能在主程序窗口(对象g)中进行分步输出

主要类别的一部分: GUI类: 因此,我只收到GUI主屏幕上的最后一条消息“Hello,Java Log.”。 据我所知,这意味着上一条消息会被下一条消息重写,但我需要一个串行消息输出(一条消息一条消息),如下所示:

logCurDateTime-文件夹存在。(或文件夹不存在,将创建。)

logCurDateTime-“你好,Java日志。”

我还尝试将printmessage方法重新编码(使用
eclipse.swt
library)为

void printmessage (String logCurDateTime, String textMessage)
             {

             JTextArea mainTextArea = new JTextArea();
             mainTextArea.append(logCurDateTime + textMessage +"/n");
             }
但这没有帮助(根本没有任何消息)


如何生成一个代码来逐个显示来自主类的消息?

方法是创建
JTextArea
的新实例,但从不将该实例添加到容器中。似乎文本区域应该在构造函数或
initComponents()
方法中实例化并添加一次。

方法
printmessage
正在创建
JTextArea
的新实例,但决不将该实例添加到容器中。似乎应该在构造函数或
initComponents()
方法中实例化并添加一次文本区域。

默认情况下,内容窗格的布局是BorderLayout。您可以用GridLayout或BoxLayout替换它

// a GridLayout
main_Window.getContentPane().setLayout(new GridLayout(0, 1));
// or a BoxLayout
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
main_Window.setContentPane(panel);

GridBagLayout也是一个可能的选项。

默认情况下,内容窗格的布局为BorderLayout。您可以用GridLayout或BoxLayout替换它

// a GridLayout
main_Window.getContentPane().setLayout(new GridLayout(0, 1));
// or a BoxLayout
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
main_Window.setContentPane(panel);
GridBagLayout也是一个可能的选择。

谢谢大家! 此类在框架中按消息显示消息:

public class Gui {
    Border solidBorder = BorderFactory.createLineBorder(Color.GRAY, 6);
    Font font = new Font("Verdana", Font.PLAIN, 12);


    JFrame main_Window = new JFrame("PPM v.2.0");
    JTextArea mainTextArea = new JTextArea(5, 200);

    public void showitself ()
    {

            main_Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            main_Window.setSize(800, 400);
            main_Window.setVisible(true);
            main_Window.add(mainTextArea);


    }


    public void printMessage(final String textMessage) {
        if (EventQueue.isDispatchThread()) {
            appendMessage(textMessage);
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    appendMessage(textMessage);
                }
            });
        }
    }

    private void appendMessage(String message) {
        mainTextArea.append(message);
        mainTextArea.append("\n");
    }

}
谢谢大家! 此类在框架中按消息显示消息:

public class Gui {
    Border solidBorder = BorderFactory.createLineBorder(Color.GRAY, 6);
    Font font = new Font("Verdana", Font.PLAIN, 12);


    JFrame main_Window = new JFrame("PPM v.2.0");
    JTextArea mainTextArea = new JTextArea(5, 200);

    public void showitself ()
    {

            main_Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            main_Window.setSize(800, 400);
            main_Window.setVisible(true);
            main_Window.add(mainTextArea);


    }


    public void printMessage(final String textMessage) {
        if (EventQueue.isDispatchThread()) {
            appendMessage(textMessage);
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    appendMessage(textMessage);
                }
            });
        }
    }

    private void appendMessage(String message) {
        mainTextArea.append(message);
        mainTextArea.append("\n");
    }

}

将main中的逻辑移到一个方法中,可能在一个单独的类中。创建包含该方法的对象实例,并使用GUI提供的任何参数调用它。(我可能不理解这个问题。)Andrew,问题是如何在user interface window.BTW中逐个显示来自主类的消息-为什么从基于Swing的GUI调用SWT方法?它只是试图使用SWT库来解决这个问题。严格地说,我尝试使用SWING。将main中的逻辑移到一个方法中,可能在一个单独的类中。创建包含该方法的对象实例,并使用GUI提供的任何参数调用它。(我可能不理解这个问题。)Andrew,问题是如何在user interface window.BTW中逐个显示来自主类的消息-为什么从基于Swing的GUI调用SWT方法?它只是试图使用SWT库来解决这个问题。○严格地说,我试着使用秋千。+1个很棒的接球。我通常不会仔细看代码片段,而是更喜欢一个可以包装我的编译器的。我通常不会仔细查看代码段,而是更喜欢一个可以包装编译器的代码段。