Establecer los componentes para la apariencia

el cuadro de texto que aparece debajo de la etiqueta tiene la apariencia de una
etiqueta. Esto le indica la potencia de la gestión de apariencias en Swing.

package masterdavidjavabasico;

import java.awt.*;
import javax.swing.*;

/*
<APPLET
    CODE=plafcomponente.class
    WIDTH=210
    HEIGHT=200 >
</APPLET>
*/

public class plafcomponente extends JApplet
{
    public void init()
    {
        Container contentPane = getContentPane();

        JNewLabel jnewlabel = new JNewLabel(
                "Esta es una etiqueta falsa.");

        contentPane.setLayout(new FlowLayout());
                                   
        contentPane.add(new JLabel("Esta es una etiqueta real."));
        contentPane.add(jnewlabel);
    }
}

class JNewLabel extends JTextField
{
    public JNewLabel(String s)
    {
        super(s);
    }
    public void updateUI()
    {
        super.updateUI();

        setHighlighter(null);
        setEditable(false);

        LookAndFeel.installBorder(this, "Label.border");

        LookAndFeel.installColorsAndFont(this, "Label.background",
            "Label.foreground", "Label.font");
    }
}

Cambio de apariencia

muestra la applet con la apariencia Metal, Motify  y la de Windows. Cambiar entre estas apariencias es tan
fácil como hacer clic sobre un botón de opción.

package masterdavidjavabasico;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


/*
<APPLET
    CODE=plaf.class
    WIDTH=210
    HEIGHT=200 >
</APPLET>
*/

public class plaf extends JApplet
{

    JRadioButton b1 = new JRadioButton("Metal"),
    b2 = new JRadioButton("Motif"),
    b3 = new JRadioButton("Windows");

    public void init()
    {
        Container contentPane = getContentPane();
        contentPane.add(new jpanel(), BorderLayout.CENTER);
    }

    class jpanel extends JPanel implements ActionListener
    {
        public jpanel()
        {
            add(new JButton("JBoton"));
            add(new JTextField("JCuadro_de_texto"));
            add(new JCheckBox("JCasilla_de_activacion"));
            add(new JRadioButton("JBoton_de_opcion"));
            add(new JLabel("JEtiqueta"));
            add(new JList(new String[] {
                "JLista Elemento 1", "JLista Elemento 2","JLista Elemento 3"}));
            add(new JScrollBar(SwingConstants.HORIZONTAL));

            ButtonGroup group = new ButtonGroup();
            group.add(b1);
            group.add(b2);
            group.add(b3);

            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);

            add(b1);
            add(b2);
            add(b3);
        }

        public void actionPerformed(ActionEvent e)
        {
            JRadioButton src = (JRadioButton)e.getSource();

            try {
                if((JRadioButton)e.getSource() == b1)
                    UIManager.setLookAndFeel(
                      "javax.swing.plaf.metal.MetalLookAndFeel");
                else if((JRadioButton)e.getSource() == b2)
                    UIManager.setLookAndFeel(
                        "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                else if((JRadioButton)e.getSource() == b3)
                    UIManager.setLookAndFeel(
                        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
            catch(Exception ex) {}

            SwingUtilities.updateComponentTreeUI(getContentPane());
        }
    }
}

Holo desde java Swing JFrame

Ahora, además, hemos usado el método setBounds para fijar la posición y
tamaño de la ventana JFrame.
package masterdavidjavabasico;

import javax.swing.*;
import java.awt.*;


public class app extends JFrame
{
    jpanel j;

    public app()
    {
        super("Aplicacion Swing");

        Container contentPane = getContentPane();
        j = new jpanel();
        contentPane.add(j);
    }

    public static void main(String args[])
    {
        final JFrame f = new app();

        f.setBounds(100, 100, 300, 300);
        f.setVisible(true);
        f.setBackground(Color.white);
        //f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        //f.addWindowListener(new WindowAdapter() {
        //    public void windowClosed(WindowEvent e) {
        //        System.exit(0);
        //    }
        //});

    }
}

class jpanel extends JPanel
{
    jpanel()
    {
        setBackground(Color.white);
    }

    public void paintComponent (Graphics g)
    {
        super.paintComponent(g);
        g.drawString("Hola desde Swing!", 0, 60);
    }
}

Applet que abre una ventana con menus

package masterdavidjavabasico;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

/*
<APPLET
    CODE=menu.class
    WIDTH=200
    HEIGHT=200 >
</APPLET>
*/

public class menu extends Applet implements ActionListener
{
    Button b1;
    frame menuWindow;

    public void init()
    {
        b1 = new Button("Visualizar el menu de la ventana");
        add(b1);
        b1.addActionListener(this);

        menuWindow = new frame("Menus");
        menuWindow.setSize(200, 200);
    }

    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == b1){
            menuWindow.setVisible(true);
        }
    }
}

