|
Mostrar Temas
|
Páginas: [1]
|
1
|
Programación / Java / problema con BufferedReader
|
en: 15 Junio 2007, 20:51 pm
|
hola: Tengo un ciclo para leer de buffer y luego imprimir lo que leyo en un TextArea y la manera de detener el ciclo es cuando el buffer sea Null. en el Text Area deberia aparece escrito la salida de un Script que ejecuto en UNIX como se ve en color rojo. Ingrese una opcion [1]Prueba [2]Saliruna vez que aparece esto se debe ingresar la opcion y sigue la ejecucion del script. pero aparentemente el buffer nunca se hace null y se queda imprimiendo siempre lo mismo en un ciclo infinito y no se le pueden enviar los datos para la seleccion de la opcion [1] o [2]... alguien tendra alguna sugerencia para que esto no suceda?? este es el codigo del hilo que lee el buffer de entrada. muchas gracias prestar atencion en esta parte del codigo while ((line = in.readLine()) != null) { t.printOut(line); } class InputThread implements Runnable { BufferedReader in; Main t; public boolean keepGoing; public InputThread(BufferedReader in, Main t) { this.in = in; this.t = t; this.keepGoing = true; } public void quitIt() { keepGoing = false; } public void run() { while (keepGoing) { String line; try { while ((line = in.readLine()) != null) { t.printOut(line); } } catch (IOException e) { t.printOut(e.getMessage()); } } } }
|
|
|
2
|
Programación / Java / Scroll automatico en un JScrollPane
|
en: 14 Junio 2007, 15:19 pm
|
hola
tengo un JScrollPane con un JText Area y a medida que voy escribiendo texto quisiera que se hiciera un scroll automatico pues hasta ahora tengo que hacerlo manualmente con el scroll del raton.
existe algun metodo de la clase JScrollPane que haga esto??
muchas gracias
|
|
|
3
|
Programación / Java / Interfaz grafica y clase process
|
en: 7 Junio 2007, 15:32 pm
|
hola: estoy desarrollando esta interfaz grafica.... cuando ejecuto los comandos dir, ipconfig, ping me muestra la salida en el cuadro de texto. pero con el comando date se tranca el programa pq espera a que le envie una fecha para actualizar... dejo el codigo completo para el que lo quiera ver y me pueda ayudar... gracias // Sistema de Gestión MTSO San Cristóbal // Realizado por: Wilmer Rondón import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.Color; import javax.swing.JTabbedPane; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JButton; import java.io.*; import java.lang.ProcessBuilder; //******************************************************************************************************************************** // CLASE PRINCIPAL DEL PROGRAMA "VENTANA" public class Main extends JPanel { protected Boolean bd1 = false, bd2 = false, bd3 = false, bd4 = false; protected JTextField texto1, texto2, texto3, texto4 ; protected JTextArea areaTexto1, areaTexto2, areaTexto3, areaTexto4 ; protected JPanel p1A, p1B, p1C, p1D, p2A, p2B, p2C, p2D, p3A, p3B, p3C, p3D, p4A, p4B, p4C, p4D ; protected JButton btOK1, btQuitar1, btSalir1, btOK2, btQuitar2, btSalir2, btOK3, btQuitar3, btSalir3, btOK4, btQuitar4, btSalir4 ; protected String texto1Inicio, texto2Inicio, texto3Inicio, texto4Inicio ; protected Color B, F1, F2, F3 ; protected int orden; protected final static String newline = "\n"; //******************************************************************************************************************************** // CONSTRUCTOR DE LA CLASE VENTANA public Main() { Component panel_pestana1 = pestana1 ("Pestana Uno"); pestanas.addTab("Troncales", icon1, panel_pestana1); Component panel_pestana2 = pestana2 ("Pestana Dos"); pestanas.addTab("Packet Pipes", icon2, panel_pestana2); Component panel_pestana3 = pestana3 ("Pestana Tres"); pestanas.addTab("Celdas", icon3, panel_pestana3); Component panel_pestana4 = pestana4 ("Pestana Cuatro"); pestanas.addTab("Mantenimiento", icon4, panel_pestana4); add(pestanas); } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA PESTAÑA "TRONCALES" { p1D = new JPanel(new GridLayout(1, 3)); // panel para 3 botones en BorderLayout.SOUTH Component panel_subpestana11 = subpestana11 ("Crear"); subpestanas1.addTab("Crear", panel_subpestana11); Component panel_subpestana12 = subpestana12 ("Crecer"); subpestanas1.addTab("Crecer", panel_subpestana12); Component panel_subpestana13 = subpestana13 ("Dedrecer"); subpestanas1.addTab("Dedrecer", panel_subpestana13); Component panel_subpestana14 = subpestana14 ("Eliminar"); subpestanas1.addTab("Eliminar", panel_subpestana14); Component panel_subpestana15 = subpestana15 ("Inclusión en Reporte TGCOM"); subpestanas1.addTab("Inclusión en Reporte TGCOMP", panel_subpestana15); Component panel_subpestana16 = subpestana16 ("Mudanza"); subpestanas1.addTab("Mudanza", panel_subpestana16); texto1. setFont(new java. awt. Font("Arial", Font. PLAIN, 12)); texto1Inicio = "Escriba aquí un comando y haga click sobre el botón \"OK\""; texto1.setText(texto1Inicio); areaTexto1.setEditable(false); areaTexto1. setFont(new java. awt. Font("Arial", Font. PLAIN, 12)); B = new Color(200, 200, 200); areaTexto1.setBackground(B); areaTexto1.setForeground(F1); btQuitar1 = new JButton("Limpiar pantalla"); texto1.addActionListener(new ejecutar()); btOK1.addActionListener(new ejecutar()); btOK1.addActionListener(new bdTroncales()); btQuitar1.addActionListener(new limpiarPantalla()); btSalir1.addActionListener(new cerrarVentana()); p1A.add(p1B); p1A.add(p1C); p1B.add(subpestanas1);//agregar sub pestanas p1D.add(btOK1); p1D.add(btQuitar1); p1D.add(btSalir1); btOK1.setToolTipText("Ingresar Comando"); btQuitar1.setToolTipText("Limpiar la Pantalla"); btSalir1.setToolTipText("Salir de la aplicación"); return p1A; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA PESTAÑA "PACKET PIPES" { p2D = new JPanel(new GridLayout(1, 3)); // panel para 3 botones en BorderLayout.SOUTH Component panel_subpestana21 = subpestana21 ("Mudar"); subpestanas2.addTab("Mudar", panel_subpestana21); Component panel_subpestana22 = subpestana22 ("Crecer"); subpestanas2.addTab("Crecer", panel_subpestana22); Component panel_subpestana23 = subpestana23 ("Dedrecer"); subpestanas2.addTab("Dedrecer", panel_subpestana23); Component panel_subpestana24 = subpestana24 ("Equipar"); subpestanas2.addTab("Equipar", panel_subpestana24); Component panel_subpestana25 = subpestana25 ("Desequipar"); subpestanas2.addTab("Desequipar", panel_subpestana25); texto2. setFont(new java. awt. Font("Arial", Font. PLAIN, 12)); texto2Inicio = "Escriba aquí un comando y haga click sobre el botón \"OK\""; texto2.setText(texto2Inicio); areaTexto2.setEditable(false); areaTexto2. setFont(new java. awt. Font("Arial", Font. PLAIN, 12)); B = new Color(200, 200, 200); areaTexto2.setBackground(B); areaTexto2.setForeground(F1); btQuitar2 = new JButton("Limpiar pantalla"); texto2.addActionListener(new ejecutar()); btOK2.addActionListener(new ejecutar()); btQuitar2.addActionListener(new limpiarPantalla()); btSalir2.addActionListener(new cerrarVentana()); p2A.add(p2B); p2A.add(p2C); p2B.add(subpestanas2);//agregar sub pestanas p2D.add(btOK2); p2D.add(btQuitar2); p2D.add(btSalir2); btOK2.setToolTipText("Ingresar Comando"); btQuitar2.setToolTipText("Limpiar la Pantalla"); btSalir2.setToolTipText("Salir de la aplicación"); return p2A; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA PESTAÑA "CELDAS" { p3D = new JPanel(new GridLayout(1, 3)); // panel para 3 botones en BorderLayout.SOUTH Component panel_subpestana31 = subpestana31 ("Mudanza de RCS"); subpestanas3.addTab("Mudanza de RCS", panel_subpestana31); Component panel_subpestana32 = subpestana32 ("Crear Celda"); subpestanas3.addTab("Crear Celda", panel_subpestana32); Component panel_subpestana33 = subpestana33 ("Borrar Celda"); subpestanas3.addTab("Borrar Celda", panel_subpestana33); Component panel_subpestana34 = subpestana34 ("---"); subpestanas3.addTab("---", panel_subpestana34); Component panel_subpestana35 = subpestana35 ("---"); subpestanas3.addTab("---", panel_subpestana35); texto3. setFont(new java. awt. Font("Arial", Font. PLAIN, 12)); texto3Inicio = "Escriba aquí un comando y haga click sobre el botón \"OK\""; texto3.setText(texto3Inicio); areaTexto3.setEditable(false); areaTexto3. setFont(new java. awt. Font("Arial", Font. PLAIN, 12)); B = new Color(200, 200, 200); areaTexto3.setBackground(B); areaTexto3.setForeground(F1); btQuitar3 = new JButton("Limpiar pantalla"); texto3.addActionListener(new ejecutar()); btOK3.addActionListener(new ejecutar()); btQuitar3.addActionListener(new limpiarPantalla()); btSalir3.addActionListener(new cerrarVentana()); p3A.add(p3B); p3A.add(p3C); p3B.add(subpestanas3);//agregar sub pestanas p3D.add(btOK3); p3D.add(btQuitar3); p3D.add(btSalir3); btOK3.setToolTipText("Ingresar Comando"); btQuitar3.setToolTipText("Limpiar la Pantalla"); btSalir3.setToolTipText("Salir de la aplicación"); return p3A; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA PESTAÑA "MANTENIMIENTO" { p4D = new JPanel(new GridLayout(1, 3)); // panel para 3 botones en BorderLayout.SOUTH Component panel_subpestana41 = subpestana41 ("CKT OOS"); subpestanas4.addTab("CKT OOS", panel_subpestana41); Component panel_subpestana42 = subpestana42 ("TST UPATH"); subpestanas4.addTab("TST UPATH", panel_subpestana42); Component panel_subpestana43 = subpestana43 ("MEM NRODD/RODD SM's"); subpestanas4.addTab("MEM NRODD/RODD SM's", panel_subpestana43); Component panel_subpestana44 = subpestana44 ("Información del Sistema"); subpestanas4.addTab("Información del Sistema", panel_subpestana44); Component panel_subpestana45 = subpestana45 ("BACKUP's DE AP's"); subpestanas4.addTab("BACKUP's DE AP's", panel_subpestana45); Component panel_subpestana46 = subpestana46 ("CHKLIST MTSO"); subpestanas4.addTab("CHKLIST MTSO", panel_subpestana46); texto4. setFont(new java. awt. Font("Arial", Font. PLAIN, 12)); texto4Inicio = "Escriba aquí un comando y haga click sobre el botón \"OK\""; texto4.setText(texto4Inicio); areaTexto4.setEditable(false); areaTexto4. setFont(new java. awt. Font("Arial", Font. PLAIN, 12)); B = new Color(200, 200, 200); areaTexto4.setBackground(B); areaTexto4.setForeground(F1); btQuitar4 = new JButton("Limpiar pantalla"); texto4.addActionListener(new ejecutar()); btOK4.addActionListener(new ejecutar()); btQuitar4.addActionListener(new limpiarPantalla()); btSalir4.addActionListener(new cerrarVentana()); p4A.add(p4B); p4A.add(p4C); p4B.add(subpestanas4);//agregar sub pestanas p4D.add(btOK4); p4D.add(btQuitar4); p4D.add(btSalir4); btOK4.setToolTipText("Ingresar Comando"); btQuitar4.setToolTipText("Limpiar la Pantalla"); btSalir4.setToolTipText("Salir de la aplicación"); return p4A; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "CREAR" DE "TRONCALES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "CRECER" DE "TRONCALES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "DECRECER" DE "TRONCALES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "ELIMINAR" DE "TRONCALES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "INCLUSION EN REPORTE TGCOMP" DE "TRONCALES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "MUDANZA" DE "TRONCALES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "MUDAR" DE "PACKET PIPES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "CRECER" DE "PACKET PIPES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "DECRECER" DE "PACKET PIPES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "EQUIPAR" DE "PACKET PIPES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "DESEQUIPAR" DE "PACKET PIPES" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "MUDANZA DE RCS" DE "CELDAS" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "CREAR" DE "CELDAS" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "BORRAR" DE "CELDAS" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "---" DE "CELDAS" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "---" DE "CELDAS" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "CKT OOS" DE "MANTENIMIENTO" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "TST UPATH" DE "MANTENIMIENTO" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "MEM NRODD/RODD SM's" DE "MANTENIMIENTO" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "INFORMACION DEL SISTEMA" DE "MANTENIMIENTO" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "BACKUP's DE AP's" DE "MANTENIMIENTO" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // METODO PARA CONSTRUIR LA SUBPESTAÑA "CHKLIST MTSO" DE "MANTENIMIENTO" { filler. setHorizontalAlignment(JLabel. CENTER); panel.add(filler); return panel; } //******************************************************************************************************************************** // MÉTODO DE ENTRADA AL PROGRAMA public static void main (String[] args ) { JFrame. setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Sistema de Gestión MTSO San Cristóbal"); frame. getContentPane(). add(new Main (), BorderLayout. CENTER); frame.setSize(1280, 770); frame.setVisible(true); frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); } //******************************************************************************************************************************** // CLASE PARA MOSTRAR LAS SALIDAS DE LOS COMANDOS EN PANTALLA { { String texto = texto1. getText(); //String[] texto = {"date"}; try { // Se lanza el ejecutable. Process p = rt. exec("cmd /c " + texto ); // Se obtiene el stream de salida del programa // Se prepara un bufferedReader para poder leer la salida más // comodamente. // Se lee la primera linea br3.write("07-06-2007\n"); // p.waitFor(); // Mientras se haya leido alguna linea while (aux!=null) { // Se escribe la linea en pantalla areaTexto1.append(aux + newline); areaTexto1.setCaretPosition(areaTexto1.getDocument().getLength()); texto1.setText(""); // y se lee la siguiente. aux = br.readLine(); } while (aux2!=null) { areaTexto1.append(aux2 + newline); areaTexto1.setCaretPosition(areaTexto1.getDocument().getLength()); texto1.setText(""); aux2 = br2.readLine(); } } { // Excepciones si hay algún problema al arrancar el ejecutable o al // leer su salida. e.printStackTrace(); } } } //******************************************************************************************************************************** // CLASE QUE ACTIVA LA BANDERA DE ESCRITURA EN TEXT AREA { { bd1 = true; } } //******************************************************************************************************************************** // CLASE QUE VACÍA TEXTAREA { { texto1. setFont(new java. awt. Font("Arial", Font. PLAIN, 12)); texto1.setForeground(F3); texto1.setText(""); texto2.setText(""); } } //******************************************************************************************************************************** // CLASE QUE SALE DEL PROGRAMA { { } } //******************************************************************************************************************************** } //FIN DE LA CLASE VENTANA
|
|
|
4
|
Programación / Java / como usar Process.getOutputStream()
|
en: 6 Junio 2007, 22:57 pm
|
hola: alguien me puede decir como usar Process.getOutputStream() para responder a comando ejecutado desde CMD de windows como por ejemplo el comando DATE que despues de ejecutado muestra la fecha actual y espera a que se ingrese la fecha nueva. este es parte del codigo que llevo hecho... puedo leer mas no escribir class ejecutar implements ActionListener { public void actionPerformed(ActionEvent evt) { String texto = texto1.getText(); Runtime rt = Runtime.getRuntime(); //String[] texto = {"date"}; try { // Se lanza el ejecutable. Process p = rt.exec("cmd /c " + texto); p.waitFor(); // Se obtiene el stream de salida del programa InputStream is = p.getInputStream(); InputStream er = p.getErrorStream(); OutputStream sal = p.getOutputStream(); // int wait = p.waitFor(); // Se prepara un bufferedReader para poder leer la salida más // comodamente. BufferedReader br = new BufferedReader (new InputStreamReader (is)); BufferedReader br2 = new BufferedReader (new InputStreamReader (er)); // Se lee la primera linea String aux = br.readLine(); String aux2 = br2.readLine(); // Mientras se haya leido alguna linea while (aux!=null) { // Se escribe la linea en pantalla areaTexto1.append(aux + newline); areaTexto1.setCaretPosition(areaTexto1.getDocument().getLength()); texto1.setText(""); // y se lee la siguiente. aux = br.readLine(); } while (aux2!=null) { areaTexto1.append(aux2 + newline); areaTexto1.setCaretPosition(areaTexto1.getDocument().getLength()); texto1.setText(""); aux2 = br2.readLine(); } } catch (Exception e) { // Excepciones si hay algún problema al arrancar el ejecutable o al // leer su salida. e.printStackTrace(); } } }
|
|
|
5
|
Programación / Java / interfaz grafica de unix en java
|
en: 30 Mayo 2007, 16:22 pm
|
hola: quiero hacer una interfaz grafica para un scritp shell de unix. consta de un textfield donde escribo un comando o un script y un textarea donde se muestra la salida de dicho comando o script he hecho pruebas en windows y en unix la aplicion funciona bien en windows cuando ejecuto comandos como "dir", "cd", "vol", es decir me muestra perfectamente la salida de dichos comandos. en Unix tambien funciona cuando ejecuto comandos como "ls", "pwd" entre otros. el problema que tengo es que cuando ejecuto un comando que necesite de un posterior ingreso de datos la aplicacion se tranca. por ejemplo: cuando ingreso el comando "date" en windows me muestra la fecha actual y me pide ingresar una nueva fecha para actualizarla. es aqui cuando se tranca el programa y tengo que cerrarlo forzosamente igual me pasa para un script en unix que requiere ingreso de datos posterior a su ejecucion entiendo que runtime.exec() ejecuta un comando en la ruta especifica pero despues termina el proceso. como hago para poder ingresar datos e interactuar con la shell una vez que la inicializo??? espero haber sido lo mas claro posible... muchas gracias por la ayuda... este es el codigo de mi aplicacion // Sistema de Gestión MTSO San Cristóbal // Realizado por: Wilmer Rondón
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.Color; import javax.swing.JTabbedPane; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JButton; import java.io.*;
// CLASE PRINCIPAL DEL PROGRAMA public class Ventana extends JPanel { protected JTextField texto1; protected JTextArea texto2; protected JPanel p1, p2; protected JButton btOK, btQuitar, btSalir; protected String texto1Inicio; protected Color B, F1, F2, F3; protected int orden; protected final static String newline = "\n";
public Ventana() { ImageIcon icon1 = new ImageIcon("images/BotonInicioOff.gif"); ImageIcon icon2 = new ImageIcon("images/BotonSalirOff.gif");
JTabbedPane pestanas = new JTabbedPane(); Component panel_pestana1 = pestana1("Pestana Uno"); pestanas.addTab("Comandos", icon1, panel_pestana1); Component panel_pestana2 = pestana2("Pestana Dos"); pestanas.addTab("Etiqueta", icon2, panel_pestana2); setLayout(new GridLayout(1, 1)); add(pestanas); }//Fin Constructor Ventana
protected Component pestana1(String text) { p1 = new JPanel(new BorderLayout()); // panel Principal p2 = new JPanel(new GridLayout(1,3)); // panel para 3 botones en BorderLayout.SOUTH p1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
texto1 = new JTextField(40); texto1.setFont(new java.awt.Font("Arial", Font.PLAIN, 12)); texto1Inicio = "Escriba aquí un comando y haga click sobre el botón \"OK\""; texto1.setText(texto1Inicio); texto2 = new JTextArea(10,40); texto2.setEditable(false); texto2.setFont(new java.awt.Font("Arial", Font.PLAIN, 12)); B = new Color(200,200,200); texto2.setBackground(B); F1 = new Color(0,0,0); texto2.setForeground(F1); JScrollPane pScroll = new JScrollPane(texto2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
btOK = new JButton("OK"); btQuitar = new JButton("Limpiar pantalla"); btSalir = new JButton("Salir"); texto1.addActionListener(new ponerTexto()); btOK.addActionListener(new ponerTexto()); btQuitar.addActionListener(new limpiarPantalla()); btSalir.addActionListener(new cerrarVentana()); p1.add(texto1, BorderLayout.NORTH); p1.add(pScroll, BorderLayout.CENTER); p1.add(p2,BorderLayout.SOUTH); p2.add(btOK); p2.add(btQuitar); p2.add(btSalir); btOK.setToolTipText("Ingresar Comando"); btQuitar.setToolTipText("Limpiar la Pantalla"); btSalir.setToolTipText("Salir de la aplicación"); return p1; }//Fin método pestana1
protected Component pestana2(String text) { JPanel panel = new JPanel(false); JLabel filler = new JLabel(text); JButton boton = new JButton(text); filler.setHorizontalAlignment(JLabel.CENTER); panel.setLayout(new GridLayout(2, 2)); panel.add(boton); panel.add(filler); return panel; }
// MÉTODO DE ENTRADA AL PROGRAMA public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Sistema de Gestión MTSO San Cristóbal"); frame.getContentPane().add(new Ventana(), BorderLayout.CENTER); frame.setSize(1280, 770); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
// CLASE QUE AnADE LÍNEAS DESDE JTEXTFIELD A TEXTAREA class ponerTexto implements ActionListener { public void actionPerformed(ActionEvent evt) { String texto = texto1.getText(); //String[] texto = {"date"}; try { // Se lanza el ejecutable. Process p = Runtime.getRuntime().exec("cmd /c " + texto); //p.waitFor(); // Se obtiene el stream de salida del programa InputStream is = p.getInputStream(); InputStream er = p.getErrorStream(); // Se prepara un bufferedReader para poder leer la salida más // comodamente. BufferedReader br = new BufferedReader (new InputStreamReader (is)); BufferedReader br2 = new BufferedReader (new InputStreamReader (er)); // Se lee la primera linea String aux = br.readLine(); String aux2 = br2.readLine(); // Mientras se haya leido alguna linea while (aux!=null) { // Se escribe la linea en pantalla texto2.append(aux + newline); texto2.setCaretPosition(texto2.getDocument().getLength()); texto1.setText(""); // y se lee la siguiente. aux = br.readLine(); } while (aux2!=null) { texto2.append(aux2 + newline); texto2.setCaretPosition(texto2.getDocument().getLength()); texto1.setText(""); aux2 = br2.readLine(); } } catch (Exception e) { // Excepciones si hay algún problema al arrancar el ejecutable o al // leer su salida. e.printStackTrace(); } } }
// CLASE QUE VACÍA TEXTAREA class limpiarPantalla implements ActionListener { public void actionPerformed(ActionEvent evt) { texto1.setFont(new java.awt.Font("Arial", Font.PLAIN, 12)); F3 = new Color(0,0,0); texto1.setForeground(F3); texto1.setText(texto1Inicio); texto2.setText(""); texto2.append("#"); } } // CLASE QUE SALE DEL PROGRAMA class cerrarVentana implements ActionListener { public void actionPerformed(ActionEvent evt) { System.exit(0); } } }//Fin class Ventana
|
|
|
6
|
Programación / Java / Interfaz grafica en java para un script shell de unix
|
en: 28 Mayo 2007, 22:27 pm
|
hola:
estoy desarrollando una aplicacion en Java y realmente no tengo mucha experiencia con este lenguaje.
tengo varios scripts en unix que automatizan tareas rutinarias.
deseo hacer una interfaz grafica con java para ejecutarla en xwindows que me permita ejecutar los scripts de unix haciendo click en un boton y poder visualizar en un cuadro de texto de la misma interfaz grafica las repuestas que se generen en la shell.
no se si queda claro el objetivo de mi interfaz grafica
espero que me puedan ayudar
aqui les dejo el codigo de unas pruebas que he ido haciendo para probar no se si voy por buen camino
// Sistema de Gestión MTSO San Cristóbal // Realizado por: Wilmer Rondón
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.Color; import javax.swing.JTabbedPane; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JButton; import java.io.*;
// CLASE PRINCIPAL DEL PROGRAMA public class Ventana extends JPanel { protected JTextField texto1; protected JTextArea texto2; protected JPanel p1, p2; protected JButton btOK, btQuitar, btSalir; protected String texto1Inicio; protected Color B, F1, F2, F3; protected int orden; protected final static String newline = "\n";
public Ventana() { ImageIcon icon1 = new ImageIcon("images/BotonInicioOff.gif"); ImageIcon icon2 = new ImageIcon("images/BotonSalirOff.gif");
JTabbedPane pestanas = new JTabbedPane(); Component panel_pestana1 = pestana1("Pestana Uno"); //Linea 35 pestanas.addTab("Comandos", icon1, panel_pestana1); Component panel_pestana2 = pestana2("Pestana Dos"); pestanas.addTab("Etiqueta", icon2, panel_pestana2); setLayout(new GridLayout(1, 1)); add(pestanas); }//Fin Constructor Ventana
protected Component pestana1(String text) { p1 = new JPanel(new BorderLayout()); // panel Principal p2 = new JPanel(new GridLayout(1,3)); // panel para 3 botones en BorderLayout.SOUTH p1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
texto1 = new JTextField(40); texto1.setFont(new java.awt.Font("Arial", Font.PLAIN, 12)); texto1Inicio = "Escriba aquí un comando y haga click sobre el botón \"OK\""; texto1.setText(texto1Inicio); texto2 = new JTextArea(10,40); texto2.setEditable(false); texto2.setFont(new java.awt.Font("Arial", Font.PLAIN, 12)); B = new Color(200,200,200); texto2.setBackground(B); F1 = new Color(0,0,0); texto2.setForeground(F1); texto2.append("#"); JScrollPane pScroll = new JScrollPane(texto2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
btOK = new JButton("OK"); btQuitar = new JButton("Limpiar pantalla"); btSalir = new JButton("Salir"); texto1.addActionListener(new ponerTexto()); btOK.addActionListener(new ponerTexto()); btQuitar.addActionListener(new limpiarPantalla()); btSalir.addActionListener(new cerrarVentana()); p1.add(texto1, BorderLayout.NORTH); p1.add(pScroll, BorderLayout.CENTER); p1.add(p2,BorderLayout.SOUTH); p2.add(btOK); p2.add(btQuitar); p2.add(btSalir); btOK.setToolTipText("Ingresar Comando"); //Linea 78 btQuitar.setToolTipText("Limpiar la Pantalla"); btSalir.setToolTipText("Salir de la aplicación"); return p1; }//Fin método pestana1
protected Component pestana2(String text) { JPanel panel = new JPanel(false); JLabel filler = new JLabel(text); JButton boton = new JButton(text); filler.setHorizontalAlignment(JLabel.CENTER); panel.setLayout(new GridLayout(2, 2)); panel.add(boton); panel.add(filler); return panel; }
// MÉTODO DE ENTRADA AL PROGRAMA public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Sistema de Gestión MTSO San Cristóbal"); frame.getContentPane().add(new Ventana(), BorderLayout.CENTER); frame.setSize(1280, 770); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
// CLASE QUE AnADE LÍNEAS DESDE JTEXTFIELD A TEXTAREA class ponerTexto implements ActionListener { public void actionPerformed(ActionEvent evt) { String texto = texto1.getText(); try { // Se lanza el ejecutable. Process p = Runtime.getRuntime().exec("cmd /c " + texto); // Se obtiene el stream de salida del programa InputStream is = p.getInputStream(); // Se prepara un bufferedReader para poder leer la salida más // comodamente. BufferedReader br = new BufferedReader (new InputStreamReader (is)); // Se lee la primera linea String aux = br.readLine(); // Mientras se haya leido alguna linea while (aux!=null) { // Se escribe la linea en pantalla texto2.append(aux + newline); texto2.setCaretPosition(texto2.getDocument().getLength()); texto1.setText(""); // y se lee la siguiente. aux = br.readLine(); } } catch (Exception e) { // Excepciones si hay algún problema al arrancar el ejecutable o al // leer su salida. e.printStackTrace(); } } }
// CLASE QUE VACÍA TEXTAREA class limpiarPantalla implements ActionListener { public void actionPerformed(ActionEvent evt) { texto1.setFont(new java.awt.Font("Arial", Font.PLAIN, 12)); F3 = new Color(0,0,0); texto1.setForeground(F3); texto1.setText(texto1Inicio); texto2.setText(""); texto2.append("#"); } } // CLASE QUE SALE DEL PROGRAMA class cerrarVentana implements ActionListener { public void actionPerformed(ActionEvent evt) { System.exit(0); } } }//Fin class Ventana
|
|
|
|
|
|
|