elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


  Mostrar Mensajes
Páginas: 1 2 3 4 5 [6] 7 8 9 10
51  Programación / Java / Re: JTextPane con scroll en: 13 Septiembre 2013, 21:43 pm
Parece que e cantado victoria demasiado deprisa, ya que el scroll al recibir mucha información parece que colapsa.

No se como hacer una class que herede propiedades de JTextArea sorry soy noob, podrías pasarme algun link para orientarme si no es mucha molestia.

Un saludo y gracias.
52  Programación / Java / Re: JTextPane con scroll en: 13 Septiembre 2013, 19:39 pm
Ya encontre la solución en esta remota web: http://java-sl.com/tip_html_letter_wrap.html

Un saludo
53  Programación / Java / JTextPane con scroll en: 13 Septiembre 2013, 17:03 pm
Hola buenas, estoy haciendo una gui en la que tengo un JTextPane montado en un JScrollPane, y tengo el problema de que si escribo textos muy largos también se activa el scroll horizontal, me gustaría que únicamente se activase el vertical y que el texto se fuese apilando.

Se que con un JTextArea está el .setLineWrap(true) que lo hace perfectamente pero el JTextPane no lo admite.

Ya e probado lo típico de HORIZONTAL_SCROLLBAR_NEVER pero unicamente la oculta.

Un saludo y como siempre gracias de antemano.
54  Programación / Java / Re: mostrar en grupos de 5 50 al azar. error que no arreglo en: 12 Septiembre 2013, 16:52 pm
Te falta especificar el tamaño de la array en la línea 7 sería así:

Código
  1. public int[] mazo = new int[50];

O debajo de la línea 13 le agregas:

Código
  1. this.mazo = new int[numeroCartas];

Un saludo
55  Programación / Java / Re: No entiendo planteamiento de un Thread en: 11 Septiembre 2013, 12:20 pm
Bien, gracias por tu rápida respuesta.

Yo lo e hecho de esta manera, sin Runnable, por eso quería saber si tal y como yo lo hago esta bien.

