Java AWT 应用程序窗口填充

发布于 2024-08-18 06:28:08 字数 497 浏览 6 评论 0原文

我正在尝试用 Java 构建一个简单的 AWT 应用程序。我希望主窗口中的所有容器都按位分开。我可以通过在 BorderLayout 构造函数中设置 Hgap 和 Vgap 来实现此目的(见下文)。

但是,我不知道如何在容器和主窗口边缘之间放置一个盖子。如何向主窗口添加几个像素的填充?

import java.awt.*;
import java.applet.Applet;

public class LayoutTest extends Applet {

    public void init() {

        BorderLayout layout = new BorderLayout(8, 8);

        setLayout(layout);

        add(new Button("Left"), BorderLayout.CENTER);
        add(new Button("Right"), BorderLayout.EAST);
    }
}

I'm trying to build a simple AWT application in Java. I want all of the containers in the main window to be separated by bit. I can accomplish this by setting the Hgap and Vgap in the BorderLayout constructor (see below.)

However, I can't figure out how to put a cap between the containers and the edges of the main window. How do I add a few pixels of padding to the main window?

import java.awt.*;
import java.applet.Applet;

public class LayoutTest extends Applet {

    public void init() {

        BorderLayout layout = new BorderLayout(8, 8);

        setLayout(layout);

        add(new Button("Left"), BorderLayout.CENTER);
        add(new Button("Right"), BorderLayout.EAST);
    }
}

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

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

发布评论

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

评论(3

骄兵必败 2024-08-25 06:28:08

我同意其他答案,并建议使用 Swing(改为使用 JApplet),这将使各种事情变得更容易(例如,您可以调用 setBorder 并使用 BorderFactory 来创建边框),但在您的情况下,您可以设置通过覆盖 getInsets 来插入:

   @Override
   public Insets getInsets()
   {
      return new Insets(10,10,10,10);
   }

将 10 替换为您喜欢的任何内容。

似乎没有设置器,或者我会说使用它来代替。如果在 AWT Applet 的情况下有更好的方法来做到这一点,请有人纠正我。

如果您决定使用 Swing,请参阅:如何使用边框< /a>

I agree with the other answers and would recommend using Swing (use JApplet instead), which would make all kinds of things easier (you could just call setBorder and use BorderFactory to create a border, for example), but in your case you can set insets by overriding getInsets:

   @Override
   public Insets getInsets()
   {
      return new Insets(10,10,10,10);
   }

Replace 10 with whatever you like.

There doesn't appear to be a setter, or I would say to use that instead. If there is a better way to do this in the case of an AWT Applet, someone please correct me.

If you decide to use Swing, see: How to Use Borders

剪不断理还乱 2024-08-25 06:28:08

AWT 并不是最新的技术。因此,除非您有在 AWT 中工作的特定要求,否则我建议您查看现代替代品 SwingSWT - 更舒适,与 AWT 相比,其行为可灵活定制且可预测。

开发它们背后的一个原因正是,您在这里尝试进行的视觉微调对于 AWT 来说是不必要的困难(如果不是不可能的话)。

AWT is not the newest technology on the block. So unless you have a specific requirement to do work in AWT, I would recommend you to check out the modern replacements Swing or SWT - much more comfortable, flexible customizable and predictable in their behaviour than AWT.

One reason behind developing them was exactly that the kind of visual fine-tuning you are trying to do here is unnecessarily difficult (if not impossible) with AWT.

凡尘雨 2024-08-25 06:28:08

虽然您可能不需要设置小程序的 insets,但我建议转向 Swing(扩展 javax.swing.JApplet)。然后将 JPanel 设置为内容窗格,并设置适当宽度的 EmptyBorder 集。

另请注意,您可能很快就必须升级到更复杂的布局管理器,例如 GridBagLayout。

Whilst you could probably get away with setting the insets of the applet, I suggest moving to Swing (extend javax.swing.JApplet). Then set a JPanel as the content pane with a EmptyBorder set of the appropriate widths.

Also note, you will probably quickly have to move up to a more sophisticated layout manager, such as GridBagLayout.

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