현재 다른 줄의 'System.out.println()'문에서 화면에 텍스트 줄을 인쇄하는 프로그램이 있습니다. Java, Eclipse 및 WindowBuilder를 처음 사용합니다.콘솔 출력을 WindowBuilder GUI
이제이 프로그램에 GUI를 추가하고 있습니다. OK 버튼을 사용하여 GUI를 만들 수있었습니다. 내 문제는 이클립스의 콘솔 (또는 명령 줄)에 인쇄되는 모든 것을 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();
}
}
내가 넷빈즈에이 솔루션을 보았다하지만 WindowBuilder을 위해 그것을 적용 할 수 있습니다 : 사전에
http://unserializableone.blogspot.com/2009/01/redirecting-systemout-and-systemerr-to.html
감사합니다.
편집 : 코드의 작업 버전은 질문에서 편집됩니다. 모든 도움에 감사드립니다.
용액은은 System.out 및 System.err에 * 리디렉션 *이 동일하고,이 넷빈즈 또는 WindowsBuilder 또는와는 전혀 무관 다른 스윙 창 구축 유틸리티 및 스윙과 관련된 모든 것. 그 해결책이 당신을 위해 작동하지 않는 경우에, 정확하게 작동하지 않는 방법 또는 왜 그것을 포함하여 저희에게 더 많은 것을 말해야합니다. –
코드를 복사하여 Eclipse에 붙여 넣으려고합니다. 그러나 내가 풀 수없는 많은 에로스를 얻는다. –
물론 당신은 그렇게 할 수 없습니다. 코드를 맹목적으로 복사하여 붙여 넣지 마십시오. 특히 이해하지 못하는 코드는 특히 그렇습니다. 대신 코드가 수행하려고하는 것을 배우고 자신의 코드를 작성하는 개념을 사용하십시오. 귀하의 질문은 귀하가 이해하지 못하는 부분에 대해 더 많은 관심을 기울여야합니다. 그렇지 않으면이 질문이 도움이되지 않습니다. –