如何使用Java中的Layoutmanager找到面板?

发布于 2025-01-24 12:27:25 字数 534 浏览 4 评论 0原文

我的GUI任务有点问题。

这是我应该实现的结果

,这就是我创建的

有人可以告诉我如何更改我的代码,以正确找到我的3个面板吗?

在第二张图片上,您可以看到我的jframe类。

I have a little problem with my GUI task.

This is the result I should achieve

enter image description here

And this is what I created.

Can someone please tell me how to change my code, in order to locate my 3 panels correctly?

On the second picture, you can see my JFrame class.

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

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

发布评论

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

评论(2

风轻花落早 2025-01-31 12:27:25

我会从“ Inside Out” 1 开始,这样做:

“在此处输入映像说明”

  1. 由'Inside Out'我的意思是:从最小的可识别 group < /em> GUI中的组件,将它们放在单个面板上。与GUI具有的任何其他组件或控制组的组件相同。然后,在逐步的过程中,向外锻炼到较大的区域,将这些小面板布局到越来越多的面板中(具有不同的布局和适当的布局约束)。

I would, starting from the 'inside out'1, do it like this:

enter image description here

  1. By 'inside out' I mean: Starting from the smallest identifiable group of components in the GUI, layout them out on a single panel. Do the same with any other groups of components or controls the GUI has. Then in a step-by-step process, work outwards to larger areas, layout those small panels into ever-larger panels (with potentially different layouts & appropriate layout constraints).
深巷少女 2025-01-31 12:27:25

仅使用borderlayout,这是您的组件结构的外观:

  • main
    • 蓝色 +红色:borderlayout.center
      • 按钮 +红色:borderlayout.north
        • 按钮:borderlayout.westborderlayout.east
        • 红色:borderlayout.center
      • 蓝色:borderlayout.center
    • 绿色 +按钮:borderlayout.east
      • 按钮:borderlayout.northborderlayout.south
      • 绿色:borderlayout.center

在代码:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel buttonsAndRed = new JPanel(new BorderLayout());
buttonsAndRed.add(new JButton("links"), BorderLayout.WEST);
buttonsAndRed.add(new JButton("rechts"), BorderLayout.EAST);
JPanel red = new JPanel();
red.setBackground(Color.RED);
buttonsAndRed.add(red, BorderLayout.CENTER);

JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);

JPanel blueAndRed = new JPanel(new BorderLayout());
blueAndRed.add(buttonsAndRed, BorderLayout.NORTH);
blueAndRed.add(blue, BorderLayout.CENTER);

JPanel buttonsAndGreen = new JPanel(new BorderLayout());
buttonsAndGreen.add(new JButton("oben"), BorderLayout.NORTH);
buttonsAndGreen.add(new JButton("unten"), BorderLayout.SOUTH);
JPanel green = new JPanel();
green.setBackground(Color.GREEN);
buttonsAndGreen.add(green, BorderLayout.CENTER);

frame.add(blueAndRed, BorderLayout.CENTER);
frame.add(buttonsAndGreen, BorderLayout.EAST);

frame.pack();
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Using only BorderLayout, this is what your component structure could look like:

  • main
    • blue + red: BorderLayout.CENTER
      • buttons + red: BorderLayout.NORTH
        • buttons: BorderLayout.WEST and BorderLayout.EAST
        • red: BorderLayout.CENTER
      • blue: BorderLayout.CENTER
    • green + buttons: BorderLayout.EAST
      • buttons: BorderLayout.NORTH and BorderLayout.SOUTH
      • green: BorderLayout.CENTER

In code:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel buttonsAndRed = new JPanel(new BorderLayout());
buttonsAndRed.add(new JButton("links"), BorderLayout.WEST);
buttonsAndRed.add(new JButton("rechts"), BorderLayout.EAST);
JPanel red = new JPanel();
red.setBackground(Color.RED);
buttonsAndRed.add(red, BorderLayout.CENTER);

JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);

JPanel blueAndRed = new JPanel(new BorderLayout());
blueAndRed.add(buttonsAndRed, BorderLayout.NORTH);
blueAndRed.add(blue, BorderLayout.CENTER);

JPanel buttonsAndGreen = new JPanel(new BorderLayout());
buttonsAndGreen.add(new JButton("oben"), BorderLayout.NORTH);
buttonsAndGreen.add(new JButton("unten"), BorderLayout.SOUTH);
JPanel green = new JPanel();
green.setBackground(Color.GREEN);
buttonsAndGreen.add(green, BorderLayout.CENTER);

frame.add(blueAndRed, BorderLayout.CENTER);
frame.add(buttonsAndGreen, BorderLayout.EAST);

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