1 回答

TA贡献1784条经验 获得超7个赞
将 JTextArea 放入 JScrollPane ——始终
将 JScrollPane 添加到使用 BorderLayout 的 JPanel 的 BorderLayout.CENTER 位置
使用 JPanel 将 JMenuBar 添加到同一 BorderLayout 的 BorderLayout.PAGE_START 位置
完毕
例如。,
JTextArea ta = new JTextArea(40, 20); // give columns and rows
JScrollPane scrollPane = new JScrollPane(ta);
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(scrollPane, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
// add menu's to the menu bar here
borderLayoutPanel.add(menuBar, BorderLayout.PAGE_START);
旁注:
您调用的代码
ta.getWidth()
可能会返回宽度值 0,因为它似乎是在呈现 JTextArea之前调用的。您几乎永远不想将组件直接添加到 JTextArea 本身,因为这可能会干扰文本区域的功能。
添加回答
举报