Java Netbeans 自动调整文本区域大小

发布于 2024-12-01 20:12:14 字数 240 浏览 2 评论 0原文

我正在使用 netbeans 7.0.1 构建一个简单的 JFrame 应用程序

我正在使用 gui 生成器放置一个文本区域和几个按钮,

这些按钮位于同一垂直级别,并且右侧按钮在调整窗口大小时向右移动 - 即很好,但我希望文本区域也能这样做 - 即调整大小以适应窗口的相关宽度。

对于我的一生,我看不到这是如何完成的 - 我环顾四周,我可以找到手工编码应用程序的代码,但不能找到 netbeans gui 构建器的代码

I am using netbeans 7.0.1 to build a simple JFrame application

I am putting a textarea and a couple of buttons on using the gui builder

the buttons are on the same vertical level and the right hand button shifts right on resize of the window - that is fine but I would like the text area to do the same - i.e. resize to fit the relevant width of the window.

For the life of me I cannot see how this is done - I have looked around and I can find code for a hand coded app but not for netbeans gui builder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与君绝 2024-12-08 20:12:14

更新:啊抱歉,没有阅读完整的问题,您真的想用 netbeans 来完成它..:) 好吧,好吧,现在您有了这篇文章如何手工制作! :)

我不会使用 GUI 构建器来完成此任务。使用 FlowLayoutBorderLayout:

screenshot

屏幕截图是由以下代码生成的:

public static void main(String... args) throws Exception {
    JFrame frame = new JFrame("Test");

    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    buttonPanel.add(new JButton("Hello"));
    buttonPanel.add(new JButton("World!"));

    frame.add(buttonPanel, BorderLayout.NORTH);
    frame.add(new JTextArea("Hello World!"), BorderLayout.CENTER);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 400);
    frame.setVisible(true);
}

Update: Ah sorry, didn't read the full question, you really want to do it with netbeans.. :) Well, well, now you have this post how to do it hand-crafted aswell! :)

I wouldn't use an GUI builder for this task. It is easy to create such layout with FlowLayout and BorderLayout:

screenshot

Screenshot was produced by this code:

public static void main(String... args) throws Exception {
    JFrame frame = new JFrame("Test");

    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    buttonPanel.add(new JButton("Hello"));
    buttonPanel.add(new JButton("World!"));

    frame.add(buttonPanel, BorderLayout.NORTH);
    frame.add(new JTextArea("Hello World!"), BorderLayout.CENTER);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 400);
    frame.setVisible(true);
}
奶茶白久 2024-12-08 20:12:14

这完全取决于您使用的布局。我个人会使用GridBagLayout,可能是因为我习惯了。基本上,您应该遵循以下步骤:

  1. 将拥有文本区域的容器的布局更改为 GridBagLayout。您可以通过右键单击容器(JFrame、面板等)来完成此操作,然后您将看到“布局”菜单。它包含一个 GridBagLayout 选项。
  2. 在组件检查器中,选择拥有 JTextArea 的 JScrollPane。查看属性选项卡中的“布局”部分。它包含 GridBagConstraints,它控制 JScrollPane 的布局行为,从而控制 JTextArea。
  3. 使用布局属性:)。基本上,您应该将 X 和 Y 权重设置为 1,并将填充设置为“两者”。这将告诉 JScrollPane 填充框架上的任何垂直和水平空间,并且 X 和 Y 权重将尽可能拉动任何其他组件。

您可以在此处阅读有关 GridBagLayout 的更多信息: http://netbeans.org/kb/docs /java/gbcustomizer-basic.html

学习 GridBagLayout 可能需要几个小时,习惯它可能需要几天,但它值得学习。只是我的2分钱。

It's all about the layout you're using. I would personally use GridBagLayout, probably because I am accustomed to it. Basically, you should follow these steps:

  1. Change the layout of the container which owns the textarea to GridBagLayout. You can do that by right-clicking on the container(being it the JFrame, a panel, whatever) and there you will see the Layout menu. It contains a GridBagLayout option.
  2. In the component inspector select the JScrollPane that owns the JTextArea. Check out the "Layout" section in the properties tab. It contains the GridBagConstraints which command the layout behaviour of the JScrollPane and thus commands the JTextArea.
  3. Play with the layout properties :). Basically you should set the X and Y weight to 1, and the Fill to "Both". This will tell the JScrollPane to fill any vertical and horizontal space there is on the Frame, and the X and Y weight will pull any other components as far as possible.

You can read more about GridBagLayout here: http://netbeans.org/kb/docs/java/gbcustomizer-basic.html

Learning GridBagLayout could take a couple of hours, getting used to it could take a couple of days, but it's worth learning. Just my 2 cents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文