1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package minooo
import javax.swing.*;
import java.awt.*;
import java.awt.print.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleFenetre extends JFrame implements ActionListener
{
private JPanel pan;
private JLabel b = new JLabel("votre nom : ");
private JButton imprime = new JButton ("imprimer");
private JTextField nom=new JTextField ();
public SimpleFenetre (){
this.setResizable(false);
this.setSize(720, 520);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan=new Panneau();
pan.setLayout(null);
nom.setBounds(340 ,100 ,100,30 );
pan.add(nom);
getContentPane().add(pan);
pan.add(b);
b.setBounds(150 ,100 ,80,24 );
pan.add(imprime);
imprime.addActionListener(this);
imprime.setBackground(Color.red);
imprime.setBounds(220 ,100 ,100,30 );
getContentPane().add(pan);
}
public static void main(String[] args) {
SimpleFenetre gui = new SimpleFenetre();
gui.setVisible(true);
}
class Panneau extends JPanel {
private ImageIcon rouge ;
public Panneau(){
rouge =new ImageIcon("image.jpg");
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Image imRouge=rouge.getImage();
g.drawImage(imRouge,0,0,this);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(imprime)) {
String text=nom.getText();
nom.Imprimer();
}
}
public void Imprimer() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable();
if (printJob.printDialog()) {
try { printJob.print(); }
catch(Exception PrinterExeception) { }
}
}
public int print(Graphics g, PageFormat pf, int pi) throws
PrinterException {
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
g.translate(100, 100);
Font f = new Font("Monospaced",Font.PLAIN,12);
g.setFont (f);
nom.paint (g);
return Printable.PAGE_EXISTS;
}
} |
Partager