2013-04-02 79 views
0

我目前有一个程序,在不同的行中在'System.out.println()'语句的屏幕上打印文本行。我是Java,Eclipse和WindowBuilder的新手。控制台输出到WindowBuilder GUI

我现在正在为此程序添加一个GUI。我能够使用按钮创建GUI,这些按钮正常工作。我的问题是,我想要将打印到eclipse控制台(或命令行)的所有内容都打印到GUI中的文本框中,而不是实时打印。我怎样才能轻松做到这一点?

package Onur; 


import java.awt.BorderLayout; 

public class BehaSendDFGUI extends JFrame { 

    private BehaviourSendWithDF1 myAgent; // Reference to the agent class 
    private JPanel contentPane; 
    private JDesktopPane desktopPane; 
    private JButton btnMessage; 
    private JButton btnMessage_1; 
    private JButton btnMessage_2; 
    private JButton btnMessage_3; 
    private JTextArea textArea = new JTextArea(); 

    public void setAgent(BehaviourSendWithDF1 a) { 
     myAgent = a; // provide the value of the reference of BehaviourSendWithDF1 class here 

    } 

    private void updateTextArea(final String text) { 
      SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       textArea.append(text); 
      } 
      }); 
     } 

     private void redirectSystemStreams() { 
      OutputStream out = new OutputStream() { 
      @Override 
      public void write(int b) throws IOException { 
       updateTextArea(String.valueOf((char) b)); 
      } 

      @Override 
      public void write(byte[] b, int off, int len) throws IOException { 
       updateTextArea(new String(b, off, len)); 
      } 

      @Override 
      public void write(byte[] b) throws IOException { 
       write(b, 0, b.length); 
      } 
      }; 

      System.setOut(new PrintStream(out, true)); 
      System.setErr(new PrintStream(out, true)); 
     } 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        BehaSendDFGUI frame = new BehaSendDFGUI(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public BehaSendDFGUI() { 


     setTitle("Behaviour Sender"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 523, 398); 

     JMenuBar menuBar = new JMenuBar(); 
     setJMenuBar(menuBar); 

     JMenu mnFile = new JMenu("File"); 
     menuBar.add(mnFile); 

     JMenuItem mntmExit = new JMenuItem("Exit"); 
     mnFile.add(mntmExit); 

     JMenu mnAbout = new JMenu("About"); 
     menuBar.add(mnAbout); 

     JMenuItem mntmAboutThisGui = new JMenuItem("About This GUI"); 
     mnAbout.add(mntmAboutThisGui); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane); 

     JToolBar toolBar = new JToolBar(); 
     toolBar.setFloatable(false); 
     contentPane.add(toolBar, BorderLayout.CENTER); 

     desktopPane = new JDesktopPane(); 
     toolBar.add(desktopPane); 

     btnMessage = new JButton("Send Message 1"); 
     btnMessage.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       BehaviourSendWithDF1.STEP = "1"; 
       System.out.println("Button Pressed => STEP = " + BehaviourSendWithDF1.STEP); 
       myAgent.behaSend();    
      } 

     }); 
     btnMessage.setBounds(10, 11, 111, 23); 
     desktopPane.add(btnMessage); 

     btnMessage_1 = new JButton("Send Message 2"); 
     btnMessage_1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       BehaviourSendWithDF1.STEP = "2"; 
       System.out.println("Button Pressed => STEP = " + BehaviourSendWithDF1.STEP); 
       myAgent.behaSend(); 
      } 
     }); 
     btnMessage_1.setBounds(131, 11, 111, 23); 
     desktopPane.add(btnMessage_1); 

     btnMessage_2 = new JButton("Send Message 3"); 
     btnMessage_2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       BehaviourSendWithDF1.STEP = "3"; 
       System.out.println("Button Pressed => STEP = " + BehaviourSendWithDF1.STEP); 
       myAgent.behaSend(); 
      } 
     }); 
     btnMessage_2.setBounds(252, 11, 111, 23); 
     desktopPane.add(btnMessage_2); 

     btnMessage_3 = new JButton("Send Message 4"); 
     btnMessage_3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       BehaviourSendWithDF1.STEP = "4"; 
       System.out.println("Button Pressed => STEP = " + BehaviourSendWithDF1.STEP); 
       myAgent.behaSend(); 
      } 
     }); 
     btnMessage_3.setBounds(373, 11, 111, 23); 
     desktopPane.add(btnMessage_3); 

     JButton btnExitGui = new JButton("Exit GUI"); 
     btnExitGui.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       dispose(); 
      } 
     }); 
     btnExitGui.setBounds(189, 293, 130, 23); 
     desktopPane.add(btnExitGui); 

     JTextPane txtpnConsoleOutput = new JTextPane(); 
     txtpnConsoleOutput.setText("Console Output:"); 
     txtpnConsoleOutput.setBounds(10, 45, 101, 20); 
     desktopPane.add(txtpnConsoleOutput); 

     JScrollPane scrollPane = new JScrollPane(); 
     scrollPane.setBounds(10, 76, 475, 206); 
     desktopPane.add(scrollPane); 
     scrollPane.setViewportView(textArea); 

     redirectSystemStreams(); 

    } 
} 

