INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

23
1 PROGRAMA INTERFAZ GRAFICA IMPLEMENTADO HILOS Crear una interfaz gráfica con un menú que contenga botones que permita calcular las siguientes operaciones: 1. Generar la serie de números primos. 2. Generar la serie de núme ros Palíndromos. Usar e implementar todos los botones y validaciones que necesite. Use el botón “Limpiar”, “Resultado”, “Regresar al Menú” y “Salir de la aplicación”  Para las dos opciones ingresar el inicio y el fin de la serie y mostrar el resultado, usando el concepto de hilos o multitarea estableciendo un tiempo de ejecución. Código Ventana Principal import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Sy stemColor; import java.awt.To olkit; import javax.swing .JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing .JButton; import java.awt.ev ent.ActionLi stener; import java.awt.event.ActionEvent; import javax.swing .ImageIcon; import javax.swing .JMenuBar; import javax.swing.JMenu; import javax.swing .JMenuItem; import javax.swing.JRadioButtonMenuItem; import javax.swing.JCheckBoxMenuItem; import javax.swing .JPopupMenu; import java.awt.Co mponent; import java.awt.event.MouseAdapter; import java.awt.ev ent.MouseEvent; import javax.swing .ButtonGroup ; import java.awt.*; import javax.swing .JSeparator; import javax.swing .JLabel; public class VentanaPrincipal extends JFrame { private JPanel contentPane ; private final ButtonGroup buttonGroup = new ButtonGroup(); public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { VentanaPrincipal frame = new VentanaPrincipal(); frame.setVisible(true); } catch (Exception e) {

Transcript of INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

Page 1: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 1/23

1

PROGRAMA INTERFAZ GRAFICA IMPLEMENTADO

HILOS

Crear una interfaz gráfica con un menú que contenga botones que permita calcular lassiguientes operaciones:

1. 

Generar la serie de números primos.2.  Generar la serie de números Palíndromos.

Usar e implementar todos los botones y validaciones que necesite.

Use el botón “Limpiar”, “Resultado”, “Regresar al Menú” y “Salir de la aplicación” 

Para las dos opciones ingresar el inicio y el fin de la serie y mostrar el resultado, usando el

concepto de hilos o multitarea estableciendo un tiempo de ejecución.

Código Ventana Principal

import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.SystemColor;import java.awt.Toolkit;

import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.ImageIcon;import javax.swing.JMenuBar;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JRadioButtonMenuItem;import javax.swing.JCheckBoxMenuItem;import javax.swing.JPopupMenu;import java.awt.Component;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.ButtonGroup;import java.awt.*;import javax.swing.JSeparator;import javax.swing.JLabel;

public class VentanaPrincipal extends JFrame {private JPanel contentPane;private final ButtonGroup buttonGroup = new ButtonGroup();

public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {

public void run() {try {

VentanaPrincipal frame = new VentanaPrincipal();frame.setVisible(true);

} catch (Exception e) {

Page 2: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 2/23

2

e.printStackTrace();}

}});

}

public VentanaPrincipal() {

setIconImage(Toolkit.getDefaultToolkit().getImage(VentanaPrincipal.class.getResource("/javax/swing/plaf/basic/icons/JavaCup16.png")));

setTitle("MENU OPCIONES");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 378, 152);

JMenuBar menuBar = new JMenuBar();setJMenuBar(menuBar);

JMenu mnOperaciones = new JMenu("OPCIONES");menuBar.add(mnOperaciones);

JMenuItem mntmSuma = new JMenuItem("PRIMOS");mntmSuma.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {VentanaPrimos suma=new VentanaPrimos();suma.setVisible(true);dispose();

}});mnOperaciones.add(mntmSuma);

JMenuItem mntmResta = new JMenuItem("PALINDROMOS");mntmResta.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

VentanaPalindromos resta=new VentanaPalindromos();resta.setVisible(true);dispose();

}});mnOperaciones.add(mntmResta);

JSeparator separator = new JSeparator();mnOperaciones.add(separator);

JMenuItem mntmSalir_1 = new JMenuItem("SALIR");mntmSalir_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

