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");
}
}
Mostrando las entradas con la etiqueta JTextFiel. Mostrar todas las entradas
Mostrando las entradas con la etiqueta JTextFiel. Mostrar todas las entradas
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());
}
}
}
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());
}
}
}
Suscribirse a:
Entradas (Atom)