亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 JMenuBar 添加到 JTextArea?

如何將 JMenuBar 添加到 JTextArea?

達令說 2023-07-13 13:52:10
我有一個JTextArea,我想JMenuBar在其中添加一個,但似乎不起作用。ta = new JTextArea();ta.setBackground(Color.RED);// ta.setLayout(null); I tried with a null layout and                        non-nullpane = new JScrollPane(ta);pane.setBounds(Main.WIDTH - (Main.WIDTH - 20), Main.HEIGHT - (Main.HEIGHT - 20), Main.WIDTH - 60, Main.HEIGHT - 500);bar = new JMenuBar();bar.setBounds(0, 0, ta.getWidth(), 20); // This won't be there if                                         // there is a non-null layout.ta.add(bar); // I also tried pane.add(bar); that didn't work either.有什么辦法可以添加JMenuBar嗎JTextArea?
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

  1. 將 JTextArea 放入 JScrollPane ——始終

  2. 將 JScrollPane 添加到使用 BorderLayout 的 JPanel 的 BorderLayout.CENTER 位置

  3. 使用 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 本身,因為這可能會干擾文本區域的功能。


查看完整回答
反對 回復 2023-07-13
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號