System.exit(0);}

});mnOperaciones.add(mntmSalir_1);contentPane = new JPanel();contentPane.setBackground(new Color(255, 255, 255));contentPane.setForeground(new Color(192, 192, 192));contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);

Page 3: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 3/23

3

JPopupMenu popupMenu = new JPopupMenu();popupMenu.setBounds(210, 11, 49, 38);addPopup(contentPane, popupMenu);

JMenuItem mntmInformacion = new JMenuItem("INFORMACION ?");mntmInformacion.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JFrameConFondo info=new JFrameConFondo();info.setVisible(true);dispose();

}});popupMenu.add(mntmInformacion);

JLabel label = new JLabel("REALIZADO POR:");label.setForeground(Color.RED);label.setFont(new Font("Calisto MT", Font.BOLD, 12));label.setBounds(77, 11, 136, 27);contentPane.add(label);

JLabel label_1 = new JLabel("FREDDY ALVAREZ");label_1.setForeground(Color.BLUE);label_1.setFont(new Font("Californian FB", Font.ITALIC, 15));label_1.setBounds(144, 40, 196, 27);contentPane.add(label_1);

}private static void addPopup(Component component, final JPopupMenu popup)

{component.addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {if (e.isPopupTrigger()) {

showMenu(e);

}}public void mouseReleased(MouseEvent e) {

if (e.isPopupTrigger()) {showMenu(e);

}}private void showMenu(MouseEvent e) {

popup.show(e.getComponent(), e.getX(), e.getY());}

});}

}

Código Ventana Primos

import java.awt.BorderLayout;import java.awt.Event;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JLabel;import javax.swing.JOptionPane;

Page 4: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 4/23

4

import javax.swing.JTextField;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.ImageIcon;import java.awt.*;import javax.swing.*;import java.util.*;

public class VentanaPrimos extends JFrame{

private JPanel contentPane;private JTextField num1;private JTextField num2;private JLabel lblRESULTADO;private String numero1;private String numero2;private int numeroint1;private int numeroint2;

class Hilo1 implements Runnable{

private int inicio,fin;private String rs="";private String rserie="";private JLabel lbl;

public Hilo1(int num1,int num2,JLabel jlbl){

inicio=num1;fin=num2;lbl=jlbl;

}

public void run(){

int contador=0;int i=0;int verificar=0;int primo,mod,incremento,cuantos;String serie="";//Para saber cuantos números primos existen

cuantos=0;

if(inicio>0 && fin>0){

if(inicio<fin){

//Controlamos si ingreso como valor inicial el 1if (inicio==1){

Page 5: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 5/23

5

inicio=2;}//Bucle de análisisfor(incremento=inicio; incremento<=fin;

incremento++){

//Inicializamos las variables en cadabucle

primo = 1;

mod = 2;//Analizamos mientras mod sea menor o

igual a incremento dividido 2 y la variable primo == a uno.while(mod<=incremento/2 && primo==1){

if(incremento % mod == 0){

primo = 0;}else{

mod++;}

}//Si llegamos hasta aquí con primo igual

a uno muestra el númeroif(primo == 1){

try{

Thread.currentThread();Thread.sleep((long)(450));

}catch(InterruptedException e){

System.out.println(e.getMessage());

}

serie=serie+" "+incremento;lbl.setText(""+serie);//Aumentamos el contador finalcuantos++;

}

}}

}else

if(inicio<0 && fin<0)

{

if(Math.abs(inicio)>Math.abs(fin)){

//Controlamos si ingreso como valorinicial el 1

if (Math.abs(fin)==1){

fin=2;}

Page 6: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 6/23

6

//Bucle de análisisfor(incremento=Math.abs(fin);

incremento<=Math.abs(inicio); incremento++){

//Inicializamos las variables encada bucle

primo = 1;mod = 2;//Analizamos mientras mod sea menor

o igual a incremento dividido 2 y la variable primo == a uno.while(mod<=incremento/2 &&

primo==1){

if(incremento % mod == 0){

primo = 0;}else{

mod++;}

}

//Si llegamos hasta aquí con primoigual a uno muestra el número

if(primo == 1){

try{

Thread.currentThread();

Thread.sleep((long)(450));}catch(InterruptedException

e){

System.out.println(e.getMessage());}

serie=serie+" -"+incremento;lbl.setText(serie);//Aumentamos el contador

finalcuantos++;

}

}}

}else