我看到的NetBeans的这个解决方案,但不能申请它的WindowBuilder:提前

http://unserializableone.blogspot.com/2009/01/redirecting-systemout-and-systemerr-to.html

感谢。

编辑:该代码的工作版本在问题中编辑。感谢所有的帮助。

+0

的解决方案,重定向的System.out和System.err * *是相同的,这有绝对无关的NetBeans或WindowsBuilder或任何其他Swing windows构建工具以及所有与Swing相关的工具。如果该解决方案对您无效,那么您需要告诉我们更多信息,包括确切地说明或不能发挥作用的原因。 –

+0

我尝试将代码复制并粘贴到Eclipse中;但得到了很多我无法解决的错误。 –

+0

当然,你不能那样做。不要盲目地复制和粘贴代码,特别是你不明白的代码。相反,要学习代码试图做什么,并使用这些概念编写自己的代码。你的问题需要更多地关注你不了解的内容,否则这个问题不会对你有所帮助。 –

回答

1

你的问题包括

  1. 你试图使用一个不存在,textPane一个变量,
  2. 你的程序不具有可以显示文本的文本组件。我建议您至少添加一个JTextArea,以便您可以将输出重定向到它。如果你愿意,可以称它为textPane,但不管你怎么称呼它,你至少需要东西,它可以显示多行文本。
  3. 同样,您提供的链接中描述的一般技术是正确的 - 您需要将System.out和System.err重定向到创建的OutputStream,但您必须小心更新Swing组件仅在Swing事件线程中使用SwingUtilities.invokeLater(new Runnable() {...}) ...
  4. 因此正确使用该文章的建议工作。所以,请继续。

再次,阅读Swing教程并放下Windows builder代码生成器。代码生成器可以节省您的时间,但是如果您在对Swing库有很好的理解之前使用它们,那么在您最需要的时候就会遇到大问题,而不是最基本的GUI和行为。

编辑
你似乎是试图调用append(...)上一个JTextField,而这个类不允许该消息。我建议

  • 你大大简化类,所以它只有最基本的GUI来演示的System.out的偏转与犯错,并
  • 再次使用一个JTextArea,不是一个JTextField。

编辑2
你问:

I couldn't solve the 'textArea.append(text);' scope error: textArea cannot be resolved.

记下你声明 textarea的变量。由于它不是在类中声明的,而是在方法或构造函数中声明的,所以在课程的其他地方是不可见的。解决办法是在课堂上宣布,而不是在别处。

编辑3
例如,

import java.awt.event.ActionEvent; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.PrintStream; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class RedirectOut extends JPanel { 
    private static final int BUTTON_COUNT = 4; 
    private JTextArea textArea = new JTextArea(20, 20); 
    private SomeAgent myAgent; 

    public RedirectOut() { 
     redirectSystemStreams(); 

     setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); 

     for (int i = 0; i < BUTTON_COUNT; i++) { 
     final int count = i + 1; 
     JButton button = new JButton(new AbstractAction("Send Message " + count){ 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       myAgent.setStep(String.valueOf(count)); 
       System.out.println("Button Pressed => STEP = " 
        + myAgent.getStep()); 
       myAgent.behaSend(); 
      } 
     }); 
     JPanel btnPanel = new JPanel(); 
     btnPanel.add(button); 
     add(btnPanel); 
     } 
     add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); 
    } 

    public void setAgent(SomeAgent agent) { 
     this.myAgent = agent; 
    } 

    public void updateTextArea(final String text) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      textArea.append(text); 
     } 
     }); 
    } 

    private void redirectSystemStreams() { 
     OutputStream out = new OutputStream() { 
     @Override 
     public void write(int b) throws IOException { 
      updateTextArea(String.valueOf((char) b)); 
     } 

     @Override 
     public void write(byte[] b, int off, int len) throws IOException { 
      updateTextArea(new String(b, off, len)); 
     } 

     @Override 
     public void write(byte[] b) throws IOException { 
      write(b, 0, b.length); 
     } 
     }; 

     System.setOut(new PrintStream(out, true)); 
     System.setErr(new PrintStream(out, true)); 
    } 

    private static void createAndShowGui() { 
     RedirectOut redirectOut = new RedirectOut(); 
     redirectOut.setAgent(new SomeAgent()); 

     JFrame frame = new JFrame("RedirectOut"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(redirectOut); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

我得到了1和2并修复。我不明白你的意思是数字4.我尝试更正范围错误,并在创建文本区域后在代码中调用redirectSystemStreams()。没有编译错误,但是当我运行GUI并且不打印任何东西时,GUI会卡住。 –

+0

@OnurDogu:请参阅编辑 –

+0

另外为什么使用JDesktopPane? –