我目前有一個程序,在不同的行中在'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
感謝。
編輯:該代碼的工作版本在問題中編輯。感謝所有的幫助。
的解決方案,重定向的System.out和System.err * *是相同的,這有絕對無關的NetBeans或WindowsBuilder或任何其他Swing windows構建工具以及所有與Swing相關的工具。如果該解決方案對您無效,那麼您需要告訴我們更多信息,包括確切地說明或不能發揮作用的原因。 –
我嘗試將代碼複製並粘貼到Eclipse中;但得到了很多我無法解決的錯誤。 –
當然,你不能那樣做。不要盲目地複製和粘貼代碼,特別是你不明白的代碼。相反,要學習代碼試圖做什麼,並使用這些概念編寫自己的代碼。你的問題需要更多地關注你不瞭解的內容,否則這個問題不會對你有所幫助。 –