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 本身,因為這可能會干擾文本區域的功能。
添加回答
舉報