为了账号安全,请及时绑定邮箱和手机立即绑定

如何将 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 关注
  • 70 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信