if(inicio<0 && fin>0){

//Bucle de análisis NEGATIVOSfor(incremento=Math.abs(inicio);

incremento>=2; incremento--){

Page 7: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 7/23

7

//Inicializamos las variables encada bucle

primo = 1;mod = 2;//Analizamos mientras mod sea menor

o igual a incremento dividido 2 y la variable primo == a uno.while(mod<=incremento/2 &&

primo==1){

if(incremento % mod == 0){

primo = 0;}else{

mod++;}

}//Si llegamos hasta aquí con primo

igual a uno muestra el númeroif(primo == 1){

try{

Thread.currentThread();

Thread.sleep((long)(450));}catch(InterruptedException

e){

System.out.println(e.getMessage());}

StringBuilder builder=newStringBuilder(serie);

StringsCadenaInvertida=builder.reverse().toString();

lbl.setText(sCadenaInvertida);serie=serie+"-"+incremento;//Aumentamos el contador

finalcuantos++;

}}

//Bucle de análisis POSITIVOSfor(incremento=2; incremento<=fin;

incremento++){

//Inicializamos las variables encada bucle

primo = 1;mod = 2;

Page 8: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 8/23

8

//Analizamos mientras mod sea menoro igual a incremento dividido 2 y la variable primo == a uno.

while(mod<=incremento/2 &&primo==1)

{if(incremento % mod == 0){

primo = 0;}

else{

mod++;}

}//Si llegamos hasta aquí con primo

igual a uno muestra el númeroif(primo == 1){

try{

Thread.currentThread();

Thread.sleep((long)(450));}catch(InterruptedException

e){

System.out.println(e.getMessage());}

serie=serie+" "+incremento;lbl.setText(""+serie);//Aumentamos el contador

final

cuantos++;}

}

}}

}

public static void main(String[] args){

EventQueue.invokeLater(new Runnable(){

public void run(){

try{

VentanaPrimos frame = new VentanaPrimos();frame.setVisible(true);

} catch (Exception e) {e.printStackTrace();

}}

Page 9: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 9/23

9

});}

private static boolean isNumber(String n){

try{

Integer.parseInt(n);

return true;} catch (NumberFormatException nfe) {

return false;}

}

public VentanaPrimos(){

setTitle("VENTANA PRIMOS ENTEROS");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 494, 250);contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);

JLabel lbl1 = new JLabel("INGRESE EL INICIO DE LA SERIE:");lbl1.setBounds(20, 11, 199, 34);contentPane.add(lbl1);

JLabel lbl2 = new JLabel("INGRESE EL FIN DE LA SERIE:");lbl2.setBounds(20, 62, 177, 34);contentPane.add(lbl2);

num1 = new JTextField();

num1.setBounds(229, 18, 86, 20);contentPane.add(num1);num1.setColumns(10);

num2 = new JTextField();num2.setBounds(229, 69, 86, 20);contentPane.add(num2);num2.setColumns(10);

JLabel lbl3 = new JLabel("RESULTADO:");lbl3.setBounds(26, 120, 149, 34);contentPane.add(lbl3);

JButton btnMenu = new JButton("MENU");btnMenu.setBounds(350, 181, 104, 23);btnMenu.setIcon(new

ImageIcon(VentanaPrimos.class.getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));

btnMenu.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {

VentanaPrincipal regresar=new VentanaPrincipal();regresar.setVisible(true);dispose();

Page 10: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 10/23

10

}});contentPane.add(btnMenu);

JButton btnRESULTADO = new JButton("RESULTADO");btnRESULTADO.setBounds(350, 17, 104, 23);btnRESULTADO.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

