The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
June 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Print

Posted by Chris on June 01, 2001 at 3:32 AM

Hi
Please look at this code everything is ok till I put in the printJob code in to print the contents of the Textarea I have highlighted the area code please can you see what is wrong. I don�t know what to do.


public class TroisTest
{

public static void main (String args[])
{
Trois t = new Trois();
t.setBounds(90,30,600,500);

t.setTitle("Trois Vallees Ski Resort");
t.setVisible(true);

}

}


import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
/*-------------------------------------------------------*/

class Trois extends Frame
implements ActionListener
{

// Declarations //

Frame0 window;
Frame2 window2;
Frame3 window3;

private Button G;
private Button E;
private Button K;
private Label Heading;
private Label name;
private Button next;

Menu menu;
MenuBar menubar;
MenuItem menuitem1,menuitem2,menuitem3;




public Trois()

{

// Layout manager is set to null //

setLayout(null);

setBackground(Color.green);

Font p = new Font ("Ariel",Font.ITALIC, 24);
Font d = new Font ("Ariel",Font.ITALIC, 18);

// constructor for a Button with a title G //

Button G = new Button("German");
add(G);
G.setBounds(70,170,90,20);
G.addActionListener(this);

Button E = new Button("English");
add(E);
E.setBounds(70,210,90,20);
E.addActionListener(this);


Button K = new Button("French");
add(K);
K.setBounds(70,250,90,20);
K.addActionListener(this);

// Language Label //
Label name = new Label("Language");
name.setFont(d);
name.setForeground(Color.black);
add(name);
name.setBounds(75,130,90,20);


// Program Label //
Label heading = new Label("Trois Vallees Ski Resort");
heading.setFont(p);
heading.setForeground(Color.black);
add(heading);
heading.setBounds(180,50,300,20);



menubar = new MenuBar();
menu = new Menu("File");


menuitem1 = new MenuItem ("Exit");
menu.add(menuitem1);
menuitem1.addActionListener(this);

//, Add and set the MenuBar >//
menubar.add(menu);
setMenuBar(menubar);

// //
addWindowListener(new MyWindowEventHandler());

// Add the 3 windows and Name them //
window = new Frame0("Trois Vallees Ski Resort");
window.setBounds(90,30,660,520);

window2 = new Frame2("Trois Vallees Ski Resort");
window2.setBounds(90,30,660,520);

window3 = new Frame3("Trois Vallees Ski Resort");;
window3.setBounds(90,30,660,520);

/*------------------------------------------------------------------------------------------------------------------*/
}

public void actionPerformed (ActionEvent e)
{

if (e.getActionCommand().equals("Close"))
{
System.exit(0);

}

if(e.getActionCommand().equals("German"))

{
window.setVisible(true);
}

if(e.getActionCommand().equals("English"))

{
window2.setVisible(true);
}

if(e.getActionCommand().equals("French"))

{
window3.setVisible(true);
}

if (e.getSource() == menuitem1)

{
System.exit(0);

}
/*------------------------------------------------------------------------------------------------------------------*/
}
}

class MyWindowEventHandler extends WindowAdapter {

public void windowClosing(WindowEvent e) {

System.exit(0); //halt program

}

}

//----------------------------------------------------------------------------------------//
// German Class //


class Frame0 extends Frame

implements ActionListener
{
Font oj = new Font ("Ariel",Font.ITALIC, 18);
Font cm = new Font ("Ariel",Font.ITALIC, 24);
Label label;
TextArea na;

Frame0(String title)

{

super(title);


setBackground(Color.green);

setLayout(null);

Button exit = new Button("exit");
add(exit);
exit.setBounds(510,470,90,20);
exit.addActionListener(this);

Label name = new Label("German");
name.setFont(cm);
name.setForeground(Color.black);
add(name);
name.setBounds(275,80,190,20);

TextArea na = new TextArea( " Trois Valley Ski Resort \n"+" To decrement counter press : DEC \n"+" To Increment counter press : INC \n"+" To set counter to zero press : ZERO \n"+" Enter a number in text field to set the counter then press : SET");
na.setBackground(Color.white);
na.setFont(oj);
na .setEditable(false);
add(na);
na.setBounds(75,160,490,250);


Button print = new Button("Print");
add(print);
print.setBounds(70,470,90,20);
print.addActionListener(this);

}

public void actionPerformed (ActionEvent e)
{

if(e.getActionCommand().equals("exit"))
{
setVisible(false);
}
//=============== Error Bellow program ok till i try to put in print job =========================//


if(e.getActionCommand().equals("Print"))

{
PrinterJob.printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(yhis);
if(printJob.printDialog());

{

try{

printJob.print();
}catch(Exception ex){

ex.printStackTrace();

}
}
//======================================================//
//----------------------------------------------------------------------------------------//

// English Class //

class Frame2 extends Frame

implements ActionListener
{

Font cjm = new Font ("Ariel",Font.ITALIC, 24);
Label label;
Frame2(String title)
{
super(title);


setBackground(Color.green);

setLayout(null);

Button exit = new Button("exit");
add(exit);
exit.setBounds(510,470,90,20);
exit.addActionListener(this);

Label name = new Label("English");
name.setFont(cjm);
name.setForeground(Color.black);
add(name);
name.setBounds(75,130,190,20);



}

public void actionPerformed (ActionEvent e)
{

if(e.getActionCommand().equals("exit"))
{
setVisible(false);

}
}

}

//--------------------------------------------------------------------------------------//
// French Class //

class Frame3 extends Frame

implements ActionListener
{

Font ak = new Font ("Ariel",Font.ITALIC, 24);

Label label;

Frame3(String title)

{

super(title);

setBackground(Color.green);

setLayout(null);

Button exit = new Button("exit");
add(exit);
exit.setBounds(510,470,90,20);
exit.addActionListener(this);

Label name = new Label("French");
name.setFont(ak);
name.setForeground(Color.black);
add(name);
name.setBounds(75,130,190,20);

}

public void actionPerformed (ActionEvent e)
{

if(e.getActionCommand().equals("exit"))

{
setVisible(false);
}
}

}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us