IT_Programming/Java

[print test] 스트링을 모니터 화면이 아닌 PC의 프린터로 출력하는 방법

JJun ™ 2008. 8. 20. 09:25

 

 

 

 

 

 

import javax.swing.JFrame;

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.JScrollPane;

import javax.swing.JTextPane;

 


public class PrintTest extends JFrame {

private JPanel jContentPane = null;
private JButton jButton = null;
private JScrollPane jScrollPane = null;
private JTextPane jTextPane = null;

public PrintTest() {
super();
initialize();
}

private void initialize() {
this.setSize(new Dimension(387, 282));
this.setContentPane(getJContentPane());
this.setTitle("PrintTest");
}

private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}

private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(131, 15, 113, 30));
jButton.setText("PrintTest");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
myPrinter();

}
});
}
return jButton;
}

protected void myPrinter() {
// TODO Auto-generated method stub

try {

jTextPane.print(); // 여기를 연구해보세요.print()로 프린터 합니다.
} catch( Exception e) {
e.printStackTrace();
}
}

private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(43, 74, 300, 155));
jScrollPane.setViewportView(getJTextPane());
}
return jScrollPane;
}

private JTextPane getJTextPane() {
if (jTextPane == null) {
jTextPane = new JTextPane();
}
return jTextPane;
}

public static void main(String[] args) {
PrintTest ss = new PrintTest();
ss.setVisible(true);

}
}