الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

طباعة صفحة

بدأه mohammeding في 14 سبتمبر 2008 · 4 رد · 1,131 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

أرجو أجابة على سؤالي

أريد أن أعرف كيف أقوم بطباعة صفحة

JTextArea

في الطابعة

#2

السلام عليكم ...

اليك الرابط : Printing JTextArea component

فيه ما تريد إن شاء الله ... :)

لا اله الا الله .. محمد رسول الله

(* ربِ اجعلني مقيم الصلاة و من ذريتي ، ربنا و تقبل دعـاء *)

يا حيّ يا قيـوم ... برحمتك استغيث ... اصلح لي شأني كله و لا تكلني الى نفسي طرفة عين

#3

هذا السورس الكامل للي انت طلبته موجود بالموقع

/*
 * PrintingTextOnJTextArea.java
 *
 * Created on 17 Juli 2008, 14:07
 */
package org.kazao.tips.tip011;

import java.awt.print.PrinterException;

/**
 *
 * @author  Mr. Kazao
 */
public class PrintingTextOnJTextArea extends javax.swing.JFrame {

	/** Creates new form PrintingTextOnJTextArea */
	public PrintingTextOnJTextArea() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// //GEN-BEGIN:initComponents
	private void initComponents() {

		scroller = new javax.swing.JScrollPane();
		text = new javax.swing.JTextArea();
		buttonPrint = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Printing JTextArea componen");

		scroller.setName("scroller"); // NOI18N

		text.setFont(new java.awt.Font("Courier New", 0, 13)); // NOI18N
		text.setText("Item				   Qty	  Price	Sub Total\n==================================================\nShampo Sunsilk 100ml	10  10.500,00   105.000,00\nHead&Shoulder 150ml	  5  12.750,00	63.750,00\nRinso Warna 2kg		  1  18.200,00	18.200,00\nCoca-cola Zero 1500	 10   6.700,00	67.000,00\n==================================================\nTotal:								  253.950,00");
		text.setName("text"); // NOI18N
		scroller.setViewportView(text);

		buttonPrint.setText("Print");
		buttonPrint.setName("buttonPrint"); // NOI18N
		buttonPrint.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonPrintActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addComponent(scroller, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
					.addComponent(buttonPrint))
				.addContainerGap())
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(scroller, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(buttonPrint)
				.addContainerGap())
		);

		pack();
	}// //GEN-END:initComponents

private void buttonPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPrintActionPerformed
	try {
		text.print();//GEN-LAST:event_buttonPrintActionPerformed
	} catch (PrinterException ex) {
	}
}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new  

			  Runnable() {

				 public void run() {
				new PrintingTextOnJTextArea().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	public javax.swing.JButton buttonPrint;
	public javax.swing.JScrollPane scroller;
	public javax.swing.JTextArea text;
	// End of variables declaration//GEN-END:variables
}

فاامان الله

والخاملون اذا غدوت تلومهم سمعوك في اذانهم تترنم .. هذا نحن العرب للاسف

[spacing]

HaKxEr HaCker

My Site

[/spacing]

#4

السلام عليكم

شكر على الرد

و اجهتني مشكلة عند تطبيق هذا السورس

بحيث يطلعلي عذا المساج

cannot find symbol class GroupeLayout

ارجو مساعدة

للعلم أنا أستخدم

JCreator LE

تم تعديل هذه المشاركة بواسطة mohammeding في 15 سبتمبر 2008 في 16:16

#5

:lol: السلام عليكم

أسمحولي

لقد وجدت الحل

كلاس أساسي كما هو أن

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.awt.print.*;

import java.io.*;

import java.util.*;

public class PrintTest extends JFrame implements ActionListener

{

JLabel label;

JTextPane jtextpane;

JButton browse,print;

JPanel p;

PrintPane printpane;

public PrintTest()

{

printpane = new PrintPane();

//jtextpane = new JTextPane();

print = new JButton("Print");

print.addActionListener(this);

browse = new JButton("Browse");

browse.addActionListener(this);

p = new JPanel();

p.add(print);

p.add(browse);

JScrollPane js = new JScrollPane(printpane);

getContentPane().add(js,BorderLayout.CENTER);

getContentPane().add(p,BorderLayout.NORTH);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource() == print)

{

//String text = jtextpane.getText();

PrinterJob printJob = PrinterJob.getPrinterJob();

printJob.setPrintable(printpane);

if(printJob.printDialog())

{

try

{

printJob.print();

}

catch (Exception pe)

{

System.out.println(pe);

}

}

}

if(ae.getSource() == browse)

{

JFileChooser filechooser = new JFileChooser();

filechooser.showOpenDialog(this);

File file = filechooser.getSelectedFile();

readFile(file);

}

}

private void readFile(File file)

{

FileInputStream in=null;

if(file!=null)

{

try

{

in = new FileInputStream(file);

byte[] buf=new byte[in.available()];

int n=0;

while(n!=-1)

{

n=in.read(buf);

if(n>0)

printpane.setText(new String(buf,0,n));

}

in.close();

}

catch(IOException e)

{

JOptionPane.showMessageDialog(this,"IO ERROR",e.getMessage(),JOptionPane.ERROR_MESSAGE);

}

// make sure the file is closed.

finally

{

try

{

in.close();

}

catch(IOException e)

{

System.out.println(e);

}

}

}

}

public static void main(String args[])

{

PrintTest frame = new PrintTest();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.setSize(650,500);

frame.setVisible(true);

}

}

كلاس

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.awt.print.*;

import java.io.*;

import java.util.*;

public class PrintPane extends JTextPane implements Printable,Serializable

{

JLabel label;

JTextPane jtextpane;

JButton browse,print;

JPanel p;

public PrintPane(){}

public int print(Graphics g,PageFormat pf,int pi) throws PrinterException

{

Graphics2D g2 = (Graphics2D)g;

g2.setColor(Color.black);

Dimension d = this.getSize(); //get size of document

double panelWidth = d.width; //width in pixels

double panelHeight = d.height; //height in pixels

double pageHeight = pf.getImageableHeight(); //height of printer page

double pageWidth = pf.getImageableWidth(); //width of printer page

double scale = pageWidth/panelWidth;

int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);

//make sure not print empty pages

if(pi >= totalNumPages)

{ return Printable.NO_SUCH_PAGE;

}

//shift Graphic to line up with beginning of print-imageable region

g2.translate(pf.getImageableX(), pf.getImageableY());

//shift Graphic to line up with beginning of next page to print

g2.translate(0f, - pi * pageHeight);

//scale the page so the width fits...

g2.scale(scale, scale);

this.paint(g2); //repaint the page for printing

return Printable.PAGE_EXISTS;

/*if (pi>=0)

{

return Printable.NO_SUCH_PAGE;

}

g.translate(100,100);

paint(g);

return Printable.PAGE_EXISTS;*/

}

}

المرفقات فيها البرنامج مثال لبرنامج الطباعة

PrintJTextPane.zip

مواضيع مشابهة