2012-06-24 15 views
6

He estado investigando sobre Swing para construir un editor css con Java. Estoy atrapado tratando de exportar CSS y HTML en el documento de JTextArea (lo crearé después de crear .css). Aquí está el GridLayout que mi diseño principal llama después de hacer clic en el elemento del menú "Crear".Haciendo el JTextArea o JEditorPane en un JFrame desplazable

package csseditor_gui_built; 
import java.awt.GridLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextArea; 
import javax.swing.JScrollPane; 
import javax.swing.JScrollBar; 
import javax.swing.text.DefaultCaret; 
import java.awt.Font; 
import java.awt.Color; 


public class ExportGridLayout extends JFrame { 
    public ExportGridLayout(String HTML, String CSS){ 


     GridLayout layout = new GridLayout(1,2,2,2); 
     setLayout(layout); 

     JTextArea textAreaHtml = new JTextArea(); 
     JTextArea textAreaCss = new JTextArea(); 

     //Creating a new font. 
     Font fontumuz = new Font("Courier New", Font.PLAIN, 12); 

     // Setting constructor strings 
     textAreaHtml.setText(HTML); 
     textAreaCss.setText(CSS); 

     //Additional details.. 
     textAreaHtml.setEditable(false); 
     textAreaCss.setEditable(false); 

     //Appending font to the textArea's 
     textAreaHtml.setFont(fontumuz); 
     textAreaCss.setFont(fontumuz); 

     // Adding the objects to JFrame 
     add(textAreaHtml); 
     add(textAreaCss); 

    } 
} 

Es bastante sencillo. Solo ayúdame a agregar barras de desplazamiento o paneles a estas textArea. Cualquier otra sugerencia en el sitio web no funciona.

+1

Para hacer un trabajo relacionado con HTML, considere usar [JTextPane/JEditorPane] (http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html), 'JTextArea' no está diseñado para tales cosas, en mi humilde opinión. –

+0

Es lo mismo, voy a usar, pero simplemente no puedo hacerlos desplazables – mozcelikors

Respuesta

8

Su esta manera ...

JTextArea text = new JTextArea();

JScrollPane scroll = new JScrollPane(text);

parte Editado

add(scroll);

Este es un código de trabajo por su ayuda:

import java.awt.*; 
import javax.swing.*; 

public class JTextAreaExample 
{ 
    private void createAndDisplayGUI() 
    { 
     JFrame frame = new JFrame("JTextArea Scrollable"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridLayout(1, 2, 2, 2)); 

     JTextArea tArea1 = new JTextArea(); 
     tArea1.setLineWrap(true); 
     JTextArea tArea2 = new JTextArea(); 
     tArea2.setLineWrap(true); 
     tArea1.setText("I got a long long line of text in my JTextArea"); 
     tArea2.setText("I got a long long line of text in my JTextArea"); 

     JScrollPane scroller1 = new JScrollPane(); 
     JScrollPane scroller2 = new JScrollPane(); 
     scroller1.setViewportView(tArea1); 
     scroller2.setViewportView(tArea2); 

     contentPane.add(scroller1); 
     contentPane.add(scroller2); 

     frame.setContentPane(contentPane); 
     frame.setSize(100, 100); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new JTextAreaExample().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 
+0

Me temo que no funciona. Inténtalo en el compilador. Es porque utilizo GridLayout o no, no sé – mozcelikors

+0

Funciona bien para JTextArea ... Y sí, espero que tengas suficiente entrada para hacer que JTextArea esté llena para que aparezca la barra de desplazamiento, también establezca el Tamaño de fotograma. –

+0

Realmente no funciona. Tengo demasiada entrada. Se supone que las áreas de texto deben mostrar el código de una página web completa. Bueno, lo hacen. Pero sin barras de desplazamiento. – mozcelikors

Cuestiones relacionadas