2012-06-24 49 views
6

Tôi đã thực hiện một số nghiên cứu về Swing để xây dựng trình soạn thảo css với Java. Tôi đang cố gắng để xuất CSS và HTML trong JTextArea của (Tôi sẽ sau khi tạo tài liệu .css.) Đây là GridLayout mà bố trí chính của tôi gọi sau khi nhấp vào "Build" mục trình đơn.Tạo JTextArea hoặc JEditorPane trong một JFrame có thể cuộn

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); 

    } 
} 

Khá thẳng về phía trước. Chỉ cần giúp tôi thêm thanh cuộn hoặc panes vào các textArea này. Bất kỳ đề xuất nào khác trên trang web đều không hoạt động.

+1

Để thực hiện công việc liên quan đến HTML, hãy xem xét sử dụng [JTextPane/JEditorPane] (http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html), 'JTextArea' không có nghĩa là như vậy mọi thứ, IMHO. –

+0

Đó là điều tương tự, tôi sẽ sử dụng, nhưng không thể làm cho chúng cuộn được – mozcelikors

Trả lời

8

của nó theo cách này ...

JTextArea text = new JTextArea();

JScrollPane scroll = new JScrollPane(text);

phần Edited

add(scroll);

Đây là một mã làm việc để được giúp đỡ của bạn:

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

Tôi sợ nó không hoạt động. Chỉ cần thử nó trên trình biên dịch. Có phải vì tôi sử dụng GridLayout hay không, tôi không biết – mozcelikors

+0

Nó hoạt động tốt cho JTextArea ... Và có, tôi hy vọng bạn có đủ mục để làm cho JTextArea đầy đủ để thanh cuộn xuất hiện, cũng thiết lập Kích thước khung hình. –

+0

Nó thực sự không hoạt động. Tôi đã nhập quá nhiều. Các textareas có nghĩa vụ phải xuất mã của toàn bộ trang web. Vâng, họ làm. Nhưng không có thanh cuộn. – mozcelikors

Các vấn đề liên quan