Código
  1. import java.io.IOException;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4.  
  5. import javax.swing.DefaultListModel;
  6.  
  7.  
  8. public class Server {
  9.  
  10. private DefaultListModel<String> allMessages = new DefaultListModel<String>();
  11.  
  12. public Server(){
  13. try {
  14. @SuppressWarnings("resource")
  15. ServerSocket serverSocket = new ServerSocket(9000);
  16. while(true){
  17. Socket newClientSocket = serverSocket.accept();
  18. ThreadOfClient newClientThread = new ThreadOfClient(allMessages,newClientSocket);
  19. newClientThread.start();
  20. }
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26.  

Código
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.net.Socket;
  4.  
  5. import javax.swing.DefaultListModel;
  6. import javax.swing.event.ListDataEvent;
  7. import javax.swing.event.ListDataListener;
  8.  
  9.  
  10. public class ThreadOfClient extends Thread implements ListDataListener{
  11.  
  12. private DefaultListModel<String> allMessages = new DefaultListModel<String>();
  13. private DataInputStream input;
  14. private DataOutputStream output;
  15.  
  16. public ThreadOfClient(DefaultListModel<String> allMessages, Socket socket){
  17. this.allMessages = allMessages;
  18. try {
  19. input = new DataInputStream(socket.getInputStream());
  20. output = new DataOutputStream(socket.getOutputStream());
  21. this.allMessages.addListDataListener(this);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. }
  26.  
  27. public void run(){
  28. try {
  29. while(true){
  30. String messageOfServer = input.readUTF();
  31. synchronized(allMessages){
  32. allMessages.addElement(messageOfServer);
  33. }
  34. }
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39.  
  40. @Override
  41. public void contentsChanged(ListDataEvent e) {
  42.  
  43.  
  44. }
  45.  
  46. @Override
  47. public void intervalAdded(ListDataEvent e) {
  48. String text = (String) allMessages.getElementAt(e.getIndex0());
  49. try {
  50. output.writeUTF(text);
  51. } catch (Exception exception) {
  52. exception.printStackTrace();
  53. }
  54.  
  55. }
  56.  
  57. @Override
  58. public void intervalRemoved(ListDataEvent e) {
  59.  
  60.  
  61. }
  62. }
  63.  
  64.  

Un saludo y como siempre mil gracias ;)
56  Programación / Java / No entiendo planteamiento de un Thread en: 11 Septiembre 2013, 11:02 am
Bien estoy trasteando con sockets, y me tope con uno de los tutoriales de Chuidiang, que haría yo sin su wiki  ;D.

Aqui esta el link:

http://www.chuidiang.com/java/sockets/hilos/socket_hilos.php

Código
  1. HiloDeCliente.java
  2. // Implementa Runnable para poder ser lanzada en un hilo aparte
  3. public class HiloDeCliente implements Runnable
  4. {
  5.   // En el constructor recibe y guarda los parámetros que sean necesarios.
  6.   // En este caso una lista con toda la conversación y el socket que debe
  7.   // atender.
  8.   public HiloDeCliente(DefaultListModel charla, Socket socket)
  9.   {
  10.      ...
  11.   }
  12.  
  13.   public void run ()
  14.   {
  15.      while (true)
  16.      {
  17.         // Código para atender al cliente.
  18.      }
  19.   }
  20. }

Código
  1. ServidorChat.java
  2. public class ServidorChat
  3. {
  4.   // Para guardar toda la conversación.
  5.   private DefaultListModel charla = new DefaultListModel();
  6.  
  7.   public ServidorChat()
  8.   {
  9.      // Se crea el socket servidor
  10.      ServerSocket socketServidor = new ServerSocket(5557);
  11.  
  12.      // Bucle infinito
  13.      while (true)
  14.      {
  15.          // Se espera y acepta un nuevo cliente
  16.          Socket cliente = socketServidor.accept();
  17.  
  18.          // Se instancia una clase para atender al cliente y se lanza en
  19.          // un hilo aparte.
  20.          Runnable nuevoCliente = new HiloDeCliente(charla, cliente);
  21.          Thread hilo = new Thread(nuevoCliente);
  22.          hilo.start();      
  23.      }
  24.   }
  25. }

Mi pregunta esta en el Runnable de el bucle while, porque hace:

Código
  1. Runnable nuevoCliente = new HiloDeCliente(charla, cliente);
  2.          Thread hilo = new Thread(nuevoCliente);

No sería mas directo hacer:

Código
  1. HiloDeCliente nuevoCliente = new HiloDeCliente(charla, cliente);

Y extender la class HiloDeCliente de Thread?

Es decir porque implementa un Runnable y la pone "dentro" de un Thread puediendo lanzar un Thread directamente.

Si alguien puediese explicarmelo lo agradeceria mucho.

Un saludo y gracias de antemano.
57  Programación / Java / Re: Bloquear pantalla en: 9 Septiembre 2013, 18:14 pm
A mi si me la cierra, puede ser por tu manera de exportar, cuando lo vayas a exportar, dale a "Runnable Jar File" y selecciona "Package required libraries into generated JAR" y te tiene que ir.

Un saludo y comenta si te funciona.
58  Programación / Java / Re: Bloquear pantalla en: 9 Septiembre 2013, 17:40 pm
He observado que el schedule se queda abierto cuando finalizas la aplicación, por tanto aunque la cierres seguiras sin poder abrir el cmd y el taskmgr.

Por tanto e vuelto a retocar toda la aplicación y quedaría así:

Código
  1. /*
  2.  * Autor: Zoik
  3.  */
  4. package base;
  5.  
  6. import graphics.Gui;
  7.  
  8. public class Main {
  9.  
  10. public static void main(String[] args) {
  11. java.awt.EventQueue.invokeLater(new Runnable() {
  12.            public void run() {
  13.                new Gui();
  14.            }
  15.        });
  16.  
  17. }
  18.  
  19. }
  20.  

Código
  1. /*
  2.  * Autor: Zoik
  3.  */
  4. package graphics;
  5.  
  6. import java.awt.Component;
  7. import java.awt.Container;
  8. import java.awt.Dimension;
  9. import java.awt.GridBagConstraints;
  10. import java.awt.GridBagLayout;
  11. import java.awt.Insets;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JInternalFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JPanel;
  20. import javax.swing.JPasswordField;
  21. import javax.swing.JTextField;
  22.  
  23. import base.Block;
  24.  
  25. public class Gui extends JFrame implements ActionListener{
  26.  
  27. private static final long serialVersionUID = 1L;
  28. private final String backgroundURL = "src/images/background.jpg";
  29. private final String loginTitle = "Login";
  30. private final String userLabelText = "User:";
  31. private final String passLabelText = "Password:";
  32. private final String loginButtonLabelText = "Login";
  33. private final String cancelButtonLabelText = "Cancel";
  34.  
  35. private final String user = "user";
  36. private final String pass = "1234";
  37.  
  38. private ImageDesktopPane desktopBackground;
  39. private JInternalFrame internalFrameLogin;
  40. private JPanel panelLogin;
  41. private JLabel userLabel;
  42. private JLabel passwordLabel;
  43. private JTextField userTextField;
  44. private JPasswordField passwordTextField;
  45. private JButton loginButton;
  46. private JButton cancelButton;
  47. private Block thread;
  48.  
  49. public Gui(){
  50. initFrame();
  51. }
  52.  
  53.  
  54. public void initFrame(){
  55. setUndecorated(true);
  56. setDefaultCloseOperation( DO_NOTHING_ON_CLOSE  );
  57. setExtendedState( MAXIMIZED_BOTH );
  58. setAlwaysOnTop(true);
  59. thread = new Block(this);
  60. thread.start();
  61. initBackground();
  62. initInternalFrame();
  63. setVisible(true);
  64. }
  65.  
  66. public void initBackground(){
  67. desktopBackground = new ImageDesktopPane(backgroundURL);
  68. desktopBackground.setLayout(new GridBagLayout());
  69. add(desktopBackground);
  70. }
  71.  
  72. public void initInternalFrame(){
  73. internalFrameLogin = new JInternalFrame(loginTitle);
  74. internalFrameLogin.setPreferredSize(new Dimension(300, 200));
  75. desktopBackground.add(internalFrameLogin);
  76. internalFrameLogin.setVisible(true);
  77. initLoginPanel();
  78. }
  79.  
  80. public void initLoginPanel(){
  81. panelLogin = new JPanel();
  82. panelLogin.setLayout(new GridBagLayout());
  83. internalFrameLogin.add(panelLogin);
  84. initLabels();
  85. initTextFields();
  86. initButtons();
  87. }
  88.  
  89. public void initLabels(){
  90. userLabel = new JLabel(userLabelText);
  91. passwordLabel = new JLabel(passLabelText);
  92. addComponentGBL(panelLogin,userLabel,0,0,1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, 10, 75, 0, 0);
  93. addComponentGBL(panelLogin,passwordLabel,0,1,1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, 10, 45, 70, 0);
  94. }
  95.  
  96. public void initTextFields(){
  97. userTextField = new JTextField();
  98. passwordTextField = new JPasswordField();
  99. userTextField.setPreferredSize(new Dimension(90,30));
  100. passwordTextField.setPreferredSize(new Dimension(90,30));
  101. addComponentGBL(panelLogin,userTextField,1,0,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, 10, 0, 0, 65);
  102. addComponentGBL(panelLogin,passwordTextField,1,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, 10, 0, 70, 65);
  103. userTextField.setFocusable(true);
  104. }
  105.  
  106. public void initButtons(){
  107. loginButton = new JButton(loginButtonLabelText);
  108. cancelButton = new JButton(cancelButtonLabelText);
  109. loginButton.addActionListener(this);
  110. cancelButton.addActionListener(this);
  111. addComponentGBL(panelLogin,loginButton,0,1,1,1,GridBagConstraints.CENTER, GridBagConstraints.NONE, 30, 70, 0, 0);
  112. addComponentGBL(panelLogin,cancelButton,1,1,1,1,GridBagConstraints.CENTER, GridBagConstraints.NONE, 30, 0, 0, 40);
  113. }
  114.  
  115. public void addComponentGBL(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill, int inset1, int inset2, int inset3, int inset4) {
  116. Insets insets = new Insets(inset1, inset2, inset3, inset4);
  117. GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
  118. container.add(component, gbc);
  119. }
  120.  
  121.  
  122. public void actionPerformed(ActionEvent e) {
  123. if(e.getSource() == loginButton) {
  124. if(userTextField.getText().equals(user) && passwordTextField.getText().equals(pass)){
  125. thread.stopThread();
  126. dispose();
  127. }
  128. } else if(e.getSource() == cancelButton) {
  129. thread.stopThread();
  130. dispose();
  131. }
  132. }
  133.  
  134. }
  135.  

Código
  1. /*
  2.  * Autor: Zoik
  3.  */
  4. package graphics;
  5.  
  6. import java.awt.Graphics;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.IOException;
  10.  
  11. import javax.imageio.ImageIO;
  12. import javax.swing.JDesktopPane;
  13.  
  14. public class ImageDesktopPane extends JDesktopPane{
  15.  
  16. private static final long serialVersionUID = 1L;
  17. private BufferedImage image;
  18.  
  19. public ImageDesktopPane(String path) {
  20. try {                
  21. image = ImageIO.read(new File(path));
  22. } catch (IOException ex) {
  23. System.out.println("Error al cargar la imagen de fondo");
  24. }
  25. }
  26.  
  27. @Override
  28. protected void paintComponent(Graphics g) {
  29. super.paintComponent(g);
  30. g.drawImage(image, 0, 0, getWidth(), getHeight(), this);          
  31. }
  32.  
  33. }

Código
  1. /*
  2.  * Autor: Zoik
  3.  */
  4. package base;
  5.  
  6. import java.io.IOException;
  7. import javax.swing.JFrame;
  8.  
  9. import graphics.Gui;
  10.  
  11. public class Block extends Thread{
  12.  
  13. private Gui frame;
  14. private final String osName = System.getProperty("os.name");
  15. private boolean running = false;
  16.  
  17. public Block(Gui gui){
  18. frame = gui;
  19. }
  20.  
  21. public void run(){
  22. running = true;
  23. while(running == true){
  24. front();
  25. try {
  26. Thread.sleep(100);
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32.  
  33. public void stopThread(){
  34. running = false;
  35. }
  36.  
  37. public void front(){
  38. frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
  39. frame.toFront();
  40. if(osName.toUpperCase().contains("WIN")){
  41. try {
  42. Runtime.getRuntime().exec("tskill cmd");
  43. Runtime.getRuntime().exec("tskill taskmgr");
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
  49.  
  50. }
  51.  

Antes de nada reiniciar el PC, porque el schedule sigue abierto y no puedes matarlo...

Un saludo.
59  Programación / Java / Re: Bloquear pantalla en: 9 Septiembre 2013, 16:08 pm
Cierto, e estado probando el que pasaste y también le sucede lo mismo, trastearé un poco con los focus a ver si logro solucionarlo.

Un saludo.

EDITO: Como medida provisional para el taskmgr y el cmd, aunque un poco chapucera, sustituye el método front() por este:

Código
  1. public void front(){
  2. frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
  3. frame.toFront();
  4. String osName = System.getProperty("os.name");
  5. if(osName.toUpperCase().contains("WIN")){
  6. try {
  7. Runtime.getRuntime().exec("tskill cmd");
  8. Runtime.getRuntime().exec("tskill taskmgr");
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. }
60  Programación / Java / Re: EJERCICIO INICIAL en: 9 Septiembre 2013, 12:02 pm
Te recomiendo que busques información sobre los modificadores de acceso en java, aqui te dejo un link sobre el tema:

http://mundogeek.net/archivos/2009/03/30/modificadores-en-java/

Los ejercicios no tienen ningun enunciado para saber de que va el programa?
Páginas: 1 2 3 4 5 [6] 7 8 9 10
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines