ngdev Posted October 25, 2014 Report Share Posted October 25, 2014 hice unos ejercicios bien básicos de imprimir números aquí el codigo: public class EjerciciosGui1 { static String eje1(){ String cad=""; for(int i=0;i<=100;i++){ String s = String.format("%3d ",i); if((i+1)%10==0) cad+=s+"\n"; else cad+=s; } return cad; } static String eje2(){ String cad=""; for(int i=100;i>=0;i--){ String s = String.format("%3d ",i); if((i-1)%10==0) cad+=s+"\n"; else cad+=s; } return cad; } static String eje3(){ String cad=""; int cont=0; for(int i=0;i<=100;i++){ if(i%2==0){ String s = String.format("%3d ",i); cont++; if(cont%10==0) cad+=s+" \n"; else cad+=s+" "; } } return cad; } static String eje4(){ int suma=0; String cad; for(int i=0;i<=100;i++){ suma+=i; } cad=String.format("La suma de los 100 primeros numeros es %d",suma); return cad; } static String eje5(){ String cad1="",cad2; int cont=0; for(int i=0;i<=100;i++){ if(i%2!=0){ String s = String.format("%3d ",i); cont++; if(cont%10==0) cad1+=s+" \n"; else cad1+=s+" "; } } cad2 = String.format("Hay %d numeros imapares\n\n%s",cont,cad1); return cad2; } } ahora si lo imprimo con System.out.println(); me imprime en el formato que yo quiero como se ve en esta imagenhttp://prntscr.com/4zmrgi pero el problema surge cuando esta misma cadena la quiero imprimir en un JTextArea queda asihttp://prntscr.com/4zmrwm recién hoy me dio por probar el Swing lo estoy haciendo con netbeans quiero aclarar que mis conocimientos son muy limitados ya que llevo poco en esto de la programacion y de una manera autodidacta asi que cualquier critica es bienvenida aqui el codigo de la parte grafica (gran parte es generado por el netbeans yo solo escribi el evento del boton que segun la seleccion del JComboBox ejecute una u otra de las funciones y la muestre en el JTextArea) public class NewJFrame extends javax.swing.JFrame { public NewJFrame() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { boton1 = new javax.swing.JButton(); seleccion = new javax.swing.JComboBox(); jScrollPane1 = new javax.swing.JScrollPane(); pantalla = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); boton1.setText("Ejecutar"); boton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { boton1ActionPerformed(evt); } }); seleccion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ejercicio 1", "Ejercicio 2", "Ejercicio 3", "Ejercicio 4", "Ejercicio 5" })); pantalla.setColumns(20); pantalla.setRows(5); jScrollPane1.setViewportView(pantalla); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(139, Short.MAX_VALUE) .addComponent(seleccion, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(boton1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addComponent(jScrollPane1) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(boton1) .addComponent(seleccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); pack(); }// </editor-fold> private void boton1ActionPerformed(java.awt.event.ActionEvent evt) { if(seleccion.getSelectedItem()=="Ejercicio 1"){ pantalla.setText(ejer.eje1()); }else if(seleccion.getSelectedItem()=="Ejercicio 2"){ pantalla.setText(ejer.eje2()); }else if(seleccion.getSelectedItem()=="Ejercicio 3"){ pantalla.setText(ejer.eje3()); }else if(seleccion.getSelectedItem()=="Ejercicio 4"){ pantalla.setText(ejer.eje4()); }else{ pantalla.setText(ejer.eje5()); } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { NewJFrame j = new NewJFrame(); j.setResizable(false); j.setVisible(true); j.setTitle("Ejercicios con GUI"); } }); } private EjerciciosGui1 ejer; // Variables declaration - do not modify private javax.swing.JButton boton1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea pantalla; private javax.swing.JComboBox seleccion; // End of variables declaration } Link to comment Share on other sites More sharing options...
gunsroses Posted October 26, 2014 Report Share Posted October 26, 2014 y si rellenas con 0 los números menores a 10? 00 01 02 03 04 05 06 07 08 09 Slds Link to comment Share on other sites More sharing options...
ngdev Posted October 26, 2014 Author Report Share Posted October 26, 2014 si ya lo había pensado y por lo que veo es lo que tendré que hacer gracias por darte el tiempo de responder. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now