cuốn sách gpt4 ai đã làm

java - 使 JFrame 中的 JTextArea 或 JEditorPane 可滚动

In lại Tác giả: Taklimakan 更新时间:2023-11-03 04:20:59 25 4
mua khóa gpt4 Nike

我一直在研究 Swing,以便用 Java 构建一个 css 编辑器。我一直在尝试在 JTextArea 中导出 CSS 和 HTML(我将在创建 .css 文档之后。)这是我的主布局在单击“构建”菜单项后调用的 GridLayout。

package csseditor_gui_built;
nhập java.awt.GridLayout;
nhập javax.swing.JFrame;
nhập javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.text.DefaultCaret;
nhập java.awt.Font;
nhập 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);

}
}

这很简单。只需帮助我向这些文本区域添加滚动条或 Pane 即可。网站中的任何其他建议均无效。

câu trả lời hay nhất

Nó như thế này...

Văn bản JTextArea = JTextArea mới();

Cuộn JScrollPane = JScrollPane mới(văn bản);

Chỉnh sửa phần

thêm (cuộn);

Đây là một mã làm việc có thể giúp bạn:

nhập java.awt.*;
nhập javax.swing.*;

lớp công khai JTextAreaExample
{
khoảng trống riêng tư createAndDisplayGUI()
{
Khung JFrame = JFrame mới("JTextArea Scrollable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

JTextArea tArea1 = JTextArea mới();
tArea1.setLineWrap(true);
JTextArea tArea2 = JTextArea mới();
tArea2.setLineWrap(true);
tArea1.setText("Tôi nhận được một dòng văn bản dài trong JTextArea của mình");
tArea2.setText("Tôi nhận được một dòng văn bản dài trong JTextArea của mình");

Trình cuộn JScrollPane1 = JScrollPane mới();
JScrollPane cuộn2 = JScrollPane mới();
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()
{
JTextAreaExample mới().createAndDisplayGUI();
}
});
}
}

Về java - tạo JTextArea hoặc JEditorPane trong JFrame có thể cuộn được, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/11178994/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress