Sistemas

Sistemas

martes, 17 de mayo de 2011

JAVA 26. MEJORANDO INTERFAZ GRAFICA

COLORES DE FONDO Y DE TEXTO

La clase Component proporciona dos métodos para modificar el color de un Componente. A través de los métodos setBackground() y setForeground(), se pueden indicar los colores del fondo y del texto, respectivamente:

Clase:  import java.awt.color;

 
label1 = new JLabel("Sumar");
text1 = new JTextField("Restar"); 
text1.setBackground( Color.blue );
label1.setForeground( Color.red );
 
 
FUENTE
                               
Cambiemos el font de caracteres a Times Roman de 12 puntos
 
Clase: import java.awt.font;
 
label1.setFont( new Font( "TimesRoman",Font.PLAIN,12 ) );
label1.setFont( new Font( "TimesRoman",Font.BOLD,24 ) );
label1.setFont( new Font("Helvetica",Font.ITALIC,12) );
 

IMAGENES

Atributo: ImageIcon imagen = new ImageIcon("C:/holamu.gif");

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

import java.awt.event.*;
public class Formulario extends JFrame {
      ImageIcon imagen = new ImageIcon("C:/holamu.gif");
         
    private JLabel label1;
    public Formulario() {
        setLayout(null);
        label1=new JLabel(imagen);
        label1.setBounds(10,20,350,350);
        add(label1);
    }
     
        public static void main(String[] ar) {
        Formulario formulario1=new Formulario();
        formulario1.setBounds(0,0,400,500);
        formulario1.setVisible(true);
       
    }
}

 
VENTANAS Y COLABORACION ENTRE VENTANAS

  
importe:
import javax.swing.WindowConstants;

Ejemplo:

import javax.swing.*;
import java.awt.Window;
import java.awt.event.*;
public class Formulario1 extends JFrame implements ActionListener{
    private JButton boton1,boton2,boton3,boton4;
    private JLabel label1,label2,label3;
    public Formulario1() {
        setLayout(null);
        boton1=new JButton("1");
        boton1.setBounds(10,100,90,30);
        add(boton1);
        boton1.addActionListener(this);
        boton2=new JButton("2");
        boton2.setBounds(110,100,90,30);
        add(boton2);
        boton2.addActionListener(this);
        boton3=new JButton("3");
        boton3.setBounds(210,100,90,30);
        add(boton3);
        boton3.addActionListener(this);
        label1=new JLabel("label de la ventana 1");
        label1.setBounds(10,10,200,30);
        label2=new JLabel("label de la ventana 2");
        label2.setBounds(10,10,200,30);
        label3=new JLabel("label de la ventana 3");
        label3.setBounds(10,10,200,30);
        boton4=new JButton("4");
        boton4.setBounds(210,100,90,30);
        boton4.addActionListener(this);
    }
   
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==boton1) {
            setTitle("boton 1");
            JFrame frame1 = new JFrame("Ventana Emergente 1");
            frame1.setLayout (null);
            frame1.add(label1);
            //frame1.pack();
            frame1.setVisible(true);
            frame1.setSize(400,500);
        }
        if (e.getSource()==boton2) {
            setTitle("boton 2");
            JFrame frame2 = new JFrame("Ventana Emergente 2");
            frame2.setLayout (null);
            frame2.add(label2);
            //frame1.pack();
            frame2.setVisible(true);
            frame2.setSize(400,600);
           
        }
        if (e.getSource()==boton3) {
            setTitle("boton 3");
            JFrame frame3 = new JFrame("Ventana Emergente 2");
            frame3.setLayout (null);
            frame3.add(label3);
            //frame1.pack();
            frame3.setVisible(true);
            frame3.setSize(400,700);
            frame3.add(boton4);
       
        }       
        if (e.getSource()==boton4) {
            setTitle("boton 4");
            JFrame frame4 = new JFrame("Ventana Emergente 4");
            frame4.setLayout (null);
           
            //frame1.pack();
            frame4.setVisible(true);
            frame4.setSize(400,700);
           
       
        }       
       
    }
   
      public static void main(String[] ar){
        Formulario1 formulario2=new Formulario1();
        formulario2.setBounds(0,0,350,200);
        formulario2.setVisible(true);
    }
}


CUADROS DE DIALOGO


//título e icono por defecto

JOptionPane.showMessageDialog(null,"Texto del mensaje.");

//título personalizado, icono de aviso

JOptionPane.showMessageDialog(null,
"Texto del mensaje.",
"Título del marco",
JOptionPane.WARNING_MESSAGE);

//título personalizado, icono de error

JOptionPane.showMessageDialog(null,
"Texto del mensaje.",
"Título del marco",
JOptionPane.ERROR_MESSAGE);

//título personalizado, sin icono

JOptionPane.showMessageDialog(null,
"Texto del mensaje.",
"Título del marco",
JOptionPane.PLAIN_MESSAGE);