1

不幸的是,我无法在主程序窗口(对象 g)中逐步输出。

主要课程的一部分:

/* Create a main window  of GUI*/  
    Gui g = new Gui();        
    g.showitself(); /*object is shown on the screen*/

    /*Check folder existing*/
    boolean inOutFilesFolderExists = (new File(vars.diskLetter+"//"+vars.ppmRootFolder+"//"+vars.inOutFilesFolder).exists());
         if (inOutFilesFolderExists)
            {
             System.out.println("Folder exists."); /*it is just for understanding if-operator works*/
             String textMessage = "\n"+"Folder " + vars.diskLetter+"//"+vars.ppmRootFolder+"//"+vars.inOutFilesFolder + "exists."+"\n" ; /*the message that I want to show in the main screen*/
             g.printmessage(logCurDateTime, textMessage); /*I am trying to show textMessage in the main GUI window by this method - g.printmessage  */
             }
         else
             { System.out.println("Folder doesn’t exists.");
            String textMessage = "Folder " + vars.diskLetter+"//"+vars.ppmRootFolder+"//"+vars.inOutFilesFolder + " doesn’t exist and will be created." ;
             g.printmessage(logCurDateTime, textMessage);
             new File(vars.diskLetter+"//"+vars.ppmRootFolder+"//"+vars.inOutFilesFolder).mkdirs();
             }
/** Now I am trying to create a new text message that should follow after previous text message */
            String textMessage = "Hello, Java Log.";
           // get to g object variables for showing 
            g.printmessage(logCurDateTime,  textMessage);

图形界面类:

        public class Gui {
                        Border solidBorder = BorderFactory.createLineBorder(Color.GRAY, 6);
                        Font font = new Font("Verdana", Font.PLAIN, 12);
                       //method which shows the main window of GUI
                        JFrame main_Window = new JFrame("PPM v.2.0");
                        public void showitself ()
                       {
                            main_Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            main_Window.setSize(800, 400);
                            main_Window.setVisible(true);
                       }
                        /*This method should show messages from main class */
                        void printmessage (String logCurDateTime, String textMessage)
                       {
                       JLabel label = new JLabel("      " + logCurDateTime + " - " + textMessage +"\n");
                        main_Window.getContentPane().add(label);
                        }
        }

结果,我只收到带有最后一条消息“Hello, Java Log.”的 GUI 主屏幕。据我了解,这意味着上一条消息被下一条重写,但我需要消息的串行输出(逐条消息),如下所示:

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

logCurDateTime - “你好,Java 日志。”

我也正在尝试将(使用eclipse.swt库)printmessage 方法重新编码为

void printmessage (String logCurDateTime, String textMessage)
             {

             JTextArea mainTextArea = new JTextArea();
             mainTextArea.append(logCurDateTime + textMessage +"/n");
             }

但这无济于事(根本没有任何消息)。

如何制作一个代码来一一显示来自主类的消息?

4

3 回答 3

4

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

于 2012-08-13T05:42:23.717 回答
3

默认情况下,内容窗格的布局是 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 也是一个可能的选项。

于 2012-08-13T10:35:31.180 回答
0

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

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");
    }

}
于 2012-08-15T11:16:01.350 回答