Hi,
I am copying the code from my book. For some reason the color doesn't work when I copy+paste teh code into Eclipse. Am I doing something wrong or do I have to enable something in Eclipse options??? I keep getting 2 windows WITH NO TXT and NO COLOR. Here is the code:
import javax.swing.*;
import java.awt.*; //needed for the Color class
public class SecondWindow extends JFrame
{
public static final int WIDTH = 200;
public static final int HEIGHT = 200;
public SecondWindow( )
{
super( );
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane( );
JLabel label = new JLabel("Now available in color!");
contentPane.add(label);
setTitle("Second Window");
contentPane.setBackground(Color.BLUE);
addWindowListener(new WindowDestroyer( ));
}
public SecondWindow(Color customColor)
{
super( );
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane( );
JLabel label = new JLabel("Now available in color!");
contentPane.add(label);
setTitle("Second Window");
contentPane.setBackground(customColor);
addWindowListener(new WindowDestroyer( ));
}
}
and the driver file
import java.awt.*; //for the class Color used in an argument.
public class SecondWindowDemo
{
/**
Creates and displays two windows of the class SecondWindow.
*/
public static void main(String[] args)
{
SecondWindow window1 = new SecondWindow( );
window1.setVisible(true);
SecondWindow window2 = new SecondWindow(Color.PINK);
window2.setVisible(true);
}
}
WINDOWS Destroyer Class
import java.awt.*;
import java.awt.event.*;
/**
If you register an object of this class as a listener to any
object of the class JFrame, then if the user clicks the
close-window button in the JFrame, the object of this class
will end the program and close the JFrame.
*/
public class WindowDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
Message was edited by:
eristic
Message was edited by:
eristic