class frame extends Frame implements ActionListener
{
    Menu menu;
    MenuBar menubar;
    MenuItem menuitem1, menuitem2, menuitem3;

    Label label;

    frame(String title)
    {
        super(title);
        label = new Label("Hola desde Java!");
        setLayout(new GridLayout(1, 1));
        add(label);
        menubar = new MenuBar();

        menu = new Menu("Archivo");

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

        menuitem2 = new MenuItem("Elemento 2");
        menu.add(menuitem2);
        menuitem2.addActionListener(this);

        menuitem3 = new MenuItem("Elemento 3");
        menu.add(menuitem3);
        menuitem3.addActionListener(this);

        menubar.add(menu);

        setMenuBar(menubar);

         addWindowListener(new WindowAdapter() {public void
              windowClosing(WindowEvent e) {setVisible(false);}});
    }

    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == menuitem1){
            label.setText("Eligio el elemento 1");
        } else if(event.getSource() == menuitem2){
            label.setText("Eligio el elemento 2");
        } else if(event.getSource() == menuitem3){
            label.setText("Eligio el elemento 3");
        }
    }
}

Abrir una ventana desde otra ventana

Como se puede ver, la ventana
aparece con un borde mínimo y la etiqueta de control que visualiza un mensaje.

package masterdavidjavabasico;

import java.awt.*;
import java.awt.event.*;


public class ventana 
{
    public static void main(String [] args)
    {
       AppFrame f = new AppFrame();

       f.setSize(200, 200);

       f.addWindowListener(new WindowAdapter() { public void
           windowClosing(WindowEvent e) {System.exit(0);}});

       f.show();
    }
}

class AppFrame extends Frame implements ActionListener
{
    Button b1, b2;
    labelWindow window;

    AppFrame()
    {
        setLayout(new FlowLayout());
        b1 = new Button("Visualizar la ventana");
        add(b1);
        b1.addActionListener(this);

        b2 = new Button("Ocultar la ventana");
        add(b2);
        b2.addActionListener(this);

        window = new labelWindow(this);
        window.setSize(300, 200);
        window.setLocation(300, 200);
    }

    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == b1){
            window.setVisible(true);
        }
        if(event.getSource() == b2){
            window.setVisible(false);
        }
    }
}

class labelWindow extends Window {
    Label label;

    labelWindow(AppFrame af){
        super(af);
        setLayout(new FlowLayout());
        label = new Label("Hola desde Java!");
        add(label);
    }

    public void paint (Graphics g)
    {
        int width = getSize().width;
        int height = getSize().height;

        g.drawRect(0, 0, --width, --height);
    }
}

Gestionar eventos de ventana

se puede ver, la ventana
frame gestiona eventos MouseListener, visualizando mensajes.

package masterdavidjavabasico;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

/*
<APPLET
    CODE=frame.class
    WIDTH=300
    HEIGHT=200 >
</APPLET>
*/

public class frame extends Applet implements ActionListener {

    Button b1, b2;
    labelFrame window;

    public void init()
    {
        b1 = new Button("Visualizar la ventana");
        add(b1);
        b1.addActionListener(this);

        b2 = new Button("Ocultar la ventana");
        add(b2);
        b2.addActionListener(this);

        window = new labelFrame("Ventana Java");
        window.setSize(350, 250);
    }

    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == b1){
            window.setVisible(true);
        }
        if(event.getSource() == b2){
            window.setVisible(false);
        }
    }
}

class labelFrame extends Frame implements MouseListener {
    Label label;

    labelFrame(String title){
        super(title);
        setLayout(new FlowLayout());
        label = new Label("Hola desde Java! Esta es una ventana frame.");
        add(label);
        addMouseListener(this);
         addWindowListener(new WindowAdapter() {public void
             windowClosing(WindowEvent e) {setVisible(false);}});
    }

    public void mousePressed(MouseEvent e)
    {
        if((e.getModifiers() & InputEvent.BUTTON1_MASK) ==
            InputEvent.BUTTON1_MASK){
            label.setText("Boton izquierdo del raton pulsado en " + e.getX() + "," + e.getY());
        }
        else{
            label.setText("Boton derecho del raton pulsado en " + e.getX() + "," + e.getY());
        }
    }

    public void mouseClicked(MouseEvent e)
    {
        label.setText("Pulso el raton en " + e.getX() + "," + e.getY());
    }

    public void mouseReleased(MouseEvent e)
    {
        label.setText("Se solto el boton del raton.");
    }

    public void mouseEntered(MouseEvent e)
    {
        label.setText("Raton para introducir.");
    }

    public void mouseExited(MouseEvent e)
    {
        label.setText("Raton para salir.");
    }
}