不幸的是,我无法在主程序窗口(对象 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");
}
但这无济于事(根本没有任何消息)。
如何制作一个代码来一一显示来自主类的消息?