numero1 = num1.getText();numero2 = num2.getText();if(!isNumber(numero1) || !isNumber(numero2)){

DialogoError error=new DialogoError();error.setVisible(true);num1.setText("");num2.setText("");

}else{

numeroint1 = Integer.parseInt(num1.getText());numeroint2 = Integer.parseInt(num2.getText());

if(numeroint1>=numeroint2){

DialogoError error=new DialogoError();error.setVisible(true);num1.setText("");num2.setText("");

}else{Hilo1 h1=new Hilo1(numeroint1,

numeroint2,lblRESULTADO);Thread t1= new Thread(h1);

t1.start();}

}

}});contentPane.add(btnRESULTADO);

JButton btnBorrar = new JButton("BORRAR");btnBorrar.setBounds(350, 68, 104, 23);btnBorrar.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {num1.setText("");

num2.setText("");lblRESULTADO.setText("");

}});contentPane.add(btnBorrar);

JButton btnSalir = new JButton("SALIR");btnSalir.setBounds(226, 181, 89, 23);btnSalir.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

Page 11: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 11/23

11

System.exit(0);}

});btnSalir.setIcon(new

ImageIcon(VentanaPrimos.class.getResource("/javax/swing/plaf/metal/icons/ocean/paletteClose-pressed.gif")));

contentPane.add(btnSalir);

JScrollPane scrollPane = new JScrollPane();

scrollPane.setBounds(126, 120, 328, 34);contentPane.add(scrollPane);

lblRESULTADO = new JLabel("");scrollPane.setViewportView(lblRESULTADO);

}}

Código Ventana Palíndromos

import java.awt.BorderLayout;import java.awt.Event;

import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.ImageIcon;import java.awt.*;import javax.swing.*;

import java.util.*;

public class VentanaPalindromos extends JFrame{

private JPanel contentPane;private JTextField num1;private JTextField num2;private JLabel lblRESULTADO;private String numero1;private String numero2;private int numeroint1;private int numeroint2;

class Hilo2 implements Runnable{

private int inicio,fin;private String rs="";private String rserie="";private JLabel lbl;

public Hilo2(int num1,int num2,JLabel jlbl){

Page 12: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 12/23

12

inicio=num1;fin=num2;lbl=jlbl;

}

public void run(){

int contador=0;

int i;

int numinverso = 0;int div_entera;int resto_div;

String serie="";if(inicio>=0 && fin>0){

if(inicio<fin){

for(i=inicio;i<=fin;i++){

numinverso = 0;

div_entera = i;resto_div = 0;while (div_entera != 0){

resto_div = div_entera % 10;div_entera = div_entera / 10;numinverso = numinverso * 10 + resto_div;

}if (i == numinverso){

try{

Thread.currentThread();

Thread.sleep((long)(450));}catch(InterruptedException

e){

System.out.println(e.getMessage());}serie=serie+" "+i;

}lbl.setText(serie);

}

}

}elseif(inicio<0 && fin<0){

if(Math.abs(inicio)>Math.abs(fin)){

Page 13: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 13/23

13

for(i=inicio;i<=fin;i++){

numinverso = 0;div_entera = i;resto_div = 0;while (div_entera != 0){

resto_div = div_entera % 10;

div_entera = div_entera / 10;numinverso = numinverso * 10 + resto_div;

}if (i == numinverso){

try{

Thread.currentThread();

Thread.sleep((long)(450));}catch(InterruptedException

e)

{

System.out.println(e.getMessage());}serie=serie+" "+i;

}lbl.setText(serie);

}

}}

else

if(inicio<0 && fin>0){

for(i=1;i<=Math.abs(inicio);i++){

numinverso = 0;div_entera = i;resto_div = 0;while (div_entera != 0){

resto_div = div_entera % 10;div_entera = div_entera / 10;numinverso = numinverso * 10 + resto_div;

}if (i == numinverso){

try{

Thread.currentThread();Thread.sleep((long)(450));

}catch(InterruptedException e){

Page 14: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 14/23

14

System.out.println(e.getMessage());}serie=serie+" -"+i;

}lbl.setText(serie);

}

for(i=1;i<=fin;i++){

numinverso = 0;div_entera = i;resto_div = 0;while (div_entera != 0){

resto_div = div_entera % 10;div_entera = div_entera / 10;numinverso = numinverso * 10 + resto_div;

}if (i == numinverso){

try{

Thread.currentThread();Thread.sleep((long)(450));

}catch(InterruptedException e){

System.out.println(e.getMessage());}serie=serie+" "+i;

}lbl.setText(serie);

}

}}

}

public static void main(String[] args){

EventQueue.invokeLater(new Runnable(){

public void run(){

try

{VentanaPalindromos frame = new

VentanaPalindromos();frame.setVisible(true);

} catch (Exception e) {e.printStackTrace();

}}

});}

Page 15: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 15/23

15

private static boolean isNumber(String n){

try{

Integer.parseInt(n);return true;

} catch (NumberFormatException nfe) {

return false;}

}

public VentanaPalindromos(){

setTitle("VENTANA PALINDROMOS");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 494, 250);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);

