EJEMPLOS DE PROGRAMACIÓN EN SWING
EJEMPLO1
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.*;
public class contenedor1 extends JApplet
{
public contenedor1()
{
Container c=getContentPane();
JLabel l1=new JLabel("HOLA DESDE UNA ETIQUETA");
c.setLayout(new FlowLayout());
c.add(l1);
}
}
EJEMPLO 2
import javax.swing.*;
import java.awt.*;
public class ejemplos extends JApplet
{
JTextField t1;
JLabel l1;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
l1=new JLabel("BIENVENIDO");
t1=new JTextField("A JAVA Y SWING");
c.add(l1);
c.add(t1);
}
}
EJEMPLO 3
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ejemplos extends JApplet implements ActionListener
{
JLabel l1;
JTextField t1;
JButton b1;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
l1=new JLabel("BIENVENIDOS A UN PROGRAMA CON UN BUTON");
t1=new JTextField(30);
b1=new JButton("SALUDAR");
b1.addActionListener(this);
c.add(l1);
c.add(t1);
c.add(b1);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
t1.setText("HOLA ESTO ES JAVA");
}
}
}
EJEMPLO 4
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ejemplos extends JApplet implements ActionListener
{
JLabel l1;
JTextField t1,t2,t3;
JButton b1;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
l1=new JLabel("BIENVENIDOS A UN PROGRAMA CON UN BUTON");
t1=new JTextField(30);
t2=new JTextField(30);
t3=new JTextField(30);
b1=new JButton("SUMAR");
b1.addActionListener(this);
c.add(l1);
c.add(t1);
c.add(t2);
c.add(t3);
c.add(b1);
}
public void actionPerformed(ActionEvent e)
{
double a,b,s;
if(e.getSource()==b1)
{
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());
s=a+b;
t3.setText(String.valueOf(s));
}
}
}
//CONECTAR A BASE DE DATOS
import javax.swing.*;
import java.sql.*;
public class conectar
{
public static void main(String args[]) throws InstantiationException, IllegalAccessException
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
//DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
Connection conectar=DriverManager.getConnection("jdbc:mysql://localhost/clientes","root","rorro");
JOptionPane.showMessageDialog(null,"ESTAS CONECTADO");
conectar.close();
}
catch(ClassNotFoundException e)
{
System.err.print(e);
}
catch(SQLException sq)
{
System.err.print(sq);
}
}
}
//CALCULADORA BASICA
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class calculadora extends JApplet implements ActionListener
{
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton b1,b2,b3,b4;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
l1=new JLabel("VALOR UNO: ");
t1=new JTextField(30);
l2=new JLabel("VALOR DOS: ");
t2=new JTextField(30);
l3=new JLabel("RESULTADO: ");
t3=new JTextField(30);
b1=new JButton("SUMAR");
b2=new JButton("RESTAR");
b3=new JButton("MULTIPLICAR");
b4=new JButton("DIVIDIR");
c.add(l1);
c.add(t1);
c.add(l2);
c.add(t2);
c.add(l3);
c.add(t3);
c.add(b1);
c.add(b2);
c.add(b3);
c.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
double a,b,r;
if(e.getSource()==b1)
{
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());
r=a+b;
t3.setText(" "+r);
}
if(e.getSource()==b2)
{
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());
r=a-b;
t3.setText(" "+r);
}
if(e.getSource()==b3)
{
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());
r=a*b;
t3.setText(" "+r);
}
if(e.getSource()==b4)
{
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());
r=a/b;
t3.setText(" "+r);
}
}
}