不幸的是我无法在主程序窗口(对象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);
}
}
结果我收到GUI主屏幕只有最后一条消息“Hello,Java Log。”。 据我所知,它意味着比前一个消息被下一个重写,但我需要一个消息的串行输出(消息消息),如下所示:
logCurDateTime - 文件夹存在。 (或文件夹不存在,将被创建。)
logCurDateTime - “Hello,Java Log。”
此外,我正在尝试重新编码(使用eclipse.swt
库)printmessage方法
void printmessage (String logCurDateTime, String textMessage)
{
JTextArea mainTextArea = new JTextArea();
mainTextArea.append(logCurDateTime + textMessage +"/n");
}
但它没有帮助(根本没有任何消息)。
如何制作一个逐个显示主类消息的代码?
答案 0 :(得分:4)
printmessage
方法正在创建JTextArea
的新实例,但从不将该实例添加到容器中。看来文本区域应该在构造函数或initComponents()
方法中实例化并添加一次。
答案 1 :(得分: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也是一种可能的选择。
答案 2 :(得分: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");
}
}