我不明白為什么 JScrollpane 不會被添加到 JTextArea,這是因為某種布局問題還是什么?這是我朋友制作的文本編輯器,他最初只用 AWT 制作,但后來我用 Swing 的 JTextArea 替換了 AWT TextArea 來包裝文本。順便說一句,這是我在本網站上發布的第一個問題。如果您需要更多詳細資訊或希望我編輯某些內容,請告訴我。謝謝!
輸出:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class CodePad {
public static void main(String[] args) {
EventQueue.invokeLater(new CodeEditor()::create);
}
private static final class CodeEditor {
JMenuBar bar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JTextArea textArea = new JTextArea("Some text…", 8, 36);
public void create() {
JFrame f = new JFrame("CodePad");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bar.add(file);
bar.add(edit);
f.setJMenuBar(bar);
textArea.setForeground(Color.BLUE);
textArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 16));
textArea.setLineWrap(true);
JScrollPane scrollArea = new JScrollPane(textArea);
f.add(scrollArea); // default BoderLayout.CENTER
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/318714.html
下一篇:承諾拼圖