contentPane.setLayout(null);

JLabel lbl1 = new JLabel("INGRESE EL INICIO DE LA SERIE:");lbl1.setBounds(20, 11, 199, 34);contentPane.add(lbl1);

JLabel lbl2 = new JLabel("INGRESE EL FIN DE LA SERIE:");lbl2.setBounds(20, 62, 177, 34);contentPane.add(lbl2);

num1 = new JTextField();num1.setBounds(229, 18, 86, 20);contentPane.add(num1);

num1.setColumns(10);

num2 = new JTextField();num2.setBounds(229, 69, 86, 20);contentPane.add(num2);num2.setColumns(10);

JLabel lbl3 = new JLabel("RESULTADO:");lbl3.setBounds(26, 120, 149, 34);contentPane.add(lbl3);

JButton btnMenu = new JButton("MENU");btnMenu.setBounds(350, 181, 104, 23);

btnMenu.setIcon(newImageIcon(VentanaPalindromos.class.getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));

btnMenu.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {

VentanaPrincipal regresar=new VentanaPrincipal();regresar.setVisible(true);dispose();

}});

Page 16: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 16/23

16

contentPane.add(btnMenu);

JButton btnRESULTADO = new JButton("RESULTADO");btnRESULTADO.setBounds(350, 17, 104, 23);btnRESULTADO.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

numero1 = num1.getText();numero2 = num2.getText();

if(!isNumber(numero1) || !isNumber(numero2)){

DialogoError error=new DialogoError();error.setVisible(true);num1.setText("");num2.setText("");

}else{

numeroint1 = Integer.parseInt(num1.getText());numeroint2 = Integer.parseInt(num2.getText());

if(numeroint1>=numeroint2)

{DialogoError error=new DialogoError();error.setVisible(true);num1.setText("");num2.setText("");

}else{Hilo2 h2=new Hilo2(numeroint1,

numeroint2,lblRESULTADO);Thread t2= new Thread(h2);t2.start();}

}

}});contentPane.add(btnRESULTADO);

JButton btnBorrar = new JButton("BORRAR");btnBorrar.setBounds(350, 68, 104, 23);btnBorrar.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {num1.setText("");num2.setText("");lblRESULTADO.setText("");

}});contentPane.add(btnBorrar);

JButton btnSalir = new JButton("SALIR");btnSalir.setBounds(226, 181, 89, 23);btnSalir.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {System.exit(0);

}

Page 17: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 17/23

17

});btnSalir.setIcon(new

ImageIcon(VentanaPalindromos.class.getResource("/javax/swing/plaf/metal/icons/ocean/paletteClose-pressed.gif")));

contentPane.add(btnSalir);

JScrollPane scrollPane = new JScrollPane();scrollPane.setBounds(126, 120, 328, 34);contentPane.add(scrollPane);

lblRESULTADO = new JLabel("");scrollPane.setViewportView(lblRESULTADO);

}}

Código Panel Imagen.java

import java.awt.Dimension;import java.awt.Graphics;import javax.swing.ImageIcon;

public class PanelImagen extends javax.swing.JPanel {public PanelImagen(){

this.setSize(400,280);}@Overridepublic void paintComponent (Graphics g){

Dimension tamanio = getSize();ImageIcon imagenFondo = new

ImageIcon(getClass().getResource("/img/imag.jpg"));g.drawImage(imagenFondo.getImage(),0,0,tamanio.width,

tamanio.height, null);setOpaque(false);super.paintComponent(g);

}}

Código JFrame con Fondo.java

import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import java.awt.Toolkit;import javax.swing.JLabel;import java.awt.Font;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.Color;import java.awt.SystemColor;public class JFrameConFondo extends JFrame {

public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {

Page 18: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 18/23

18

public void run() {try {

JFrameConFondo frame = new JFrameConFondo();frame.setVisible(true);

} catch (Exception e) {e.printStackTrace();

}}

});

}public JFrameConFondo() {

setForeground(SystemColor.textHighlight);

setIconImage(Toolkit.getDefaultToolkit().getImage(JFrameConFondo.class.getResource("/com/sun/java/swing/plaf/motif/icons/Inform.gif")));

setTitle("Informaci\u00F3n");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 432, 168);PanelImagen p = new PanelImagen();p.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(p);p.setLayout(null);

JLabel lblRealizadoPor = new JLabel("REALIZADO POR:");lblRealizadoPor.setForeground(Color.RED);lblRealizadoPor.setFont(new Font("Calisto MT", Font.BOLD, 12));lblRealizadoPor.setBounds(181, 21, 136, 27);p.add(lblRealizadoPor);

JLabel lblFreddyAlvarez = new JLabel("FREDDY ALVAREZ");lblFreddyAlvarez.setForeground(Color.BLUE);lblFreddyAlvarez.setFont(new Font("Californian FB", Font.ITALIC,

15));lblFreddyAlvarez.setBounds(210, 59, 196, 27);p.add(lblFreddyAlvarez);

JButton btnOk = new JButton("OK");btnOk.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {VentanaPrincipal prin=new VentanaPrincipal();prin.setVisible(true);dispose();

}});btnOk.setBounds(351, 96, 55, 23);p.add(btnOk);

}

}

Código Diálogo Error

import java.awt.BorderLayout;import java.awt.FlowLayout;

import javax.swing.JButton;

Page 19: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 19/23

19

import javax.swing.JDialog;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JLabel;import java.awt.Toolkit;import java.awt.Color;import java.awt.Font;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;

public class DialogoError extends JDialog {

private final JPanel contentPanel = new JPanel();

/*** Launch the application.*/public static void main(String[] args) {

try {DialogoError dialog = new DialogoError();dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

dialog.setVisible(true);} catch (Exception e) {

e.printStackTrace();}

}

/*** Create the dialog.*/public DialogoError() {

setIconImage(Toolkit.getDefaultToolkit().getImage(DialogoError.class.getResource("/javax/swing/plaf/metal/icons/Warn.gif")));

setTitle("ERROR!!!");setBounds(100, 100, 310, 116);getContentPane().setLayout(new BorderLayout());contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));getContentPane().add(contentPanel, BorderLayout.CENTER);contentPanel.setLayout(null);

JLabel lblIngreseValoresCorrectos = new JLabel("ERROR!!! INGRESEVALORES CORRECTOS");

lblIngreseValoresCorrectos.setFont(new Font("Tahoma", Font.BOLD,11));

lblIngreseValoresCorrectos.setForeground(new Color(255, 0, 0));lblIngreseValoresCorrectos.setBounds(36, 11, 360, 25);

contentPanel.add(lblIngreseValoresCorrectos);{

JPanel buttonPane = new JPanel();buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));getContentPane().add(buttonPane, BorderLayout.SOUTH);{

JButton okButton = new JButton("OK");okButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {dispose();

Page 20: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 20/23

20

}});okButton.setActionCommand("OK");buttonPane.add(okButton);getRootPane().setDefaultButton(okButton);

}}

}}

Ventana Principal

Ventana Primos

Page 21: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 21/23

21

Ventana Palíndromos

Ventana Diálogo Error

Page 22: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 22/23

22

Ventana JFrame con Fondo

Vista Ejecución

Page 23: INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

8/10/2019 INTERFAZ GRAFICA CON HILOS JAVA ECLIPSE

http://slidepdf.com/reader/full/interfaz-grafica-con-hilos-java-eclipse 23/23

23